0

I am trying to set a left Drawer with ListView. On item click, I want to take some action but OnItemClick() doesnt get called.

Heres my code.

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. I have looked into OnItemCLickListener not working in listview and nothing seems to be working. I tried every combination.

Community
  • 1
  • 1
  • What exactly have you tried already? – Joop May 01 '15 at 18:53
  • Are you sure none of the items in your list are receiving the clicks? Try setting click able to false in your text view. Also. Try removing the background activatedbackgroundindicator and see if that fixes the issue. – android_student May 01 '15 at 18:53
  • @Joop I tried every combination of android:descendantFocusability="blocksDescendants" android:clickable="false" android:focusable="false" – Saarrthi May 01 '15 at 19:03
  • @hhoang Tried removing activatedbackgroundindicator, still not working :( – Saarrthi May 01 '15 at 19:06
  • are you getting the Log ? – Alan Deep May 01 '15 at 19:11
  • Strange. Sorry, I'd have to try creating a sample app to test it. I would think that setting descendeantFocusability on your listview, as well as clickable="false", "focusable=false", and removing the background would've worked. Worst case, you could create a custom ArrayAdapter and set onclicklisteners on each view inside the getView method to call a callback on your activity to close your list. Maybe even wrapping the textview inside a linearlayout or something could work? seems like something inside your list item is getting focus. – android_student May 01 '15 at 19:27
  • I would take it out of oncreate and set the onclicklistener to "this" – Taylor Courtney May 01 '15 at 19:27

1 Answers1

0

The problem is the z ordering. The issue is drawer list is not in front of view hierarchy. So need to bring it to front. To do that we can use following code snippet.

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

Use "setDrawerListener" of DrawerLayout and onDrawerOpened put:

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

Following link will explain the problem more and how to fix it: http://vardhan-justlikethat.blogspot.com.es/2014/05/android-custom-navigation-drawer-not.html

GAJJE82
  • 1,427
  • 1
  • 10
  • 7
  • @Saarrthi I know this after long time, but if you can check this and let everyone know if it works for you, then select as the answer it would be great help to all who are looking for answers to similar problems. – GAJJE82 Jun 21 '16 at 03:38