Android RecycleView OnClick不适用于整个itemView

时间:2015-07-04 00:24:11

标签: android

所以问题是onClick只为imageButton而不是while itemView调用。这是我的ViewHolder类

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

更新:

因为我在我的recycleview中使用了cardviews,所以我最终将我的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());
    }
}

它有效

5 个答案:

答案 0 :(得分:1)

因为我在我的recycleview中使用了cardviews,所以我最终将我的viewholder代码更改为

isValid.Result

`它有效

答案 1 :(得分: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布局以及您可以共享的任何其他代码。

答案 2 :(得分:0)

RecyclerView不支持整个 itemview onClick方法。如果您要实施onClick,则需要在 itemview 的子视图上设置OnClickListener

与此同时,RecyclerView仅支持 itemview 上的onTouch OnTouchListener方法。

答案 3 :(得分:0)

recyclelerView中的多个onClick事件:

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

答案 4 :(得分:-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());
    }
}