setOnItemClickListener不使用DrawerLayout ListView

时间:2015-05-01 18:47:36

标签: android android-listview drawerlayout

我正在尝试使用ListView设置左抽屉。在单击项目时,我想采取一些操作,但OnItemClick()不会被调用。

继承我的代码。

MainActivity.java

protected void onCreate(Bundle savedInstanceState) {
    setContentView(R.layout.activity_main);
    mNewsCategories = getResources().getStringArray(R.array.category_arrays);
    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    mDrawerList = (ListView) findViewById(R.id.left_drawer);

    // Set the adapter for the list view
    mDrawerList.setAdapter(new ArrayAdapter<String>(this, R.layout.drawer_list_item, mNewsCategories));

    // Set the list's click listener
    mDrawerList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Log.i("POSITION", "onItemClick:"+position);
            mDrawerLayout.closeDrawer(mDrawerList);
        } 
    });
}

activity_main.xml中

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

    <!-- The navigation drawer -->
    <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:dividerHeight="0dp" android:background="#111" />

    <android.support.v4.view.ViewPager
        android:id="@+id/pager" android:layout_width="match_parent"
        android:layout_height="match_parent" />

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

drawer_list_item.xml

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceListItemSmall"
android:gravity="center_vertical"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:textColor="#fff"
android:focusable="false"
android:background="?android:attr/activatedBackgroundIndicator"
android:minHeight="?android:attr/listPreferredItemHeightSmall"/>

P.S。我调查了OnItemCLickListener not working in listview,似乎没有任何工作。我尝试了每一种组合。

1 个答案:

答案 0 :(得分:0)

问题是z排序。问题是抽屉列表不在视图层次结构的前面。所以需要把它带到前面。为此,我们可以使用以下代码段。

mDrawerList.setOnScrollListener(new OnScrollListener() {

        @Override
       public void onScrollStateChanged(AbsListView view, int scrollState) {
                   if (scrollState == SCROLL_STATE_IDLE) {
                             mDrawerList.bringToFront();
                                mDrawerLayout.requestLayout();
                    }               
        }

        @Override
        public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
        }
});

使用DrawerLayout和onDrawerOpened put的“setDrawerListener”:

mDrawerList.bringToFront();
mDrawerLayout.requestLayout();

以下链接将更详细地解释问题以及如何解决问题: http://vardhan-justlikethat.blogspot.com.es/2014/05/android-custom-navigation-drawer-not.html