2015-07-04 88 views
0

所以問題是onClick只會被調用imageButton而不是while itemView。這裏是我的ViewHolder類Android RecycleView OnClick不適用於整個itemView

public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{ 
    protected TextView title; 
    protected TextView rank; 
    protected ImageView image; 
    protected ImageButton share; 
    public ViewHolder(View itemView) { 
     super(itemView); 
     title = (TextView) itemView.findViewById(R.id.main_title); 
     rank = (TextView) itemView.findViewById(R.id.rank_text); 
     image = (ImageView) itemView.findViewById(R.id.main_image); 
     share = (ImageButton) itemView.findViewById(R.id.main_share); 
     share.setOnClickListener(this); 
     itemView.setOnClickListener(this); 
    } 

    @Override 
    public void onClick(View v) { 
     Log.d("click", "clicked at" + getAdapterPosition()); 
    } 
} 

UPDATE:

因爲我是用cardviews我recycleview,我最終改變我的viewholder代碼這個

public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{ 
    protected TextView title; 
    protected TextView rank; 
    protected ImageView image; 
    protected ImageButton share; 
    protected CardView cardView; 
    public ViewHolder(View itemView) { 
     super(itemView); 
     title = (TextView) itemView.findViewById(R.id.main_title); 
     rank = (TextView) itemView.findViewById(R.id.rank_text); 
     image = (ImageView) itemView.findViewById(R.id.main_image); 
     share = (ImageButton) itemView.findViewById(R.id.main_share); 
     cardView = (CardView) itemView.findViewById(R.id.main_card_view); 
     share.setOnClickListener(this); 
     cardView.setOnClickListener(this); 
    } 

    @Override 
    public void onClick(View v) { 
     Log.d("test", "test" + getAdapterPosition()); 
    } 
} 

和它的作品

+1

看看這個:http://stackoverflow.com/questions/24471109/recyclerview-onclick –

+0

@Garr ett請按照上面的url –

回答

0

您正在使用相同的點擊收聽者itemViewshare

  • 單獨視圖點擊正確

    @Override 
    public void onClick(View view) { 
        if (view.equals(share) { 
         // 'share' was tapped 
        } 
        else { 
         // 'itemView' was tapped 
        } 
    } 
    
  • 在你的XML佈局(我在這裏猜測它:)變化如下(即在適當的地方加上clickable

    // this is your list cell (the item renderer) 
    <RelativeLayout 
        android:clickable="true" 
        ...> 
        <TextView 
         android:id="@+id/main_title" 
         android:clickable="false" 
         .../> 
        <TextView 
         android:id="@+id/rank_text" 
         android:clickable="false" 
         .../> 
        <ImageView 
         android:id="@+id/main_image" 
         android:clickable="false" 
         .../> 
        <ImageButton 
         android:id="@+id/main_share" 
         android:clickable="true" 
         .../> 
    </RelativeLayout> 
    

如果沒有出於某種原因,請發表您的XML佈局,你可以分享任何其他代碼。

0

RecyclerView不支持onClick整個方法itemview。如果要實施onClick,則需要在itemview的子視圖上設置OnClickListener

同時,RecyclerView只支持上ItemView控件OnTouchListeneronTouch方法。

+0

我正在嘗試這個類似的問題,我正在尋求解決。我很感激您的任何想法或見解。位於這裏:https://stackoverflow.com/questions/47783631/recyclerview-how-to-add-onclick-and-keep-onlongclick-working – AJW

0

因爲我是用cardviews我recycleview,我最終改變我的viewholder代碼這個

public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{ 
    protected TextView title; 
    protected TextView rank; 
    protected ImageView image; 
    protected ImageButton share; 
    protected CardView cardView; 
    public ViewHolder(View itemView) { 
     super(itemView); 
     title = (TextView) itemView.findViewById(R.id.main_title); 
     rank = (TextView) itemView.findViewById(R.id.rank_text); 
     image = (ImageView) itemView.findViewById(R.id.main_image); 
     share = (ImageButton) itemView.findViewById(R.id.main_share); 
     cardView = (CardView) itemView.findViewById(R.id.main_card_view); 
     share.setOnClickListener(this); 
     cardView.setOnClickListener(this); 
    } 

    @Override 
    public void onClick(View v) { 
     Log.d("test", "test" + getAdapterPosition()); 
    } 
} 

'和它的作品

-1
 
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{ 
    protected TextView title; 
    protected TextView rank; 
    protected ImageView image; 
    protected ImageButton share; 
    protected CardView cardView; 
    public ViewHolder(View itemView) { 
     super(itemView); 
     title = (TextView) itemView.findViewById(R.id.main_title); 
     rank = (TextView) itemView.findViewById(R.id.rank_text); 
     image = (ImageView) itemView.findViewById(R.id.main_image); 
     share = (ImageButton) itemView.findViewById(R.id.main_share); 
     cardView = (CardView) itemView.findViewById(R.id.main_card_view); 
     itemView.share.setOnClickListener(this); 
     itemView.cardView.setOnClickListener(this); 
    } 

    @Override 
    public void onClick(View v) { 
     Log.d("test", "test" + getAdapterPosition()); 
    } 
} 
0

多的onClick事件recyclerView內:

public static class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnLongClickListener{ 

    public ImageView iconImageView; 
    public TextView iconTextView; 

    public MyViewHolder(final View itemView) { 
     super(itemView); 

     iconImageView = (ImageView) itemView.findViewById(R.id.myRecyclerImageView); 
     iconTextView = (TextView) itemView.findViewById(R.id.myRecyclerTextView); 
     // set click event 
     itemView.setOnClickListener(this); 
     iconTextView.setOnClickListener(this); 
     // set long click event 
     iconImageView.setOnLongClickListener(this); 
    } 

    // onClick Listener for view 
    @Override 
    public void onClick(View v) { 

     if (v.getId() == iconTextView.getId()){ 
      Toast.makeText(v.getContext(), "ITEM PRESSED = " + String.valueOf(getAdapterPosition()), Toast.LENGTH_SHORT).show(); 
     } else { 
      Toast.makeText(v.getContext(), "ROW PRESSED = " + String.valueOf(getAdapterPosition()), Toast.LENGTH_SHORT).show(); 
     } 
    } 


    //onLongClickListener for view 
    @Override 
    public boolean onLongClick(View v) { 

     final AlertDialog.Builder builder = new AlertDialog.Builder(v.getContext()); 
     builder.setTitle ("Hello Dialog") 
       .setMessage ("LONG CLICK DIALOG WINDOW FOR ICON " + String.valueOf(getAdapterPosition())) 
       .setPositiveButton("OK", new DialogInterface.OnClickListener() { 
        @Override 
        public void onClick(DialogInterface dialog, int which) { 

        } 
       }); 

     builder.create().show(); 
     return true; 
    } 
}