如何单击特定列表视图项子视图

时间:2017-02-16 18:44:40

标签: android android-view android-espresso

我试图点击列表视图项中的特定子视图。

考虑ListView项目。每个项目都是具有子视图的复合视图。

我首先要选择特定的列表项,然后使用此项目视图查找具有特定ID的子项并执行单击。

这是我获取列表项的代码,但我已经尝试继续进一步实现目标,但我找不到合适的方法。

onData(anything()).inAdapterView(withId(R.id.list_view_books)).atPosition(0).??

您可以通过id - R.id.button_delete帮助找到点击所选项目的子视图(在本例中为0位置)的方法。

如果有任何帮助,我将不胜感激。

修改*

onData(anything()).inAdapterView(withId(R.id.list_view_books)).atPosition(0)工作正常,我可以获得一个元素,但我不知道如何点击这个元素的子元素。

如果项目有帮助,这是我的XML文件。

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

我的适配器

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 个答案:

答案 0 :(得分:0)

试试这个:

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();
            }
        });