2016-04-11 79 views
0

所以我一直在使用與cardview在Android的回收視圖和一切工作正常...我只需要知道如何我可以提醒,如果在視圖中列表項被點擊而不是整個列表項它self.Here是我的列表項的XML代碼如何知道在回收站查看項目中查看是否點擊android

<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:card_view="http://schemas.android.com/apk/res-auto" 
android:layout_height="wrap_content" 
android:layout_width="match_parent" 
card_view:cardCornerRadius="5dp" 
android:id="@+id/child_card" 
android:layout_margin="5dp"> 
<RelativeLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:textAppearance="?android:attr/textAppearanceLarge" 
     android:text="Large Text" 
     android:id="@+id/child_name" 
     android:background="@color/colorPrimaryLight" 
     android:paddingLeft="5dp" 
     /> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textAppearance="?android:attr/textAppearanceMedium" 
     android:text="Medium Text" 
     android:id="@+id/child_dob" 
     android:layout_below="@+id/child_name" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentStart="true" 
     android:textColor="#000000" 
     android:paddingLeft="5dp" 
     android:layout_marginTop="10dp" /> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textAppearance="?android:attr/textAppearanceMedium" 
     android:text="Medium Text" 
     android:id="@+id/child_gender" 
     android:textColor="#000000" 
     android:paddingLeft="5dp" 
     android:layout_below="@+id/child_dob" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentStart="true" 
     android:layout_marginTop="30dp" /> 

    <ImageView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/child_remove" 
     android:layout_alignBottom="@+id/child_gender" 
     android:layout_alignParentRight="true" 
     android:layout_alignParentEnd="true" 
     android:src="@drawable/ic_clear_black_24dp"/> 


</RelativeLayout> 

什麼我需要點擊通知的是孩子取出ImageView的。我的看法回收已初始化並提前

+0

試試這個http://stackoverflow.com/a/24933117/3790150 – saeed

+0

感謝虐待看看它@saeed – carrion

回答

2

工作fine.Thanks你可以在OnBind方法

@Override 
    public void onBindViewHolder(MyViewHolder holder, int position) { 
     holder.image.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       //your logic 
      } 
     }); 

    } 

另一種方式設置一個OnClickListener做的是,你可以在你的ViewHolder類

實現OnClickListener
public class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener { 

    public ImageView image; 

    public MyViewHolder(View itemView) { 
     super(itemView); 
     image = (ImageView) itemView.findViewById(R.id.child_remove); 
     itemView.setOnClickListener(this); 
    } 

    @Override 
    public void onClick(View v) { 
     if(v instanceOf ImageView){ 
      // your logic 
     } 
    } 

} 
+0

非常感謝我只是顯示舉杯試了一下它的工作..感謝 – carrion