【问题标题】:Android parent Navigation Drawer for all activities所有活动的Android父导航抽屉
【发布时间】:2014-09-15 07:13:31
【问题描述】:

我正在创建一个应用程序,它要求所有活动都使用相同的导航抽屉。为此,我创建了一个扩展 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 作为独立类运行,那么它运行完美。我必须采取哪些额外步骤才能使导航抽屉可用于所有类。

【问题讨论】:

  • 参考这个链接:androidhive.info/2013/11/…
  • @Prag'sshi 我已经说过,当我使导航类独立时它可以工作,但当我尝试在其他类中扩展它时它不工作。
  • 那将是问题所在。 setContentView() 将当前设置为活动布局的任何内容替换为您指定的新布局。也就是说,当您在扩展的活动中调用它时,它会清除您在基类中完成的 DrawerLayout 设置。
  • @MikeM。那怎么办呢?
  • @MikeM。在 valentin 的回答下查看我的评论并建议我应该怎么做。

标签: android inheritance navigation-drawer android-navigation


【解决方案1】:

根据 cmets,在扩展的 Activity 中,我们只是找到基类的 DrawerLayout 的内容视图,并将扩展的 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);   
        ...
    }
}

【讨论】:

    【解决方案2】:

    您可以在活动中使用片段。这是更好的解决方案。

    但是如果你想扩展这个活动,你应该在调用 super.onCreate() 方法之前设置布局。并且在您的布局中为每个扩展活动添加导航抽屉。

    让示例: 在 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;
        }
    }
    

    【讨论】:

    • 如果我将 NavigationDrawerClass 作为 AndroidManifest.xml 中的启动器类,您的代码将正常工作,那么 YourActivity 的布局将不可见。但是,如果我尝试将YourActivity 作为启动器类,那么应用程序会由于setContentView 无法正常工作而崩溃,因此所有视图都设置为null
    【解决方案3】:

    @MikeM 解决方案的一个更简洁的解决方案是在基类上覆盖 setContentView 并在那里完成技巧:

    @Override
    public void setContentView(int layoutResID)
    {
        super.setContentView(R.layout.navigation_drawer_class);
    
        ViewGroup content = (ViewGroup) findViewById(R.id.content_frame);
        getLayoutInflater().inflate(layoutResID, content, true);
    }
    

    然后,在扩展的活动上,只需正常调用setContentView

    setContentView(R.layout.other_class);
    

    编辑: 此外,在基类上,有必要在onPostCreate 方法而不是onCreate 上进行抽屉、抽屉切换等的初始化,以避免调用setContentView 两次(在基类onCreate 和在扩展类onCreate):

    @Override
    protected void onPostCreate(Bundle savedInstanceState)
        super.onPostCreate(savedInstanceState);
    
        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);
    }
    

    【讨论】:

      【解决方案4】:

      这对我有用

      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)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-05-09
        • 1970-01-01
        • 1970-01-01
        • 2014-08-22
        • 2014-10-29
        相关资源
        最近更新 更多