Home  >  Article  >  Java  >  How to Remove Items from RecyclerView Using a Cross Button?

How to Remove Items from RecyclerView Using a Cross Button?

Barbara Streisand
Barbara StreisandOriginal
2024-10-31 23:29:29528browse

How to Remove Items from RecyclerView Using a Cross Button?

Removing Items from RecyclerView Using Cross Button

In your RecyclerView adapter, you'll need to handle cross button events and remove the corresponding item from the dataset. Here's an enhanced version of your adapter:

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

    private ArrayList<String> mDataset;
    private static Context sContext;

    public MyAdapter(Context context, ArrayList<String> myDataset) {
        mDataset = myDataset;
        sContext = context;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent,int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.my_text_view, parent, false);

        ViewHolder holder = new ViewHolder(v);
        holder.mNameTextView.setOnClickListener(this);
        holder.mCrossButtonImageView.setOnClickListener(this);
        return holder;
    }

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

        holder.mNameTextView.setText(mDataset.get(position));

    }

    @Override
    public int getItemCount() {
        return mDataset.size();
    }

    @Override
    public void onClick(View view) {
        ViewHolder holder = (ViewHolder) view.getTag();
        if (view.getId() == holder.mCrossButtonImageView.getId()) {
            int position = holder.getAdapterPosition();
            mDataset.remove(position);

            notifyItemRemoved(position);

            Toast.makeText(sContext, "Item " + holder.mNameTextView.getText() + " has been removed from list",
                    Toast.LENGTH_SHORT).show();
        }
    }
    
    public static class ViewHolder extends RecyclerView.ViewHolder {
        public TextView mNameTextView;
        public ImageView mCrossButtonImageView;

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

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

This code adds an onClick listener to the cross button, which removes the corresponding item when clicked. Note that getAdapterPosition() should be used instead of getPosition(), as getPosition() is now deprecated.

The above is the detailed content of How to Remove Items from RecyclerView Using a Cross Button?. 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