2015-10-09 6 views
1

Я прочитал некоторый вопрос относительно этого, но все ответы касаются фрагментов, и возникает вопрос, похожий на этот, но ответ неполный, я хочу повторно использовать набор макетов или кодов в несколько действий, я создал baseActivity, который распространяется на Activity с помощью кода ниже.Расширение навигационной панели в другую активность (без фрагментов)

Я также прочитал, что вам нужно поместить код в onCreateOptionMenu, но он все еще не работает. (Код в baseacitivty XML работает, и домашняя страница XML работает, но не показывает navigation_layout)

public class BaseActivity extends Activity { 
    private ImageButton ibButtonHome; 
    private ImageButton ibButtonFavorite; 
    private ImageButton ibButtonRandomize; 
    private ImageButton ibButtonHistory; 
    private ImageButton ibButtonLogOut; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.navigation_layout); 

    } 

    View.OnClickListener Navigation = new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      Intent i = new Intent(); 
      if (v.equals(ibButtonHome)) { 
       i.setClass(getBaseContext(), HomePage.class); 
      } else if (v.equals(ibButtonFavorite)) { 
       i.setClass(getBaseContext(), Favorite.class); 
      } else if (v.equals(ibButtonHome)) { 
       i.setClass(getBaseContext(), HomePage.class); 
      } else if (v.equals(ibButtonRandomize)) { 
       i.setClass(getBaseContext(), Randomize.class); 
      } else if (v.equals(ibButtonHistory)) { 
       i.setClass(getBaseContext(), History.class); 
      } else if (v.equals(ibButtonLogOut)) { 
       //TODO: something code here to not crash on activity exit?? 
       i.setClass(getBaseContext(), MainActivity.class); 
      } 

      startActivity(i); 

     } 
    }; 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     super.onCreateOptionsMenu(menu); 
     ibButtonHome = (ImageButton) findViewById(R.id.button_Home); 
     ibButtonFavorite = (ImageButton) findViewById(R.id.button_favorites); 
     ibButtonRandomize = (ImageButton) findViewById(R.id.button_randomize); 
     ibButtonHistory = (ImageButton) findViewById(R.id.button_history); 
     ibButtonLogOut = (ImageButton) findViewById(R.id.button_logout); 

     ibButtonFavorite.setOnClickListener(Navigation); 
     ibButtonRandomize.setOnClickListener(Navigation); 
     ibButtonHome.setOnClickListener(Navigation); 
     ibButtonHistory.setOnClickListener(Navigation); 
     ibButtonLogOut.setOnClickListener(Navigation); 

     getMenuInflater().inflate(R.menu.menu_filter_menus, menu); 
     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     // Handle action bar item clicks here. The action bar will 
     // automatically handle clicks on the Home/Up button, so long 
     // as you specify a parent activity in AndroidManifest.xml. 
     int id = item.getItemId(); 

     //noinspection SimplifiableIfStatement 
     if (id == R.id.action_settings) { 
      return true; 
     } 

     return super.onOptionsItemSelected(item); 
    } 

Главной деятельность

public class HomePage extends BaseActivity { 
    private CustomAdpaterFoodFeed ExpAdapter; 
    private ArrayList<FoodFeed> foodFeeds; 
    private ExpandableListView ExpandList; 
    //Onclick listener for the Navigation Bar 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_home_page); 

     ExpandList = (ExpandableListView) findViewById(R.id.evFoodFeed); 



     //runs the function and returns the data to foodFeeds 
     foodFeeds = SetStandardGroups(); 

     //Adapter for ExapadableListView 
     ExpAdapter = new CustomAdpaterFoodFeed(HomePage.this, foodFeeds); 
     ExpandList.setAdapter(ExpAdapter); 



    } 

    // Dummy data method for pictures and comments 
    public ArrayList<FoodFeed> SetStandardGroups() { 

     String names[] = {"Geraldine", "Marielle", "Gina", "Bryan", 
       "Pat", "Eugene", "Shermaine", "Kook"}; 

     String comments[] = {"TasteGood", "Nah", "DONT EAT HERE", "Cameroon", 
       "Nice place", "chill", "woah Spain", "lalala"}; 

     int Images[] = {R.mipmap.ic_launcher, R.mipmap.ic_launcher, 
       R.mipmap.ic_launcher, R.mipmap.ic_launcher, 
       R.mipmap.ic_launcher, R.mipmap.ic_launcher, 
       R.mipmap.ic_launcher, R.mipmap.ic_launcher 
     }; 

     ArrayList<FoodFeed> list = new ArrayList<FoodFeed>(); 

     ArrayList<Comments> comments_list; 


     for (int images : Images) { 
      FoodFeed gru = new FoodFeed(); 
      gru.setIcon(images); 

      comments_list = new ArrayList<Comments>(); 
      for (int j = 0; j < 4; j++) { 
       Comments comments1 = new Comments(); 
       comments1.setName(names[j]); 
       comments1.setComments(comments[j]); 

       comments_list.add(comments1); 
      } 
      gru.setComments(comments_list); 
      list.add(gru); 


     } 

     return list; 
    } 


    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.menu_home_page, menu); 
     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     // Handle action bar item clicks here. The action bar will 
     // automatically handle clicks on the Home/Up button, so long 
     // as you specify a parent activity in AndroidManifest.xml. 
     int id = item.getItemId(); 

     //noinspection SimplifiableIfStatement 
     if (id == R.id.action_settings) { 
      return true; 
     } 

     return super.onOptionsItemSelected(item); 
    } 
} 
+0

только один вид деятельности может быть виден одновременно, но вы можете использовать базовую активность для расширить навигационный ящик в других действиях – Awadesh

+0

вы можете расширить макет активности во фрагментах, потому что они являются частью активности – Awadesh

+0

Я уже расширил базовую активность до других моих действий, но она все еще не работает, я думаю, что мне не хватает что-то @Awadesh – SCS

ответ

0

базовой активность расширить панель навигации в других видах деятельности вы можете следовать эта ссылка, хорошо описана. это хорошо протестировано я следовал тому же :)

http://androiddeveloperdemo.blogspot.in/2014/08/android-navigation-drawer-with-multiple.html

Вы можете получить ту же панель действий в других видах деятельности, объявив в AndroidManifest.xml как этот

<activity 
     android:name=".SettingsActivity" 
     android:label="@string/activity_title" 
     android:theme="@style/AppTheme" /> 

Для различных вариантов меню определяют xml-файл в папке меню в студии android и раздуйте этот файл в onCreateOptionsMenu (Меню) переопределенный метод вашей активности

+0

проблема с этим я не знаю, как удалить анимацию в боковой панели, я только хочу, чтобы navbar быть похожим на instagram navbar, я просто новичок в android :( – SCS

Смежные вопросы