【发布时间】:2017-05-07 20:31:44
【问题描述】:
我已经尝试过使用可点击和可聚焦,但似乎没有任何东西可以让列表项成为可点击的。
xml文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/add_recipe_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/recipe_activity_add"
android:background="@color/colorPrimary"
android:textColor="#fff"/>
<ListView
android:id="@+id/recipe_list"
android:layout_width="match_parent"
android:focusable="false"
android:clickable="true"
android:layout_height="match_parent" />
java代码
recipeListView = (ListView)findViewById(R.id.recipe_list);
//make viewRecipe Work
recipeListView.setOnItemClickListener(new ListView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Log.d("MainActivity", "ListView item clicked.");
}
});
【问题讨论】:
-
列表项默认是可点击的。见this sample app。
-
@CommonsWare 不适合我
-
试试我链接的示例项目。如果示例项目在您的测试环境中工作,那么您可以确定我的项目与您的项目相比有何不同。如果示例项目在您的测试环境中不起作用,则说明测试环境存在问题。
标签: java android listview onclicklistener