ONItemClickListener无法单击导航抽屉列表项

时间:2014-11-16 05:29:28

标签: android android-layout

我可以打开带有指定数组中条目的抽屉只有问题是我点击不被监听 我的应用程序的布局文件在这里....... [澄清]抽屉列表在所有其他布局之后的drawerlayout中结束。即。滚动/线性。

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

我的活动我打开抽屉,点击抽屉元素就在这里....

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

                }
                  });
        }

    }

2 个答案:

答案 0 :(得分:0)

我想您忘了设置adapter的{​​{1}}:

listview

<强>更新

尝试添加:

drawer_list.setAdapter(yourAdapter);

android:descendantFocusability="blocksDescendants" listview最外层布局。

示例:

OnItemCLickListener not working in listview

答案 1 :(得分:0)

Ahhh atlast我做到了,它可能不是其他人的情况,但我仍然张贴作为一个新的机器学习者可能会遇到这种情况。 - 问题在于我的onResume()函数,我在onResume()中使用了setContentView(layout),并且在onCreate()中也是这样做的,它导致不触发onClickListener(),