Marco Giovanni Marco Giovanni - 2 months ago 5
Android Question

Listview effect click child of item

I have a listview, and have this item style

enter image description here

when I click on any part of it is all selected

enter image description here

but I would like to get the separate effect of Description and Action 1 and Action 2

this style:

enter image description here

enter image description here

My XML item

...
<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>
...


My class 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;
}
}


How to solve this problem? Thanks!!

Answer

Try adding this to your adapters item layout.

android:descendantFocusability="blocksDescendants"

Update

Since I didn't notice that the onClickListener was on the layout instead of the TextView, you can either add the click listener to the TextViews or apply this attribute to the LinearLayouts

android:clickable="true" 

And this to all the TextViews within the layouts:

android:clickable="false"
Comments