在抽屉导航

时间:2015-06-15 05:57:41

标签: android listview android-activity

我已经使用了以下代码用于我的navigation drawer,它通过包含布局和创建对象以及调用函数来使用并包含在多个activities中。我想为它添加功能,当点击一个项目时,它会启动新的活动。

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

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

        <ListView
            android:layout_width="240dp"
            android:background="#eaeaea"
            android:divider="@null"
            android:layout_height="match_parent"
            android:entries="@array/drawerList"
            android:id="@+id/drawerlist"
            android:layout_gravity="right"
            />

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



public class DrawerNav {


    private Activity drawerAct;
    private ListView listView;
    private String[] drawerArray;
    private ActionBarDrawerToggle drawerListener;
    private DrawerLayout drawerLayout;

    public DrawerNav(Activity activity){
        drawerAct = activity;
    }


    public void drawerAction() {

        final ImageView setting = (ImageView) drawerAct.findViewById(R.id.setting);
        final ImageView sepTitle = (ImageView) drawerAct.findViewById(R.id.seperatorTitle);

//drawer navigation
        drawerLayout = (DrawerLayout) drawerAct.findViewById(R.id.drawerLayout);
        drawerArray = drawerAct.getResources().getStringArray(R.array.drawerList);
        listView = (ListView) drawerAct.findViewById(R.id.drawerlist);
        listView.setAdapter(new ArrayAdapter<>(this.drawerAct, R.layout.list_item_layout, drawerArray));


        drawerListener = new ActionBarDrawerToggle(this.drawerAct, drawerLayout, null, R.string.openD, R.string.closeD) {

            @Override
            public void onDrawerSlide(View drawerView, float slideOffset) {
                setting.setImageResource(R.drawable.settingfocused);
                sepTitle.setVisibility(View.GONE);
            }

            @Override
            public void onDrawerOpened(View drawerView) {
                setting.setImageResource(R.drawable.settingfocused);
            }

            @Override
            public void onDrawerClosed(View drawerView) {

                setting.setImageResource(R.drawable.setting);
                sepTitle.setVisibility(View.VISIBLE);
            }

        };
        drawerLayout.setDrawerListener(drawerListener);
        //drawer navigation

    }
}

1 个答案:

答案 0 :(得分:4)

为listView对象实现这样的OnItemClick Listener。

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
             switch (position){
                 case 0:
                     //your code to call intent
                     break;
                 case 1:
                     //your code to call intent
                     break;
                     //and so on
                 default:
                     break;
             }
            }
        });