2017-02-16 92 views
0

我想单击列表视图项中的特定子视图。如何点击特定的列表视图项子视图

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

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

这里是我的代码获取列表项,但我试图继续这个链进一步实现所需的目标,但我找不到合适的方法。

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

能否请你帮忙找到一种方法,点击所选项目与id子视图(在这种情况下,在0度位置) - R.id.button_delete

我将不胜感激任何帮助。

编辑*

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; 
    } 
} 
+0

你是如何实现'ListView'?你有什么?,使用表现出一定的代码适配器。 – leobelizquierdo

+0

@leobelizquierdo请参阅我的更新 – gzbuaapzroyn

+0

@gzbuaapzroyn您应该分享整个项目JAVA CLASS ...而不是将完整的项目xml发送给我们。谢谢 – ymz

回答

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

是否可以不更改代码? – gzbuaapzroyn

相关问题