我有一个列表视图,并且具有此项目样式
当我点击它的任何部分时全部被选中
但我希望得到描述和行动1 和行动2
的单独效果这种风格:
我的XML项目
...
<LinearLayout android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="@+id/linearLayout"
android:layout_marginLeft="3dp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20sp"
android:id="@+id/txtDescription"
android:text="description" />
<!--this is one separator horizontal -->
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@android:color/darker_gray"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:weightSum="100"
android:orientation="horizontal">
<LinearLayout android:layout_width="wrap_content"
android:layout_weight="50"
android:id="@+id/test1"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:id="@+id/txtAction1"
android:text="Action 1" />
</LinearLayout>
<!--this is one separator vertical-->
<View
android:layout_width="1dp"
android:layout_height="match_parent"
android:background="@android:color/darker_gray" />
<LinearLayout android:layout_width="wrap_content"
android:id="@+id/test2"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="50"
android:gravity="center"
android:id="@+id/txtAction2"
android:text="Action 2" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
...
我的班级Java
//ViewHolder
static class ViewHolderItem {
public LinearLayout test1;
public LinearLayout test2;
}
//Adapter
class RoteiroAdapter extends BaseAdapter{
private Context mContext = null;
private List<ItemRoteiro> mListaRoteiro = null;
public RoteiroAdapter(Context context, List<ItemRoteiro> lista){
this.mContext = context;
this.mListaRoteiro = lista;
}
...
...
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolderItem viewHolder;
if(convertView == null){
LayoutInflater inflater = ((Activity) mContext).getLayoutInflater();
convertView = inflater.inflate(R.layout.item_roteiro, parent, false);
viewHolder = new ViewHolderItem();
viewHolder.test1= (LinearLayout) convertView.findViewById(R.id.txtTest1);
viewHolder.test2= (LinearLayout) convertView.findViewById(R.id.txtTest2);
convertView.setTag(viewHolder);
}
else{
viewHolder = (ViewHolderItem) convertView.getTag();
}
ItemRoteiro item = mListaRoteiro.get(position);
if(item != null) {
viewHolder.test1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
viewHolder.test2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
}
return convertView;
}
}
如何解决这个问题?谢谢!
答案 0 :(得分:1)
尝试将此添加到适配器项目布局中。
android:descendantFocusability="blocksDescendants"
<强>更新强>
由于我没有注意到onClickListener位于布局而不是TextView上,您可以将Click监听器添加到TextViews或将此属性应用于LinearLayouts
android:clickable="true"
这适用于布局中的所有TextView:
android:clickable="false"