如何处理 Android 中的 ListView 点击
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2468100/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to handle ListView click in Android
提问by teepusink
How do I listen to click event on a ListView?
如何监听 ListView 上的点击事件?
This is what I have now
这就是我现在所拥有的
ListView list = (ListView)findViewById(R.id.ListView01);
...
list.setAdapter(adapter);
When I do the following
当我执行以下操作时
list.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView parentView, View childView,
int position, long id)
{
setDetail(position);
}
public void onNothingSelected(AdapterView parentView) {
}
});
That doesn't seem to do anything on click.
And all those code live within a class that extends Activity.
这似乎对点击没有任何作用。
所有这些代码都存在于一个扩展 Activity 的类中。
回答by dxh
On your list view, use setOnItemClickListener
在您的列表视图中,使用 setOnItemClickListener
回答by Aditya Mehta
Suppose ListView object is lv, do the following-
假设 ListView 对象是 lv,请执行以下操作-
lv.setClickable(true);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
Object o = lv.getItemAtPosition(position);
/* write you handling code like...
String st = "sdcard/";
File f = new File(st+o.toString());
// do whatever u want to do with 'f' File object
*/
}
});
回答by Vijay C
You need to set the inflated view "Clickable" and "able to listen to click events" in your adapter class getView() method.
您需要在您的适配器类 getView() 方法中设置膨胀视图“可点击”和“能够监听点击事件”。
convertView = mInflater.inflate(R.layout.list_item_text, null);
convertView.setClickable(true);
convertView.setOnClickListener(myClickListener);
and declare the click listener in your ListActivity as follows,
并在您的 ListActivity 中声明点击侦听器,如下所示,
public OnClickListener myClickListener = new OnClickListener() {
public void onClick(View v) {
//code to be written to handle the click event
}
};
This holds true only when you are customizing the Adapter by extending BaseAdapter.
这仅在您通过扩展 BaseAdapter 来自定义 Adapter 时才适用。
Refer the ANDROID_SDK/samples/ApiDemos/src/com/example/android/apis/view/List14.java for more details
更多详情请参考 ANDROID_SDK/samples/ApiDemos/src/com/example/android/apis/view/List14.java
回答by Dimitar Dimitrov
The two answers before mine are correct - you can use OnItemClickListener
.
我之前的两个答案是正确的 - 你可以使用OnItemClickListener
.
It's good to note that the difference between OnItemClickListener
and OnItemSelectedListener
, while sounding subtle, is in fact significant, as item selection and focus are related with the touch mode of your AdapterView
.
这是很好的注意,差之间OnItemClickListener
和OnItemSelectedListener
,虽然听上去含蓄,其实显著,为项目选择和重点与您的触摸模式相关AdapterView
。
By default, in touch mode, there is no selection and focus. You can take a look herefor further info on the subject.
默认情况下,在触摸模式下,没有选择和焦点。您可以在此处查看有关该主题的更多信息。
回答by Amio.io
This solution is really minimalistic and doesn't mess up your code.
该解决方案非常简约,不会弄乱您的代码。
In your list_item.xml(NOT listView!) assign the attribute android:onClicklike this:
在你的list_item.xml(不是 listView!)中,像这样分配属性android:onClick:
<RelativeLayout android:onClick="onClickDoSomething">
and then in your activity call this method:
然后在您的活动中调用此方法:
public void onClickDoSomething(View view) {
// the view is the line you have clicked on
}
回答by Shudy
You have to use setOnItemClickListener
someone said.
The code should be like this:
你setOnItemClickListener
得用别人说的。
代码应该是这样的:
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// When clicked, show a toast with the TextView text or do whatever you need.
Toast.makeText(getApplicationContext(), ((TextView) view).getText(), Toast.LENGTH_SHORT).show();
}
});
回答by bourax webmaster
First, the class must implements the click listenener :
首先,该类必须实现点击监听器:
implements OnItemClickListener
Then set a listener to the ListView
然后为 ListView 设置一个监听器
yourList.setOnItemclickListener(this);
And finally, create the clic method:
最后,创建 clic 方法:
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
Toast.makeText(MainActivity.this, "You Clicked at " +countries[+ position], Toast.LENGTH_SHORT).show();
}
you can take a look and download code here
您可以在此处查看并下载代码
回答by Amey Haldankar
Use setOnItemClickListener() api in your activity. Following is the sample.
在您的活动中使用 setOnItemClickListener() api。以下是示例。
listView.setOnItemClickListener(new AdapterView.OnItemClickListener(){
@Override
public void onItemClick(AdapterView<> parent, View view, int position, long id)
{
// your code here.
}
});
回答by Allen
In Kotlin, add a listener to your listView as simple as java
在 Kotlin 中,为你的 listView 添加一个监听器就像 java 一样简单
your_listview.setOnItemClickListener { parent, view, position, id ->
Toast.makeText(this, position, Toast.LENGTH_SHORT).show()
}