所有活动中的导航抽屉

时间:2014-08-26 05:56:37

标签: android navigation-drawer android-navigation

大家好我想把导航抽屉放在所有活动中。

布局文件:

     <?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <FrameLayout
        android:id="@+id/content_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <ListView
        android:id="@+id/left_drawer"
        android:layout_width="275dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:background="@color/black"
        android:choiceMode="singleChoice"
        android:divider="@android:color/transparent"
        android:dividerHeight="0dp"
        android:paddingLeft="5dp" />

</android.support.v4.widget.DrawerLayout>

在其他活动中,我正在扩展这门课程。但这只是这个不起作用的是抽屉徽标进入其他活动页面。

请告诉我这里做错了什么。 提前致谢。

我找到了解决方案。谢谢你的帮助。

更新

请在下面找到我的答案。

5 个答案:

答案 0 :(得分:1)

创建布局drawer_layout

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout  xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<FrameLayout
    android:id="@+id/content_frame"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

<ListView
    android:id="@+id/left_drawer"
    android:layout_width="275dp"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:background="@color/black"
    android:choiceMode="singleChoice"
    android:divider="@android:color/transparent"
    android:dividerHeight="0dp"
    android:paddingLeft="5dp" />

drwaer_custom_layout_file 这适用于抽屉中的每一行。(根据要求自定义):

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginLeft="5dp" >

<LinearLayout
    android:id="@+id/itemLayoutColor"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:minHeight="50dp"
    android:orientation="horizontal" >

    <ImageView
        android:id="@+id/drawer_icon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:contentDescription="@string/sku_search"
        android:paddingLeft="15dp" />

    <TextView
        android:id="@+id/drawer_itemName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:paddingLeft="10dp"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="@color/white" />
</LinearLayout>

<View
    android:id="@+id/dividerView"
    android:layout_width="match_parent"
    android:layout_height="1dp"
    android:background="#191919"
    android:paddingLeft="15dp" >
</View>

创建Adapter课程。 (删除与您无关的元素)

public class CustomDrawerAdapter extends ArrayAdapter<DrawerItem> {

Context context;
List<DrawerItem> drawerItemList;
int layoutResID;
int selectedPosition;


public CustomDrawerAdapter(Context context, int layoutResourceID,
        List<DrawerItem> listItems, int selectedPosition) {
    super(context, layoutResourceID, listItems);
    this.context = context;
    this.drawerItemList = listItems;
    this.layoutResID = layoutResourceID;
    this.selectedPosition = selectedPosition;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub

    DrawerItemHolder drawerHolder;
    View view = convertView;

    if (view == null) {
        LayoutInflater inflater = ((Activity) context).getLayoutInflater();
        drawerHolder = new DrawerItemHolder();

        view = inflater.inflate(layoutResID, parent, false);
        drawerHolder.ItemName = (TextView) view
                .findViewById(R.id.drawer_itemName);
        drawerHolder.icon = (ImageView) view.findViewById(R.id.drawer_icon);
        drawerHolder.itemLayoutColor = (LinearLayout) view
                .findViewById(R.id.itemLayoutColor);
        // drawerHolder.dividerView = (View) view
        // .findViewById(R.id.dividerView);
        view.setTag(drawerHolder);

    } else {
        drawerHolder = (DrawerItemHolder) view.getTag();

    }

    DrawerItem dItem = (DrawerItem) this.drawerItemList.get(position);
    drawerHolder.ItemName.setTypeface(tfNormal);
    drawerHolder.ItemName.setText(dItem.getItemName());
    if (dItem.getImgResID() != 0) {
        drawerHolder.icon.setImageDrawable(view.getResources().getDrawable(
                dItem.getImgResID()));

    } else {

        drawerHolder.ItemName.setTextColor(context.getResources().getColor(
                R.color.black));
        drawerHolder.itemLayoutColor.setBackgroundColor(context
                .getResources().getColor(R.color.pGray));
        drawerHolder.icon.setVisibility(View.GONE);
        // drawerHolder.dividerView.setBackgroundColor(Color.GREEN);
    }
    if(selectedPosition == position){
        drawerHolder.itemLayoutColor.setBackgroundColor(context
                .getResources().getColor(R.color.lightyellow));
    }
    return view;
}

private static class DrawerItemHolder {
    TextView ItemName;
    ImageView icon;
    LinearLayout itemLayoutColor;
    // View dividerView;
}
}

