为什么OnItemClick关闭抽屉

时间:2014-06-25 01:45:35

标签: java android eclipse navigation-drawer

我不明白为什么当我点击抽屉列表的项目时,抽屉只是关闭而且什么都不做。我认为DrawerItemClickListener它无法正常工作......你能帮助我吗?我是java的新手。

my onCreate:

        setContentView(R.layout.meus_horarios);
        setTitle("  Meus Horários");
        mNavigationDrawerItemTitles= getResources().getStringArray(R.array.navigation_drawer_items_array);
        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        mDrawerList = (ListView) findViewById(R.id.left_drawer);
        ObjectDrawerItem[] drawerItem = new ObjectDrawerItem[3];
        drawerItem[0] = new ObjectDrawerItem(R.drawable.ic_action_discard, "Brinquedos");
        drawerItem[1] = new ObjectDrawerItem(R.drawable.ic_action_discard, "Meus Horários");
        drawerItem[2] = new ObjectDrawerItem(R.drawable.ic_action_discard, "Sair");
        DrawerItemCustomAdapter adapter = new DrawerItemCustomAdapter(this, R.layout.listview_item_row, drawerItem);
        mDrawerList.setAdapter(adapter);
        mDrawerList.setOnItemClickListener(new DrawerItemClickListener());

和我的班级:

public class DrawerItemCustomAdapter extends ArrayAdapter<ObjectDrawerItem> {

        Context mContext;
        int layoutResourceId;
        ObjectDrawerItem data[] = null;

        public DrawerItemCustomAdapter(Context mContext, int layoutResourceId, ObjectDrawerItem[] data) {

            super(mContext, layoutResourceId, data);
            this.layoutResourceId = layoutResourceId;
            this.mContext = mContext;
            this.data = data;
        }

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

            View listItem = convertView;

            LayoutInflater inflater = ((Activity) mContext).getLayoutInflater();
            listItem = inflater.inflate(layoutResourceId, parent, false);

            ImageView imageViewIcon = (ImageView) listItem.findViewById(R.id.imageViewIcon);
            TextView textViewName = (TextView) listItem.findViewById(R.id.textViewName);

            ObjectDrawerItem folder = data[position];


            imageViewIcon.setImageResource(folder.icon);
            textViewName.setText(folder.name);

            return listItem;
        }

    public class ObjectDrawerItem {

        public int icon;
        public String name;

        // Constructor.
        public ObjectDrawerItem(int icon, String name) {

            this.icon = icon;
            this.name = name;
        }
    }

    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) {

        switch (position) {
        case 0:
            CarregarTelaBrinquedos();
            break;
        case 1:
            CarregarMeusHorarios();
            break;
        case 2:
            btnSair.callOnClick();
            break;

        default:
            break;
        }

    }

片段:

<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" >

<!-- Navigation Drawer -->

<ListView
        android:id="@+id/left_drawer"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:background="#111"
        android:choiceMode="singleChoice"
        android:divider="@android:color/transparent"
        android:dividerHeight="0dp"
         />

<!-- Conteudo principal -->

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/MeusHorarios"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/fundoo" >

    <ListView
        android:id="@+id/lstMeusHorarios"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true" />

</RelativeLayout>


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

1 个答案:

答案 0 :(得分:0)

尝试以下方法:

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

        switch (position) {
        case 0:
            mDrawerLayout.closeDrawer(mDrawerList);

            Handler handler0 = new Handler();
            handler0.postDelayed(new Runnable() {

                @Override
                public void run() {
                    // TODO Auto-generated method stub

                }
            }, 250);

            break;
        case 1:
            mDrawerLayout.closeDrawer(mDrawerList);

            Handler handler0 = new Handler();
            handler0.postDelayed(new Runnable() {

                @Override
                public void run() {
                    // TODO Auto-generated method stub

                }
            }, 250);

            break;

         //case 2: etc...

        }
    }

}