0

I am using a ListView in a Navigation Drawer. Everything was working fine but recently it stop handling click events when I did not even touch that part of my code. I placed a breakpoint in onItemClick but it never gets to that line.

This is my code:

private void initializeDrawer(){

     ...

     String[] mDrawerMenu = {"Option1", "Option2", "Option3"};

     mDrawerList.setAdapter(new ArrayAdapter<String>(this, R.layout.drawer_list_item, mDrawerMenu));
     mDrawerList.setOnItemClickListener(new DrawerItemClickListener());

}

private class DrawerItemClickListener implements ListView.OnItemClickListener {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Toast toast = Toast.makeText(context, "Click on Option " + position, Toast.LENGTH_LONG);
        toast.show();
    }
}

This is "drawer_list_item.xml":

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@android:id/text1"
        android:layout_width="match_parent"
        android:layout_height="40sp"
        android:paddingLeft="16dp"
        android:paddingRight="16dp"
        android:textColor="#fff"
        android:textSize="20sp"
        android:focusable="false"
     />

Thank you for your help.

ig343
  • 277
  • 1
  • 3
  • 17
  • Did you add focusable views like Buttons to your row layouts? This discussion might be helpful: http://stackoverflow.com/questions/8955270/listview-items-are-not-clickable-why – wkarl Jul 16 '14 at 23:43
  • @iguarna - just tried your code , working fine with me, just check if your list view layout_width is set to fill parent/match parent, otherwise your click event is restricted to only text view not on whole item. – Manish Jul 17 '14 at 04:16

1 Answers1

0

I realized my navigation drawer layout was missing a "main content view". I am not sure how that is related but it solved the issue.

<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 main content view THIS WAS MISSING-->

    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   android:id="@+id/frame_container">

    </FrameLayout>

    <!-- 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.widget.DrawerLayout>
ig343
  • 277
  • 1
  • 3
  • 17