创建一个将extends Activity写入以下方法的课程:

@Override
public void setContentView(int layoutResID) {
    mDrawerLayout = (DrawerLayout) getLayoutInflater().inflate(
            R.layout.navigation_drawer_layout, null);
    actContent = (FrameLayout) mDrawerLayout
            .findViewById(R.id.content_frame);
    getLayoutInflater().inflate(layoutResID, actContent, true);
    super.setContentView(mDrawerLayout);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    if (mDrawerToggle.onOptionsItemSelected(item)) {
    }
    return true;
}

protected void navigationDrawer(DrawerLayout mDrawerLayout,
        ListView mDrawerList, int selectedPosition) {

    this.selectedPosition = selectedPosition;

    activity = (Activity) NavigationDrawerBaseActivity.this;
    actionBar = getActionBar();

    actionBar.setDisplayHomeAsUpEnabled(true);
    actionBar.setHomeButtonEnabled(true);


    mTitle = mDrawerTitle = getTitle();
    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    mDrawerList = (ListView) findViewById(R.id.left_drawer);
    this.mDrawerLayout = mDrawerLayout;
    this.mDrawerList = mDrawerList;

    mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow,
            GravityCompat.START);

    // set up the drawer's list view with items and click listener
    mDataList = new ArrayList<DrawerItem>();

    **********Here add the items in the list***********
   i.e.
    mDataList.add(new DrawerItem(DrawerConstant.LOGOUT,
            R.drawable.ic_signout));

    adapter = new CustomDrawerAdapter(this,
            R.layout.drawer_custom_single_layout, mDataList,
            selectedPosition);

    mDrawerList.setAdapter(adapter);

    mDrawerList.setOnItemClickListener(new DrawerItemClickListener());

    mDrawerToggle = new ActionBarDrawerToggle(this, /* host Activity */
    mDrawerLayout, /* DrawerLayout object */
    R.drawable.ic_drawer, /* nav drawer image to replace 'Up' caret */
    R.string.drawer_open, /* "open drawer" description for accessibility */
    R.string.drawer_close /* "close drawer" description for accessibility */
    ) {
        public void onDrawerClosed(View view) {
            getActionBar().setTitle(mTitle);
            invalidateOptionsMenu(); // creates call to
                                        // onPrepareOptionsMenu()
        }

        public void onDrawerOpened(View drawerView) {
            getActionBar().setTitle(mDrawerTitle);
            invalidateOptionsMenu(); // creates call to
                                        // onPrepareOptionsMenu()
        }
    };
    mDrawerLayout.setDrawerListener(mDrawerToggle);

}

public class DrawerItemClickListener implements
        ListView.OnItemClickListener {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position,
            long id) {

            if (position != selectedPosition) {
                selectItem(position);
    }
}

protected void selectItem(int position) {
    // update the main content by replacing fragments

    switch (position) {
    case 0:
        // Call the another activity
        mDrawerList.setItemChecked(position, true);
        mDrawerLayout.closeDrawer(mDrawerList);
        break;
    case 1:
        // Call the another activity
        mDrawerList.setItemChecked(position, true);
        mDrawerLayout.closeDrawer(mDrawerList);
        break;
    case 2:
        // Call the another activity
        mDrawerList.setItemChecked(position, true);
        mDrawerLayout.closeDrawer(mDrawerList);
        break;

    default:
        break;
    }
}

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    // Pass any configuration change to the drawer toggls
    mDrawerToggle.onConfigurationChanged(newConfig);
}

