[英]Custom Layouts for ListView Breaks OnItemClickListener
When using a custom layout for an item in listview, it turns out that OnItemClickListener does not fire. 当对列表视图中的项目使用自定义布局时,事实证明不会触发OnItemClickListener。
Previously, I was using android.R.layout.two_line_list_item. 以前,我使用的是android.R.layout.two_line_list_item。 All I did was swap out this deprecated layout for my own custom layout. 我所做的就是将这个不推荐使用的布局换成我自己的自定义布局。
I have tried: 我努力了:
-setting the choice mode to single -将选择模式设置为单一
-enabling long and short clickability in the parent list and list items -在父级列表和列表项中启用长时和短时可点击性
-enabling focus and descendant focusability -使焦点和子孙可聚焦性
-requesting focus -请求焦点
Any Feedback would be greatly appreciated!!! 任何反馈将不胜感激!!!
Thank you! 谢谢!
I am guessing you can try setting android:clickable="true" in the customlayout's xml file. 我猜您可以尝试在customlayout的xml文件中设置android:clickable =“ true” 。 But the code of your layout file could be more helpful. 但是布局文件的代码可能会更有帮助。
Here is the custom layout... 这是自定义布局...
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:focusable="false"
android:focusableInTouchMode="false" >
<TextView
android:id="@+id/itemSummary"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:focusable="false"
android:focusableInTouchMode="false"
android:textIsSelectable="true" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toLeftOf="@id/itemSummary"
android:focusable="false"
android:focusableInTouchMode="false"
android:orientation="vertical" >
<TextView
android:id="@+id/item"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="false"
android:focusableInTouchMode="false"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textIsSelectable="true" />
<TextView
android:id="@+id/subItem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/item"
android:focusable="false"
android:focusableInTouchMode="false"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textIsSelectable="true" />
</RelativeLayout>
</RelativeLayout>
Here is some view item creation code... 这是一些查看项创建代码...
adapter = new ArrayAdapter<ComplexObject>(this, R.layout.item_row, 0,
al) {
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = inflater.inflate(R.layout.item_row, parent,
false);
}
ComplexObject item = adapter.getItem(position);
((TextView) convertView.findViewById(R.id.item)).setText(item
.getShape());
((TextView) convertView.findViewById(R.id.subItem))
.setText(item.getWeight() + " lbs");
((TextView) convertView.findViewById(R.id.itemSummary))
.setText(item.getAge() + " years");
return convertView;
}
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.