【发布时间】:2015-12-02 15:41:44
【问题描述】:
我有一个带有ImageView's、TextView's 的列表项。问题是整个列表项不可点击,只有当我点击任何ImageView 时它才能点击。我为每个孩子设置了 android:descendantFocusability="blocksDescendants" 为 root 及以下
android:focusable="false"
android:focusableInTouchMode="false"
仍然只有ImageViews 是可点击的。下面是我的列表项 xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://splashurl.com/m22ydvb
android:id="@+id/itemdisplaylist_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:descendantFocusability="blocksDescendants"
android:minHeight="@dimen/loclist_item_minumum_height"
android:orientation="horizontal"
android:weightSum="1" >
<ImageView
android:id="@+id/listitem_pic"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="0.2"
android:contentDescription="@null"
android:focusable="false"
android:focusableInTouchMode="false" />
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.7"
android:descendantFocusability="blocksDescendants"
android:orientation="vertical"
android:weightSum="1" >
<TextView
android:id="@+id/listitem_name"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.3"
android:focusable="false"
android:focusableInTouchMode="false"
android:textColor="@color/loclistitem_text"
android:textIsSelectable="true" >
</TextView>
<TextView
android:id="@+id/listitem_address"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.5"
android:focusable="false"
android:focusableInTouchMode="false"
android:textIsSelectable="true" >
</TextView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.2"
android:descendantFocusability="blocksDescendants"
android:orientation="horizontal" >
<TextView
android:id="@+id/listitem_text2"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.4"
android:focusable="false"
android:focusableInTouchMode="false"
android:textColor="@color/loclistitem_text"
android:textIsSelectable="true" />
<ImageView
android:id="@+id/listitem_prop1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.2"
android:contentDescription="@null"
android:focusable="false"
android:focusableInTouchMode="false" />
<ImageView
android:id="@+id/listitem_prop2"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.2"
android:contentDescription="@null"
android:focusable="false"
android:focusableInTouchMode="false" />
<ImageView
android:id="@+id/listitem_prop3"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.2"
android:contentDescription="@null"
android:focusable="false"
android:focusableInTouchMode="false" />
</LinearLayout>
</LinearLayout>
<ImageView
android:id="@+id/listitem_img3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.1"
android:contentDescription="@null"
android:focusable="false"
android:focusableInTouchMode="false"
android:clickable="false"/>
</LinearLayout>
我不确定我哪里出错了,但在过去的 6 个小时里一直在苦苦挣扎。
自定义适配器代码(部分 Coe):
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
holder = new ViewHolder();
convertView = inflater.inflate(R.layout.list_listitem, parent,false);
holder.locName = (TextView) convertView
.findViewById(R.id.listitem_name);
holder.locDistance = (TextView) convertView
.findViewById(R.id.listitem_text2);
holder.locRating = (ImageView) convertView
.findViewById(R.id.listitem_img3);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
convertView.setOnClickListener(this);
holder.locName.setText(data.get(position).getLocationName());
return convertView;
请指教。
Solution
最后我能够通过在列表项布局中使用相对布局而不是线性布局来做到这一点。
【问题讨论】:
-
在
getView方法上尝试convertView.setOnClickListener(....)并删除setOnItemClickListener表单活动 -
感谢您的回复。我之前曾尝试过,现在又按照建议进行了尝试。还是同样的问题。当我单击 ImageView 时,它会执行 onClick 方法。在 TextView 点击无响应。
-
你在使用视图持有者模式吗?
-
发布您的自定义适配器代码。
-
@Tareq : 是的,我正在使用支架模式