@Override
protected void onPostCreate(Bundle savedInstanceState) {
    super.onPostCreate(savedInstanceState);
    // Sync the toggle state after onRestoreInstanceState has occurred.
    mDrawerToggle.syncState();
  }
 }

在“活动”中将以下代码添加到“导航抽屉”中

  • 扩展NavigationDrawerBaseActivity类。

  • setContentView之后调用方法:

    // set Naviagtion Drawer
    DrawerLayout mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    ListView mDrawerList = (ListView) findViewById(R.id.left_drawer);
    super.navigationDrawer(mDrawerLayout, mDrawerList, 1);
    
  • 在同一Activity中添加以下方法:

     @Override
      public boolean onOptionsItemSelected(MenuItem item) {
      if (mDrawerToggle.onOptionsItemSelected(item)) {
        }
     }
    

答案 1 :(得分:0)

创建一个实现抽屉的BaseActivity类,让所有活动扩展此BaseActivity。

@Override
public void setContentView(int layoutResID) {
    mDrawerLayout = (DrawerLayout) getLayoutInflater().inflate(R.layout.activity_main, null); 
    actContent = (FrameLayout) mDrawerLayout.findViewById(R.id.frame_container);

    setContentView(mDrawerLayout);
    getLayoutInflater().inflate(layoutResID, actContent, true);
}

将抽屉布局设为主要内容

答案 2 :(得分:0)

我已按照以下程序完成了询问方案。

<强> XML

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/venue_bg"
    android:orientation="vertical" >

    <!-- The navigation drawer -->

    <android.support.v4.widget.DrawerLayout
        android:id="@+id/drawerLayoutMain"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <FrameLayout
            android:id="@+id/frameLayoutContent"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

        <ExpandableListView
            android:id="@+id/ExpandableList"
            android:layout_width="@dimen/drawer_size"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            android:background="@drawable/left_drawer_item_gradient"
            android:choiceMode="singleChoice"
            android:clickable="true"
            android:divider="@android:color/transparent"
            android:dividerHeight="0dp" />

        <!-- content layout -->

    </android.support.v4.widget.DrawerLayout>

</LinearLayout>

NavigationDrawerBaseActivity中的setcontentView

@Override
    public void setContentView(int layoutID) 
    {
        // TODO Auto-generated method stub

        fullLayout = (LinearLayout) getLayoutInflater().inflate(R.layout.main_menu_activity_container, null);

        pocketFrame = (FrameLayout) fullLayout.findViewById(R.id.frameLayoutContent);

        getLayoutInflater().inflate(layoutID, pocketFrame, true);

        super.setContentView(fullLayout);
    }

NavigationDrawerBaseActivity制作一个设置setcontentView&amp;的方法以布局为参数,&amp;在每个扩展子类中重写该方法&amp;在参数中传递布局。在xml @ + id / ExpandableList中是一个左侧和右侧的列表。 @ + id / frameLayoutContent就像传入参数布局的容器一样,这将为您提供替换容器中的fraqgments的副本。

注意:这只是一种解决方法,建议使用Fragments。

答案 3 :(得分:0)

您可以添加一个抽象方法来获取它扩展导航抽屉的活动的布局:public abstract int NavigatonDrawerBaseActivity();并使您的类成为抽象类(此处使用setContentView(NavigatonDrawerBaseActivity()))。然后在它扩展的类中,Navigation抽屉实现了方法并将它传递给你的xml布局(不使用setContentView方法):

@Override
public int NavigatonDrawerBaseActivity() {

    return R.layout.yourLayout;
}

最后在你的布局中你需要添加NavigationDrawerBaseActivity xml的相同代码:

这可以通过片段形成我。希望它有所帮助。

答案 4 :(得分: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)
相关问题