简体   繁体   中英

ItemClickListener is not working on Clicking Navigation Drawer List Item

I can open drawer with entries in the array specified only problem is i click is not listened The Layout file for my app is here.......[clarifying] Drawer List is at the end in drawerlayout after all other layouts. ie. scroll/linear.

<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" 
    >
<ScrollView 
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/bg015"
    android:fillViewport="true" 
    android:descendantFocusability="blocksDescendants">

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="50"
        android:orientation="vertical" >

         <TextView
             android:id="@+id/tvEvents"
             android:layout_width="fill_parent"
             android:layout_height="wrap_content"
             android:textAlignment="center"
             android:background="#555555"
             android:text="Today&apos;s Events"
             android:textColor="#aaaaaa"
             android:textSize="18sp" />

     <ListView
         android:id="@+id/scheduleView"
         android:layout_width="fill_parent"
         android:layout_below="@+id/tvEvents"
         android:layout_height="209dp" />

     <TextView
         android:id="@+id/tvClasses"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/scheduleView"
        android:layout_centerHorizontal="true"
        android:textAlignment="center"
        android:fitsSystemWindows="false"
        android:text="Today&apos;s Classes"
        android:textColor="#aaaaaa"
        android:background="#555555"
        android:textSize="18sp" />

     <ListView
         android:id="@+id/classesView"
         android:layout_below="@+id/tvClasses"
         android:layout_width="fill_parent"
         android:layout_height="210dp" />



         <TextView 
             android:id="@+id/tvTasks"
             android:layout_width="fill_parent"
             android:layout_height="wrap_content"
             android:layout_below="@+id/classesView"
             android:text="Today&apos;s Task"
             android:layout_centerHorizontal="true"
             android:textAlignment="center"
             android:textColor="#aaaaaa"
             android:background="#555555"
             android:textSize="18sp" />

          <ListView
              android:id="@+id/taskView"
              android:layout_below="@+id/tvTasks"
              android:layout_width="fill_parent"
              android:layout_height="210dp"
          />
    </RelativeLayout>
    </ScrollView>

<ListView
    android:id="@+id/list_drawer"
    android:layout_width="240dp"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:background="#fff"
    android:choiceMode="singleChoice"
    android:divider="@android:color/black"
    android:dividerHeight="1dp"
    android:entries="@array/main_menu" />

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

My Activity wher i am opening drawer and clicking the drawer element is here....

public class MainActivity extends Activity {
    DrawerLayout dLayout;
    ListView drawer_list;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        dLayout=(DrawerLayout) findViewById(R.id.drawer_layout);
        drawer_list=(ListView) findViewById(R.id.list_drawer);
        drawer_list.setClickable(true);
        drawer_list.setSelector(android.R.color.holo_blue_dark);

        drawer_list.setOnItemClickListener(new OnItemClickListener(){


                @Override
                public void onItemClick(AdapterView<?> parent, View view,
                        int position, long id) {
                    // TODO Auto-generated method stub


Toast.makeText(MainActivity.this,""+position,Toast.LENGTH_LONG).show();
                    Log.d("clicked",""+position);

                }
                  });
        }

    }

I think you forgot to set the adapter of your listview :

drawer_list.setAdapter(yourAdapter);

UPDATE

Try to add :

android:descendantFocusability="blocksDescendants"

To your listview item's xml most outer layout.

Example :

OnItemCLickListener not working in listview

Ahhh atlast i did it, it may not be a case with someone else but i am still posting as a new android learner may can face this type of situation. -The problem was with my onResume() function, i was using setContentView(layout) in onResume() and was doing it in onCreate() as well, it was causing not to fire onClickListener(),

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM