包括导航抽屉在内的所有活动

时间:2015-06-27 06:11:14

标签: android navigation-drawer

您好我试图将导航抽屉包含在我的应用中的所有活动中。但我收到错误,因为代码必须包含id为android.R.id.list的id。这是我的代码。我真正需要的是将抽屉布局包含在所有其他活动中。我搜索了很多并尝试了许多样本,但没有任何帮助我。

基类

@Override
protected void onCreate(Bundle savedInstanceState) {

    setContentView(R.layout.activity_main1);
    super.onCreate(savedInstanceState);


}
@Override
public void setContentView(int layoutResID) {

    mDrawerLayout = (DrawerLayout) getLayoutInflater().inflate(R.layout.activity_main1, null);
    mFrameLayout = (FrameLayout) mDrawerLayout.findViewById(R.id.content_frame);

    getLayoutInflater().inflate(layoutResID, mFrameLayout, true);

    super.setContentView(mDrawerLayout);

}

抽屉布局xml

<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="240dp"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:choiceMode="singleChoice"
    android:divider="@android:color/transparent"
    android:listSelector="@android:color/transparent"
    android:dividerHeight="0dp"
    android:background="#111"/>

我需要添加导航抽屉的活动是

@Override
protected void onCreate(Bundle savedInstanceState) {
    setContentView(R.layout.main_category);
    super.onCreate(savedInstanceState);

}

1 个答案:

答案 0 :(得分:0)

尝试这样,它会起作用,但我的导航抽屉类将在同一个活动页面中。

public class Alerts extends ActionBarActivity 
    implements OnItemClickListener
    {


        //for Navigation Bar
            DrawerLayout drawerLayout;
            ListView listView;
            String[] drawerlist;
            ActionBarDrawerToggle drawerListener;

            private MyAdapter4 myAdapter4;



        @Override
        protected void onCreate(Bundle savedInstanceState) 
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.alerts);



                    //navigation initialize
                            drawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
                            listView = (ListView) findViewById(R.id.drawerList);
                            drawerlist = getResources().getStringArray(R.array.drawerlist);

                            myAdapter4 = new MyAdapter4(this);
                            listView.setAdapter(myAdapter4);

                            //for navigation click

                            listView.setOnItemClickListener(new DrawerItemClickListener());

                            //Navigation Bar starts

                            drawerListener = new ActionBarDrawerToggle
                           (this, drawerLayout, R.drawable.ic_drawer, 
                                        R.string.drawer_open, R.string.drawer_close)
                            {

                                public void onDrawerClosed(View drawerView)
                                {
                                    super.onDrawerClosed(drawerView);
                                }

                                public void onDrawerOpened(View drawerView)
                                {
                                    super.onDrawerOpened(drawerView);
                                }
                            };

                                drawerLayout.setDrawerListener(drawerListener);
                                getSupportActionBar().setHomeButtonEnabled(true);
                                getSupportActionBar().setDisplayHomeAsUpEnabled(true);*/
                }

        //for Action Bar Back Button
            @Override
            public boolean onOptionsItemSelected(MenuItem item) {
                switch (item.getItemId()) 
                {
                case android.R.id.home:
                    this.finish();
                    return true;
                }
                return super.onOptionsItemSelected(item);
            }



                            // Creating the Navigation Bar

                            protected void onPostCreate(Bundle savedInstanceState)
                            {
                                super.onPostCreate(savedInstanceState);
                                drawerListener.syncState();

                            }

                            @Override
                            public boolean onOptionsItemSelected(MenuItem item)
                            {

                                if(drawerListener.onOptionsItemSelected(item))
                                {

                                    return super.onOptionsItemSelected(item);
                                }

                                switch(item.getItemId()) {
                                case R.id.Add:
                                  Intent addIntent = new Intent(this, Add.class);
                                  startActivity(addIntent);
                                  break;
                                case R.id.help:
                                  Intent helpIntent = new Intent(this, Help.class);
                                  startActivity(helpIntent);
                                  break;

                          }     return super.onOptionsItemSelected(item);
                            }

                            public boolean onCreateOptionsMenu(Menu menu) {
                                // Inflate the menu; this adds items to the action bar if it is present.
                                getMenuInflater().inflate(R.menu.testmenu, menu);
                                return super.onCreateOptionsMenu(menu);
                            }


                            class MyAdapter4 extends BaseAdapter
                            {
                                private Context context;
                                String[] list1;
                                int[] images = {R.drawable.ic_action_image1,
                                        R.drawable.ic_action_image2, 
                                        R.drawable.ic_action_image2,
                                        };      


                                public MyAdapter4(Context context){
                                    this.context = context;
                                    list1 = context.getResources().getStringArray(R.array.drawerlist);

                                }

                                @Override
                                public int getCount() {

                                    return list1.length;

                                }

                                @Override
                                public Object getItem(int position) {

                                    return list1[position];
                                }

                                @Override
                                public long getItemId(int position) {

                                    return position;
                                }

                                @Override
                                public View getView(int position, View convertView, ViewGroup parent)
                                {

                                    View row = null;
                                    if(convertView == null){

                                        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                                        row = inflater.inflate(R.layout.easy_nav1, parent, false);


                                    }
                                    else
                                    {

                                        row = convertView;

                                    }

                                    TextView titleTextView = (TextView) row.findViewById(R.id.textView1);
                                    ImageView titleImageView = (ImageView) row.findViewById(R.id.imageView1);
                                    titleTextView.setText(list1[position]);
                                    titleImageView.setImageResource(images[position]);

                                    return row;
                                }


                        }
                            // navigation drawer click listener
                            private class DrawerItemClickListener implements ListView.OnItemClickListener 
                            {

                                @Override
                                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                                    selectItem(position);
                                }

                            }

                            private void selectItem(int position) {

                                // update the main content by replacing fragments

                               // Fragment fragment = null;
                                drawerLayout.closeDrawer(listView);

                                switch (position) {
                                case 0:
                                    Intent addIntent1 = new Intent(this, YourActivtyPage1.class);
                                      startActivity(addIntent1);
                                      break;

                                case 1:
                                    Intent addIntent2 = new Intent(this, YourActivtyPage2.class);
                                      startActivity(addIntent2);
                                    break;



                                }   
                            }  

                            }

您的activivty xml文件

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawerLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <!-- Action Bar -->

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

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent" >



            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:orientation="vertical" >

// Add your xml what you need

                </LinearLayout>
        </RelativeLayout>
    </FrameLayout>

    <ListView
        android:id="@+id/drawerList"
        android:layout_width="220dp"
        android:layout_height="match_parent"
        android:layout_gravity="left"
        android:background="#fff"
        android:divider="#bdc3c7"
        android:dividerHeight="0.5dp"
        android:entries="@array/drawerlist" >
    </ListView>

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

编辑:1

在String中添加您的导航抽屉项目 的strings.xml

 <string-array name="drawerlist">
        <item>navlist1</item>
        <item>navlist2</item>


    </string-array>

    <string name="drawer_open">Open Navigation Drawer</string>
    <string name="drawer_close">Close Navigation Drawer</string>