Home  >  Article  >  Java  >  How to Remove Items from an Android RecyclerView with Cross Buttons?

How to Remove Items from an Android RecyclerView with Cross Buttons?

Patricia Arquette
Patricia ArquetteOriginal
2024-11-03 00:51:03132browse

How to Remove Items from an Android RecyclerView with Cross Buttons?

Android RecyclerView Item Addition and Removal

Problem

This RecyclerView implementation includes a TextView box and a cross button (ImageView). A button outside the RecyclerView is used to toggle the visibility of the cross button. The objetivo is to remove an item from the RecyclerView when the corresponding cross button is clicked.

Solution

Within the ViewHolder class of the adapter:

  1. Implement the OnClickListener interface for the ViewHolder to handle clicks on both the ImageView and the TextView.
  2. Add an OnClick handler for the cross button (ImageView). Within the handler, remove the item from the dataset and update the RecyclerView with notifyDataSetChanged().
  3. In your Adapter class, add a method to remove an item at a specific position, calling notifyItemRemoved() and notifyItemRangeChanged().

Example Adapter Code

<code class="java">public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {

    private ArrayList<String> mDataset;

    public MyAdapter(ArrayList<String> myDataset) {
        mDataset = myDataset;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent,int viewType) {
        // ...

        holder.mNameTextView.setOnClickListener(this);
        holder.crossButton.setOnClickListener(this);

        return holder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {

        // ...

    }

    @Override
    public void onClick(View view) {
        if (view.getId() == holder.crossButton.getId()) {
            removeAt(holder.getAdapterPosition());
        } else if (view.getId() == holder.mNameTextView.getId()) {
            // ...
        }
    }

    public void removeAt(int position) {
        mDataset.remove(position);
        notifyItemRemoved(position);
        notifyItemRangeChanged(position, mDataset.size());
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        TextView mNameTextView;
        ImageView crossButton;

        public ViewHolder(View v) {
            super(v);

            mNameTextView = (TextView) v.findViewById(R.id.nameTextView);
            crossButton = (ImageView) v.findViewById(R.id.crossButton);
        }
    }
}</code>

The above is the detailed content of How to Remove Items from an Android RecyclerView with Cross Buttons?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn