setOnItemClickListener() 不适用于自定义 ListView @ Android
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1518338/
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
setOnItemClickListener() not working on custom ListView @ Android
提问by Sameer
I have Implemented a custom ListView
by extending LinearLayout
for every row. Every row has a small thumbnail, a text and a check box
. The list view
is deployed properly and I can scroll and fling through it without any problems.
我ListView
通过LinearLayout
为每一行扩展来实现一个自定义。每行都有一个小缩略图、一个文本和一个check box
. 在list view
被正确部署,我可以滚动,并通过它没有任何问题一扔。
But The ListView
doesn't seem to respond to the setOnItemClickListener()
at all, So I had to find a workaround by setting click listener in the getView()
of the Text inside every row which is obviously creating problem when I am trying to reuse the adapter
. Does anyone have a solution?
但是 TheListView
似乎根本没有响应setOnItemClickListener()
,所以我不得不通过getView()
在每行的 Text 中设置单击侦听器来找到解决方法,这在我尝试重用adapter
. 有没有人有办法解决吗?
回答by bhatt4982
Try this
For ListView,
试试这个
对于 ListView,
final ListView list = (ListView) findViewById(R.id.list);
list.setItemsCanFocus(false);
Also, make sure that for CheckBox inside list item set focusable false
另外,请确保对于列表项内的 CheckBox 设置 focusable false
android:focusable="false"
android:focusableInTouchMode="false"
回答by Ewoks
old answer:I wrote in previous post here
旧答案:我在上一篇文章中写过
android:focusable="false"
android:clickable="false"
will not help when ImageButton is in custom view.. One must use button.setFocusable(false);
during runtime (from java source code)
当 ImageButton 处于自定义视图时将无济于事。必须button.setFocusable(false);
在运行时使用(来自 java 源代码)
Edit:There is even more elegant solution. Try to add android:descendantFocusability="blocksDescendants"
in root layout of list element. That will make clicks onListItem possible and separately u can handle Button or ImageButton clicks
编辑:还有更优雅的解决方案。尝试android:descendantFocusability="blocksDescendants"
在列表元素的根布局中添加。这将使点击 onListItem 成为可能,并且您可以单独处理 Button 或 ImageButton 点击
回答by Tomas
For a ListView where you set the item views to CheckBox
对于将项目视图设置为 CheckBox 的 ListView
android:focusable="false"
android:clickable="false"
回答by Iman Marashi
Set these properties:
设置这些属性:
android:focusable="false"
android:focusableInTouchMode="false"
for your all UI elements in your list_item.xmlfile.
用于list_item.xml文件中的所有 UI 元素。
if this is not resolved in your adapter set:
如果这在您的适配器集中没有解决:
v.imageView.setFocusable(false);
v.imageView.setFocusableInTouchMode(false);
回答by Hamza Waqas
Did you made any ViewHolder in your extended adapter class? If yes, then make an instance of your that placeholder in the setOnItemClickListener() something will may work like this.
你在你的扩展适配器类中制作了任何 ViewHolder 吗?如果是,则在 setOnItemClickListener() 中创建该占位符的实例,可能会像这样工作。
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
View rowView = v;
if (rowView == null) {
LayoutInflater inflater = this.getLayoutInflater();
// GET INFLATE OF YOUR LAYOUT.
rowView = inflater.inflate(R.layout.projectpeopledescrate, null);
// CUSTOM ViewHolder Class Created in Adapter.
// name,title,comment are my components on the same listview clicked item.
PPDViewHolder viewHolder = new PPDViewHolder();
viewHolder.name = (TextView) rowView.findViewById(R.id.ppeopledescrvname);
viewHolder.title = (TextView) rowView.findViewById(R.id.ppeopledescrvtime);
viewHolder.comment = (TextView) rowView.findViewById(R.id.ppeoplervcomment);
viewHolder.hiddenLayout = (RelativeLayout) rowView.findViewById(R.id.hiddenCommentPanel);
rowView.setTag(viewHolder);
}
// ANOTHER object instance to apply new changes.
PPDViewHolder holder = (PPDViewHolder) rowView.getTag();
// I've setted up visibility over the components. You can set your onClickListener over your buttons.
holder.comment.setVisibility(View.GONE);
holder.name.setVisibility(View.GONE);
holder.title.setVisibility(View.GONE);
holder.hiddenLayout.setVisibility(View.VISIBLE);
holder.hiddenLayout.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.MATCH_PARENT));
holder.hiddenLayout.bringToFront();
}
Hope, you want something same. Good Luck!
希望,你想要同样的东西。祝你好运!
回答by Kevin ABRIOUX
I have this code
我有这个代码
this.mListView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
Log.v(TAG,"loul");
}
});
But it didn't work
但它没有用
So i have juste put a onItemSelectedListener under and it work Oo :
所以我刚刚在下面放置了一个 onItemSelectedListener 并且它可以工作:
this.mListView.setItemsCanFocus(false);
this.mListView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
Log.v(TAG,"loul");
}
});
//listener for nothing but it allow OnItemClickListener to work
this.mListView.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});