适用于所有活动的Android父级导航抽屉 [英] Android parent Navigation Drawer for all activities
问题描述
我正在创建一个需要针对所有活动使用相同导航抽屉的应用程序.为此,我创建了一个扩展Activity
(子类需要)的类,并在其中编写了Navigation Drawer的代码.
I am creating an app which requires same Navigation Drawer for all activities. To do so, I have created a class which extends Activity
(need for child classes) and written the code for Navigation Drawer there.
public class NavigationDrawerClass extends Activity {
String [] names = new String[]{"Rohan", "Muthu","Rishi"};
private ActionBarDrawerToggle mDrawerToggle;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.navigation_drawer_class);
DrawerLayout drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
ListView list = (ListView) findViewById(R.id.left_drawer);
list.setAdapter(new ArrayAdapter<String>(NavigationDrawerClass.this, android.R.layout.simple_list_item_1, names));
// enable ActionBar app icon to behave as action to toggle nav drawer
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeButtonEnabled(true);
mDrawerToggle = new ActionBarDrawerToggle(
NavigationDrawerClass.this, /* host Activity */
drawerLayout, /* DrawerLayout object */
R.drawable.ic_drawer, /* nav drawer image to replace 'Up' caret */
R.string.open_drawer , /* "open drawer" description for accessibility */
R.string.close_drawer /* "close drawer" description for accessibility */
) {
public void onDrawerClosed(View view) {
getActionBar().setTitle("Drawer Closed");
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
public void onDrawerOpened(View drawerView) {
getActionBar().setTitle("Drawer Opened");
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
};
drawerLayout.setDrawerListener(mDrawerToggle);
}
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
mDrawerToggle.syncState();
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Pass any configuration change to the drawer toggle
mDrawerToggle.onConfigurationChanged(newConfig);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// The action bar home/up action should open or close the drawer.
// ActionBarDrawerToggle will take care of this.
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void setTitle(CharSequence title) {
getActionBar().setTitle("Navigation Drawer Example");
}
}
然后我尝试将其扩展到其他一些类,例如-public class MyActivity extends NavigationDrawerClass
,但是导航抽屉不起作用.单击抽屉图标或滑动都没有任何影响.如果我尝试将NavigationDrawerClass
作为独立类运行,则它将完美运行.为了使导航抽屉可用于所有课程,我还需要采取哪些其他步骤.
Then I am trying to extend it in some other classes eg-public class MyActivity extends NavigationDrawerClass
but the navigation drawer is not working. Neither clicking on the drawer icon or sliding has any affect. If I try to run the NavigationDrawerClass
as a stand alone class then it runs perfectly. What additional steps I have to take to make the navigation drawer available for all classes.
推荐答案
每个注释中,在扩展的Activity中,我们只是找到基类的DrawerLayout的内容视图,然后将扩展的Activity的布局扩大到其中.
Per the comments, in the extended Activity, we're simply finding the base class's DrawerLayout's content View, and inflating the extended Activity's layout into it.
public class MyActivity extends NavigationDrawerClass
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
ViewGroup content = (ViewGroup) findViewById(R.id.content_frame);
getLayoutInflater().inflate(R.layout.my_activity, content, true);
...
}
}
这篇关于适用于所有活动的Android父级导航抽屉的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!