问题
I am trying to click on a specific child view in a list view item.
Consider ListView
with items. Each item is a compound view with child views.
I want firstly select specific list item and than use this item view find child with a specific id and perform a click.
Here is my code for getting list item, but I have tried to continue this chain further to achieve desired goal, but I can't find proper methods.
onData(anything()).inAdapterView(withId(R.id.list_view_books)).atPosition(0).??
Could you please help to find a way to click on the child view of selected item (in this case at 0 position) with id
- R.id.button_delete
.
I would be grateful for any help.
EDIT*
onData(anything()).inAdapterView(withId(R.id.list_view_books)).atPosition(0)
works fine and I can get an element, but I don't know how to click on a child of this element.
Here is my XML file for the item if it helps.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:bootstrap="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/relative_layout_root"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:background="@drawable/rounded_corner"
android:padding="3dp">
<RelativeLayout
android:id="@+id/relative_layout_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="2dp">
<ImageView
android:id="@+id/image_view_color"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_centerVertical="true"
android:layout_margin="10dp"
android:contentDescription="@null"
android:padding="15dp"
android:src="@drawable/ic_add_large" />
<EditText
android:id="@+id/edit_text_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginStart="10dp"
android:layout_marginTop="15dp"
android:layout_toEndOf="@+id/image_view_color"
android:layout_toLeftOf="@+id/button_accept"
android:layout_toRightOf="@+id/image_view_color"
android:layout_toStartOf="@+id/button_accept"
android:ellipsize="none"
android:textColor="@color/white"
android:textSize="22sp"
android:textStyle="bold" />
<TextView
android:id="@+id/tvDuration"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/edit_text_name"
android:layout_alignStart="@+id/edit_text_name"
android:layout_below="@+id/edit_text_name"
android:layout_marginTop="10dp"
android:paddingBottom="10dp"
android:textColor="@color/white"
android:textSize="18sp"
tools:text="Date info" />
<com.beardedhen.androidbootstrap.BootstrapButton
android:id="@+id/button_edit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="@+id/button_remove"
android:layout_toStartOf="@+id/button_remove"
android:layout_centerVertical="true"
android:layout_margin="5dp"
bootstrap:bb_icon_left="fa-pencil"
bootstrap:bb_roundedCorners="true"
bootstrap:bb_type="warning" />
<com.beardedhen.androidbootstrap.BootstrapButton
android:id="@+id/button_accept"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="@+id/button_remove"
android:layout_toStartOf="@+id/button_remove"
android:layout_centerVertical="true"
android:layout_margin="5dp"
bootstrap:bb_icon_left="fa-check"
bootstrap:bb_roundedCorners="true"
bootstrap:bb_type="success" />
<com.beardedhen.androidbootstrap.BootstrapButton
android:id="@+id/button_remove"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_margin="5dp"
bootstrap:bb_icon_left="fa-trash-o"
bootstrap:bb_roundedCorners="true"
bootstrap:bb_type="danger" />
</RelativeLayout>
<LinearLayout
android:id="@+id/linear_layout_filter"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/relative_layout_container"
android:layout_marginBottom="15dp"
android:layout_marginEnd="15dp"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:layout_marginStart="15dp"
android:orientation="horizontal"
android:visibility="visible"
android:weightSum="2">
<RelativeLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1">
<TextView
android:id="@+id/text_view_from"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_margin="5dp"
android:gravity="center"
android:text="@string/duration_start"
android:textColor="@color/white"
android:textSize="25sp"
android:textStyle="bold" />
<com.beardedhen.androidbootstrap.BootstrapButton
android:id="@+id/button_briefing_date"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/text_view_from"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
bootstrap:bb_icon_left="fa-calendar"
bootstrap:bb_roundedCorners="true"
bootstrap:bb_type="primary"
tools:text="test" />
</RelativeLayout>
<RelativeLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1">
<TextView
android:id="@+id/text_view_to_date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_margin="5dp"
android:gravity="center"
android:text="@string/duration_end"
android:textColor="@color/white"
android:textSize="25sp"
android:textStyle="bold" />
<com.beardedhen.androidbootstrap.BootstrapButton
android:id="@+id/button_briefing_to"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/text_view_to_date"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
bootstrap:bb_icon_left="fa-calendar"
bootstrap:bb_roundedCorners="true"
bootstrap:bb_type="primary"
tools:text="test" />
</RelativeLayout>
</LinearLayout>
</RelativeLayout>
And my adapter
public class CustomAdapter extends ArrayAdapter<Book > implements View.OnClickListener{
private ArrayList<Book> dataSet;
Context mContext;
// View lookup cache
private static class ViewHolder {
Button deleteButton;
// Other fields
}
public CustomAdapter(ArrayList<Book > data, Context context) {
super(context, R.layout.list_view_item_book, data);
this.dataSet = data;
this.mContext=context;
}
@Override
public void onClick(View v) {
switch (v.getId())
{
case R.id.button_delete:
// Delete
break;
.............
}
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Book dataModel = getItem(position);
ViewHolder viewHolder; // view lookup cache stored in tag
final View result;
if (convertView == null) {
viewHolder = new ViewHolder();
LayoutInflater inflater = LayoutInflater.from(getContext());
convertView = inflater.inflate(R.layout.list_view_item_book, parent, false);
// Find views
result=convertView;
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
result=convertView;
}
viewHolder.deleteButton.setOnClickListener(this);
return convertView;
}
}
回答1:
try this:
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
View target = view.findViewById(R.id.button_delete);
target.performClick();
}
});
来源:https://stackoverflow.com/questions/42282209/how-to-click-on-specific-list-view-item-child-view