如何从RecyclerView对CardView项目进行点击事件

时间:2019-04-22 07:54:38

标签: android android-recyclerview

我引用了Simple Android RecyclerView example来进行Recyclerview中项目的点击事件。 然后我将这种方式从Recyclerview应用于CardView,但失败了。

ViewHolder上的

OnClick事件不起作用。 看起来代码几乎相同。 我不知道是什么问题。

我刚刚从Simple Android RecyclerView example

复制了代码

然后将ArrayList更改为ArrayList 另外,我用cardview替换了包含textview代码的xml。

当我将onClickListener放在onBindViewHolder上时,该事件成功了。 但是我听说它使应用变得更重。所以我想避免这种方式。

MainActivity.java

public class MainActivity extends AppCompatActivity implements MyAdapter.ItemClickListener{

    RecyclerView mRecyclerView;
    MyAdapter mAdapter;
    RecyclerView.LayoutManager mLayoutManager;
    ArrayList<MyData> myDataset;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mRecyclerView = (RecyclerView) findViewById(R.id.my_recycler_view);

        // use this setting to improve performance if you know that changes
        // in content do not change the layout size of the RecyclerView
        mRecyclerView.setHasFixedSize(true);

        // use a linear layout manager
        mLayoutManager = new LinearLayoutManager(this);
        mRecyclerView.setLayoutManager(mLayoutManager);

        myDataset = new ArrayList<>();
        myDataset.add(new MyData("#InsideOut", R.drawable.a));
        myDataset.add(new MyData("#Mini", R.drawable.b));
        myDataset.add(new MyData("#ToyStroy", R.drawable.c));

        mAdapter = new MyAdapter(this, myDataset);
        mAdapter.setClickListener(this);
        mRecyclerView.setAdapter(mAdapter);
    }
    @Override
    public void onItemClick(View view, int position) {
        Toast.makeText(this, "You clicked " + mAdapter.getItem(position) + " on row number " + position, Toast.LENGTH_SHORT).show();
    }
}

MyAdapter.java

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
    private ArrayList<MyData> mDataset;
    private LayoutInflater mInflater;

    private ItemClickListener mClickListener;


    public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
        // each data item is just a string in this case
        public ImageView mImageView;
        public TextView mTextView;
        public CardView cv;

        public ViewHolder(View view) {
            super(view);
            mImageView = (ImageView)view.findViewById(R.id.image);
            mTextView = (TextView)view.findViewById(R.id.textview);
            cv = (CardView)view.findViewById(R.id.cardview);
            view.setOnClickListener(this);
        }

        @Override
        public void onClick(View view) {
            if (mClickListener != null) mClickListener.onItemClick(view, getAdapterPosition());
            Log.d("이거이거", ""+getAdapterPosition());
        }
    }

    // Provide a suitable constructor (depends on the kind of dataset)
    public MyAdapter(Context context, ArrayList<MyData> myDataset) {
        this.mDataset = myDataset;
        this.mInflater = LayoutInflater.from(context);
    }

    // Create new views (invoked by the layout manager)
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = mInflater.inflate(R.layout.item_cardview, parent, false);
        return new ViewHolder(v);
    }

    // Replace the contents of a view (invoked by the layout manager)
    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {
        // - get element from your dataset at this position
        // - replace the contents of the view with that element
        Log.d("position", position+"");

        holder.mTextView.setText(mDataset.get(position).text);
        holder.mImageView.setImageResource(mDataset.get(position).img);

    }

    // Return the size of your dataset (invoked by the layout manager)
    @Override
    public int getItemCount() {
        return mDataset.size();
    }
    MyData getItem(int id) {
        return mDataset.get(id);
    }

    void setClickListener(ItemClickListener itemClickListener) {
        this.mClickListener = itemClickListener;
    }
    public interface ItemClickListener {
        void onItemClick(View view, int position);
    }
}

class MyData{
    public String text;
    public int img;
    public MyData(String text, int img){
        this.text = text;
        this.img = img;
    }
}

4 个答案:

答案 0 :(得分:1)

还有一种方法可以做到

 public ViewHolder(final View view) {
        super(view);
        mImageView = (ImageView)view.findViewById(R.id.image);
        mTextView = (TextView)view.findViewById(R.id.textview);
        cv = (CardView)view.findViewById(R.id.cardview);

        cv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mClickListener != null) mClickListener.onItemClick( view,getAdapterPosition());
            }
        });
}

答案 1 :(得分:1)

public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
    // each data item is just a string in this case
    public ImageView mImageView;
    public TextView mTextView;
    public CardView cv;

    public ViewHolder(View view) {
        super(view);
        mImageView = (ImageView)view.findViewById(R.id.image);
        mTextView = (TextView)view.findViewById(R.id.textview);
        cv = (CardView)view.findViewById(R.id.cardview);

      //for whole cardview (If you want to click only card)
      view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //Do the action.
            }
        });

    //if you want to click the only particular item in the CardView
    mImageView .setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               //Do the action.
            }
        });

    }
}

尝试此代码段,让我知道。如果无法解决,我可以为您解决。

答案 2 :(得分:1)

问题在于您的interfaceadapter class的一部分,而您的onclicklistenerholder class的一部分。您需要从适配器clicklistener调用onBindViewHolder。为此,您必须将id赋予itemholder layout file的根布局,并在ID中查找ID itemholder constructor。之后,您可以按以下方式致电clicklistener。

 @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {
        // - get element from your dataset at this position
        // - replace the contents of the view with that element
        Log.d("position", position+"");

        holder.mTextView.setText(mDataset.get(position).text);
        holder.mImageView.setImageResource(mDataset.get(position).img);
        holder.view.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            mClickListener.onItemClick(v, position)
        }
    });
    }

答案 3 :(得分:1)

只需在 onBindViewHolder 中添加以下行即可 您已经添加了此

  

cv =(CardView)view.findViewById(R.id.cardview);

行以使其正常工作或确保您在xml id中给CardView命名为cardview

cv = (CardView)view.findViewById(R.id.cardview);

holder.cv.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            mClickListener.onItemClick(v, position)
        }
    });