我正在创建一个应用程序,它需要所有活动的相同导航抽屉。为此,我创建了一个扩展Activity
(需要子类)的类,并在那里编写了Navigation Drawer的代码。
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
作为一个独立的类运行,那么它运行完美。我必须采取哪些额外步骤才能使导航抽屉适用于所有类。
答案 0 :(得分:6)
根据评论,在扩展的Activity中,我们只是找到基类的DrawerLayout的内容View,并将扩展的Activity的布局膨胀到其中。
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);
...
}
}
答案 1 :(得分:1)
您可以在活动中使用片段。这是更好的解决方案。
但是如果你想扩展这个活动,你应该在调用super.onCreate()方法之前设置布局。并且在每个扩展活动的布局中都必须添加导航抽屉。
让&#39;例如: 在NavigationDrawerClass中添加方法getLayout()并调用onCreate()
public class NavigationDrawerClass extend Activity {
public void onCreate(Bundle saveInstanceState) {
super.onCreate(savedInstanceState);
setContentView(getLayout());
// your code here
}
protected int getLayout() {
return R.layout.navigation_drawer_class;
}
}
在儿童活动中使用这样的代码
public class YourActivity extends NavigationDrawerClass{
public void onCreate(Bundle saveInstanceState) {
super.onCreate();
// here do not call setContentView() method
// all your other code
}
@Override
protected int getLayout() {
return R.layout.your_activity_xml_layout;
}
}
答案 2 :(得分:0)
这项工作对我来说
public class MyDrawer extends AppCompatActivity {
ActionBarDrawerToggle toggle;
protected RelativeLayout fullLayout;
protected FrameLayout frameLayout;
@Override
public void setContentView(final int layoutResID) {
fullLayout = (RelativeLayout) getLayoutInflater().inflate(R.layout.mydrawer, null);
frameLayout = (FrameLayout) fullLayout.findViewById(R.id.drawer_frame);
getLayoutInflater().inflate(layoutResID, frameLayout, true);
super.setContentView(fullLayout);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
//setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
final DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout3);
toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
final NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
drawer.closeDrawers();
int itemId = menuItem.getItemId();
Toast.makeText(getApplicationContext(), menuItem.getTitle().toString(),
Toast.LENGTH_LONG).show();
//navigationView.getMenu().findItem(R.id.drawer_5_reasons).setChecked(true);
return true;
}
});
}
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
if (toggle.onOptionsItemSelected(item))
{
return true;
}
return super.onOptionsItemSelected(item);
}
}
的xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="0dp"
android:layout_height="0dp"
android:id="@+id/drawer_framelayout">
<FrameLayout
android:id="@+id/drawer_frame2"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start"
>
<FrameLayout
android:id="@+id/drawer_frame"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
<android.support.design.widget.NavigationView android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="@layout/nav_header_main2"
app:menu="@menu/activity_main_drawer"
android:background="#fefefd" />
</android.support.v4.widget.DrawerLayout>
</RelativeLayout>
使用:
public class yourclass extends MyDrawer {
主要问题.setOnItemClickListener
<android.support.v4.widget.DrawerLayout>
<FrameLayout>
your main content stuff here
</android.support.v4.widget.DrawerLayout>
<FrameLayout>
your main content stuff here(.setOnItemClickListener)