Ticker

6/recent/ticker-posts

RecyclerView long click to remove an item


 

In this series of recyclerView tutorial, we’re going to see how to use long-click to remove an item from recyclerView.

Before that please visit the previous tutorials on what is RecyclerView and How to implement recyclerView using CardView in android.

There are many ways we can remove an item from recyclerView. In this series, we will discuss every possible way we could delete or remove an item from recyclerView. Today we will see a long-click to remove an item.

Before that we will see some of the other stuff like, how to add ripple effect on an itemView and how to set divider item decoration in recyclerView.


Let’s discuss three of them below

1. First, add the RecyclerView dependency

implementation 'androidx.recyclerview:recyclerview:1.1.0'

 

2. Ripple effect on itemView

We just need to add a line in the layout file of the item which represents the item view for the recyclerView.

android:background="?attr/selectableItemBackground"

The above attribute will give a ripple effect on the item of the recyclerView.

Example:




3. Set Divider item decoration in recyclerView

DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(this,

                                        DividerItemDecoration.VERTICAL);

recyclerView.addItemDecoration(dividerItemDecoration);

Here we set a divider vertically below each item of the recyclerView.


4. Now. Let’s remove the item from recyclerView with long-click

For removing an item from recyclerView with a long click, set the long click listener on the itemView.

itemView.setOnLongClickListener(new View.OnLongClickListener() {

                @Override

                public boolean onLongClick(View view) {

                    moviesList.remove(getAdapterPosition());

                    notifyItemRemoved(getAdapterPosition());

                    return true;

                }

            });

 

notifyItemRemoved(getAdapterPosition()), it removed the item from recyclerView.


Example:



 

You can also, use the below codes and run it into your projects.

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:app="http://schemas.android.com/apk/res-auto"

    xmlns:tools="http://schemas.android.com/tools"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    tools:context=".MainActivity">

 

    <androidx.recyclerview.widget.RecyclerView

        android:id="@+id/recyclerView"

        android:layout_width="match_parent"

        android:layout_height="match_parent"

        app:layout_constraintBottom_toBottomOf="parent"

        app:layout_constraintEnd_toEndOf="parent"

        app:layout_constraintStart_toStartOf="parent"

        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

 

row_item.xml

<?xml version="1.0" encoding="utf-8"?>

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:app="http://schemas.android.com/apk/res-auto"

    xmlns:tools="http://schemas.android.com/tools"

    android:layout_width="match_parent"

    android:layout_height="wrap_content"

    android:padding="5dp"

    android:background="?attr/selectableItemBackground">

 

    <ImageView

        android:id="@+id/imageView"

        android:layout_width="48dp"

        android:layout_height="48dp"

        android:layout_marginStart="16dp"

        android:layout_marginTop="16dp"

        android:layout_marginBottom="16dp"

        app:layout_constraintBottom_toBottomOf="parent"

        app:layout_constraintStart_toStartOf="parent"

        app:layout_constraintTop_toTopOf="parent"

        app:srcCompat="@android:drawable/btn_star_big_on" />

 

    <TextView

        android:id="@+id/textView"

        android:layout_width="0dp"

        android:layout_height="wrap_content"

        android:layout_marginStart="16dp"

        android:fontFamily="@font/poppins_semibold"

        android:text="TextView"

        android:textSize="16sp"

        android:textColor="@android:color/black"

        app:layout_constraintBottom_toTopOf="@+id/rowCount"

        app:layout_constraintEnd_toEndOf="parent"

        app:layout_constraintStart_toEndOf="@+id/imageView"

        app:layout_constraintTop_toTopOf="parent" />

 

    <TextView

        android:id="@+id/rowCount"

        android:layout_width="0dp"

        android:layout_height="wrap_content"

        android:fontFamily="@font/poppins_light"

        android:text="TextView"

        android:textSize="14sp"

 

        app:layout_constraintBottom_toBottomOf="parent"

        app:layout_constraintEnd_toEndOf="parent"

        app:layout_constraintStart_toStartOf="@+id/textView"

        app:layout_constraintTop_toBottomOf="@+id/textView" />

