在drawerlayout中默认listview的onitemclick不起作用

时间:2015-11-14 17:40:17

标签: android listview drawerlayout android-navigation-drawer

我是Android编程的新手。最近我正在尝试构建一个需要抽象布局的应用程序。我按照Android开发者指南中的示例,似乎找到但我发现我无法将onItemClicklistener设置为drawerLayout中的listView。

我试图谷歌它,发现没有任何帮助(setfocusable(false)不起作用)。我的片段包含listview和recyclerview。当我使用调试模式时,我发现程序甚至无法进入onItemClick方法。希望任何人都可以提供帮助!

  private class DrawerItemClickListener implements ListView.OnItemClickListener {
    @Override
    public void onItemClick(AdapterView parent, View view, int position, long id) {
        mDrawerList.setItemChecked(position, true);
    }
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.fragment_enter, container, false);
    RecyclerView mRecyclerView = (RecyclerView) rootView.findViewById(R.id.recycler_main_content);
    RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getActivity());

    rootView.findViewById(R.id.button_add_question).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(getActivity(), NewQuestionActivity.class);
            startActivity(intent);
        }
    });

    DrawerLayout mDrawerLayout = (DrawerLayout) rootView.findViewById(R.id.drawer_layout);
    mDrawerList = (ListView) rootView.findViewById(R.id.left_drawer);

    mDrawerList.setAdapter(new ArrayAdapter<>(this.getActivity(), R.layout.list_item_main_drawer, mDrawerItem));
    mDrawerList.setOnItemClickListener(new DrawerItemClickListener());

    mRecyclerView.setLayoutManager(mLayoutManager);

    mAdapter = new MainContentListAdapter(mList);
    mRecyclerView.setAdapter(mAdapter);


    mRecyclerView.addOnItemTouchListener(
            new RecyclerItemClickListener(getActivity(), new RecyclerItemClickListener.OnItemClickListener() {
                @Override
                public void onItemClick(View view, int position) {
                    Intent intent = new Intent(getActivity(), DetailActivity.class);
                    intent.putExtra("questionId", mList.get(position).getQuestionId());
                    startActivity(intent);
                }
            })
    );

    return rootView;
}

我的XML文件

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


<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <android.support.v7.widget.RecyclerView
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/recycler_main_content"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

    <com.getbase.floatingactionbutton.FloatingActionsMenu
        android:id="@+id/button_click"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        fab:fab_addButtonColorNormal="@color/black_semi_transparent"
        fab:fab_addButtonColorPressed="@color/black_semi_transparent"
        fab:fab_addButtonPlusIconColor="@color/half_black"
        fab:fab_labelStyle="@style/menu_labels_style"
        fab:fab_labelsPosition="left"
        android:longClickable="false"
        android:layout_marginBottom="49dp"
        android:layout_alignParentBottom="true"
        android:layout_alignParentEnd="true">

        <com.getbase.floatingactionbutton.FloatingActionButton
            android:id="@+id/button_add_question"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            fab:fab_colorNormal="@color/blue_semi_transparent"
            fab:fab_title="Raise Quesion"
            fab:fab_colorPressed="@color/blue_semi_transparent_pressed"/>

        <com.getbase.floatingactionbutton.FloatingActionButton
            android:id="@+id/button_nothing"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            fab:fab_colorNormal="@color/blue_semi_transparent"
            fab:fab_title="nothing"
            fab:fab_colorPressed="@color/blue_semi_transparent_pressed"/>

    </com.getbase.floatingactionbutton.FloatingActionsMenu>

</RelativeLayout>

<ListView
    android:id="@+id/left_drawer"
    android:layout_width="240dp"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:dividerHeight="2dp"
    android:background="#111"/></android.support.v4.widget.DrawerLayout>

更新

我发现listView总是在recyclerView后面,也许这就是问题所在。但是,我尝试过bringToFront,但似乎也无效。有什么帮助吗?

1 个答案:

答案 0 :(得分:0)

尝试做这样的事情:

drawerListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

                @Override
                public void onItemClick(AdapterView<?> arg0, View view,
                        int position, long id) {
                 Intent intent = new Intent(getActivity(), DetailActivity.class);
                intent.putExtra("questionId", mList.get(position).getQuestionId());
                startActivity(intent);
    }
            });

而不是:

mDrawerList.setOnItemClickListener(new DrawerItemClickListener());