Android Navigation Drawer (triggering actions) with AbstractMainActivity

I want to have AbstractMainActivity, which creates a navigation box. There I should also handle clicks on menu items, and then trigger new actions. In these steps, I want to use the same navigation box again.

I would expand into subclasses using AbstractMainActivity and call getLayoutResourceID differently from each subclass (as suggested here: android, how to create your own activity and extend it? ).

The problem is that now in AbstractMainActivity, where I want to create a navigation box, I have no access to the navigation bar layout element (xml), since of course I want to have a different base layout for the subclasses.

Do I need to “include a layout” in all subclass layout files? But it doesn’t work, what am I doing wrong if I want to use “Actions” instead of “Snippets” with a navigation box?

public abstract class MainActivity extends Activity { private String[] menuItems; private DrawerLayout mDrawerLayout; private ListView mDrawerList; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //setContentView(R.layout.activity_main); setContentView(getLayoutResourceId()); menuItems = getResources().getStringArray(R.array.menu_items); mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); mDrawerList = (ListView) findViewById(R.id.left_drawer); // Set the adapter for the list view mDrawerList.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, menuItems)); // Set the list click listener mDrawerList.setOnItemClickListener(new DrawerItemClickListener()); } protected abstract int getLayoutResourceId(); @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } private class DrawerItemClickListener implements ListView.OnItemClickListener { @Override public void onItemClick(AdapterView parent, View view, int position, long id) { selectItem(position); } /** Swaps fragments in the main content view */ private void selectItem(int position) { //Fragment fragment = new PlanetFragment(); Bundle args = new Bundle(); // args.putInt(PlanetFragment.ARG_PLANET_NUMBER, position); Intent intent = new Intent(MainActivity.this, ProductListActivity.class); startActivity(intent); } } public class ProductListActivity extends MainActivity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.options_menu, menu); return true; } @Override protected int getLayoutResourceId() { // TODO Auto-generated method stub return R.layout.activity_product_list; } 

This is the layout of the subclass of the product list (activity_product_list.xml):

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".ProductList" > <include layout="@layout/activity_main"/> <ListView android:id="@+id/listView1" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerVertical="true" > </ListView> 

This is the layout of the navigation box (activity_main.xml):

 <android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:android1="http://schemas.android.com/apk/res/android" android:id="@+id/drawer_layout" android:layout_width="300dp" android:layout_height="500dp" > <!-- The main content view --> <FrameLayout android:id="@+id/content_frame" android:layout_width="match_parent" android:layout_height="match_parent" /> <!-- The navigation drawer --> <ListView android:id="@+id/left_drawer" android:layout_width="240dp" android:layout_height="match_parent" android:layout_gravity="start" android:choiceMode="singleChoice" android:divider="@android:color/transparent" android:dividerHeight="0dp" android:background="#c3c3c3"/> 

But this does not work, but if I don’t have it, I get null pointer exceptions when my subclass calls onCreate of the abstract class, where I want to build a navigation box, it does not find mock elements for setting lists and mock (R.id. left_drawer or R.id.drawer_layout)!

+7
source share
2 answers

I am also trying to figure out how to do this.

I saw a very good tutorial that does exactly what you want here . The idea is to create an AbstractNavDrawerActivity abstract activity class that inherits all actions with boxes. This class uses the NavDrawerActivityConfiguration bean class, which contains all the information about the navigation box, including the layout that needs to be inflated

Another approach would be to create a NavDrawerUtil class in which you put static methods that interact with the nav box. Then you call these methods from each action as you need.

The second approach gives you more flexibility, and you don’t have to worry about layout layout, etc., but I think this is a less clean solution than the first with AbstractNavDrawerActivity that all navigator overlay actions are inherited from as you suggested.

+12
source

What I did in previous applications is a sliding menu, created as a function call in an abstract activity. When you set up a new action that extends abstract activity, you make a function call in onCreate (). I'm currently working on a similar implementation using the navigation box, so I'm not quite sure if it works, but this may be a good place for you. All of your actions that will invoke the navigation box should have DrawerLayout as the top-level layout element.

+1
source

All Articles