[英]ItemClickListener is not working on Clicking Navigation Drawer List Item
我可以在指定的數組中打開帶有條目的抽屜,這是唯一的問題,就是我沒有聽過單擊。我的應用程序的布局文件在這里....... [說明]抽屜列表在所有其他布局之后位於抽屜布局的末尾。 即。 滾動/線性。
<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'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'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'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);
}
});
}
}
我認為您忘記設置listview
的adapter
:
drawer_list.setAdapter(yourAdapter);
更新
嘗試添加:
android:descendantFocusability="blocksDescendants"
到您的listview
項目的xml
最外部布局。
范例:
Ahhh atlast我做到了,可能不是其他人的情況,但我仍在發布,因為新的android學習者可能會遇到這種情況。 -問題出在我的onResume()函數上,我在onResume()中使用setContentView(layout),也在onCreate()中使用它,導致沒有觸發onClickListener(),
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.