简体   繁体   English

ItemClickListener不适用于单击导航抽屉列表项

[英]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 : 我认为您忘记设置listviewadapter

drawer_list.setAdapter(yourAdapter);

UPDATE 更新

Try to add : 尝试添加:

android:descendantFocusability="blocksDescendants"

To your listview item's xml most outer layout. 到您的listview项目的xml最外部布局。

Example : 范例:

OnItemCLickListener not working in listview OnItemCLickListener在列表视图中不起作用

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. Ahhh atlast我做到了,可能不是其他人的情况,但我仍在发布,因为新的android学习者可能会遇到这种情况。 -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(), -问题出在我的onResume()函数上,我在onResume()中使用setContentView(layout),也在onCreate()中使用它,导致没有触发onClickListener(),

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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