</androidx.constraintlayout.widget.ConstraintLayout>

 

RecyclerViewAdapter.java

package com.codewithgolap.logindesignusingbottomsheet;

 

import android.util.Log;

import android.view.LayoutInflater;

import android.view.View;

import android.view.ViewGroup;

import android.widget.ImageView;

import android.widget.TextView;

import android.widget.Toast;

 

import androidx.annotation.NonNull;

import androidx.recyclerview.widget.RecyclerView;

 

import java.util.List;

 

public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.ViewHolder>{

 

    private static final String TAG = "RecyclerAdapter";

    List<String> moviesList;

 

    public RecyclerAdapter(List<String> moviesList) {

        this.moviesList = moviesList;

    }

 

    @NonNull

    @Override

    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

 

        LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());

        View view = layoutInflater.inflate(R.layout.row_item, parent, false);

        ViewHolder viewHolder = new ViewHolder(view);

        return viewHolder;

 

    }

 

    @Override

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

        holder.rowCountTextView.setText(String.valueOf(position));

        holder.textView.setText(moviesList.get(position));

    }

 

    @Override

    public int getItemCount() {

        return moviesList.size();

    }

 

    class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{

 

        ImageView imageView;

        TextView textView, rowCountTextView;

 

        public ViewHolder(@NonNull View itemView) {

            super(itemView);

 

            imageView = itemView.findViewById(R.id.imageView);

            textView = itemView.findViewById(R.id.textView);

            rowCountTextView = itemView.findViewById(R.id.rowCount);

 

            itemView.setOnClickListener(this);

 

            itemView.setOnLongClickListener(new View.OnLongClickListener() {

                @Override

                public boolean onLongClick(View view) {

                    moviesList.remove(getAdapterPosition());

                    notifyItemRemoved(getAdapterPosition());

                    return true;

                }

            });

        }

 

        @Override

        public void onClick(View view) {

            Toast.makeText(view.getContext(), moviesList.get(getAdapterPosition()), Toast.LENGTH_SHORT).show();

        }

    }

 

}

 

MainActivty.java

package com.codewithgolap.logindesignusingbottomsheet;

 

import androidx.appcompat.app.AppCompatActivity;

import androidx.recyclerview.widget.DividerItemDecoration;

import androidx.recyclerview.widget.LinearLayoutManager;

import androidx.recyclerview.widget.RecyclerView;

 

import android.os.Bundle;

 

import java.util.ArrayList;

import java.util.List;

 

public class MainActivity extends AppCompatActivity {

 

    RecyclerView recyclerView;

    RecyclerAdapter recyclerAdapter;

 

    List<String> moviesList;

 

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

 

        moviesList = new ArrayList<>();

 

        recyclerView = findViewById(R.id.recyclerView);

        recyclerAdapter = new RecyclerAdapter(moviesList);

 

        recyclerView.setLayoutManager(new LinearLayoutManager(this));

        recyclerView.setAdapter(recyclerAdapter);

 

        DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(this,

                DividerItemDecoration.VERTICAL);

        recyclerView.addItemDecoration(dividerItemDecoration);

 

        moviesList.add("Captain America");

        moviesList.add("The Fantastic Four");

        moviesList.add("Howard the Duck");

        moviesList.add("Blade");

        moviesList.add("X-Men");

        moviesList.add("Spider-Man");

        moviesList.add("Daredevil");

        moviesList.add("Hulk");

        moviesList.add("The Punisher");

        moviesList.add("Ghost Rider");

        moviesList.add("Iron Man");

        moviesList.add("Thor");

        moviesList.add("The Avengers");

        moviesList.add("Guardians of the Galaxy");

        moviesList.add("Ant-Man");

        moviesList.add("Deadpool");

        moviesList.add("Doctor Strange");

        moviesList.add("Logan");

        moviesList.add("Black Panther");

        moviesList.add("Venom");

        moviesList.add("Dark Phoenix");

        moviesList.add("The New Mutants");

 

    }

}



 

 

Post a Comment

0 Comments