Вам необходимо использовать ItemTouchHelper
- Это служебный класс для добавления свайпа в
dismiss
и поддержки перетаскивания в RecyclerView
. - Он работает с
RecyclerView
и классом обратного вызова, который определяет, какой тип взаимодействия разрешен, а также получает события, когда пользователь выполняет эти действия.
ЗдесьПример кода как использовать ItemTouchHelper
с RecyclerView
StackActivity
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.helper.ItemTouchHelper;
import java.util.ArrayList;
public class StackActivity extends AppCompatActivity {
RecyclerView myRecyclerView;
private ArrayList<ItemModel> arrayList = new ArrayList<>();
FavAdapter favAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_stack);
myRecyclerView = findViewById(R.id.myRecyclerView);
myRecyclerView.setLayoutManager(new LinearLayoutManager(this));
myRecyclerView.setHasFixedSize(true);
// here i'm adding dummy data inside list
addDataInList();
// setting adapter to RecyclerView
favAdapter = new FavAdapter(this, arrayList);
myRecyclerView.setAdapter(favAdapter);
new ItemTouchHelper(new ItemTouchHelper.SimpleCallback(0, ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT) {
@Override
public boolean onMove(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, @NonNull RecyclerView.ViewHolder target) {
return false;
}
@Override
public void onSwiped(@NonNull RecyclerView.ViewHolder viewHolder, int direction) {
// when user swipe thr recyclerview item to right remove item from avorite list
if (direction == ItemTouchHelper.RIGHT) {
favAdapter.addToFav(viewHolder.getAdapterPosition(), false);
}
// when user swipe thr recyclerview item to left remove item from avorite list
else if (direction == ItemTouchHelper.LEFT) {
favAdapter.addToFav(viewHolder.getAdapterPosition(), true);
}
}
}).attachToRecyclerView(myRecyclerView);
}
//method to add dummy data inside ourlist
private void addDataInList() {
arrayList.add(new ItemModel("Item 1", "https://i.stack.imgur.com/1dWdI.jpg", false));
arrayList.add(new ItemModel("Item 2", "https://i.stack.imgur.com/1dWdI.jpg", false));
arrayList.add(new ItemModel("Item 3", "https://i.stack.imgur.com/1dWdI.jpg", false));
arrayList.add(new ItemModel("Item 4", "https://i.stack.imgur.com/1dWdI.jpg", false));
arrayList.add(new ItemModel("Item 5", "https://i.stack.imgur.com/1dWdI.jpg", false));
arrayList.add(new ItemModel("Item 6", "https://i.stack.imgur.com/1dWdI.jpg", false));
arrayList.add(new ItemModel("Item 7", "https://i.stack.imgur.com/1dWdI.jpg", false));
arrayList.add(new ItemModel("Item 8", "https://i.stack.imgur.com/1dWdI.jpg", false));
arrayList.add(new ItemModel("Item 9", "https://i.stack.imgur.com/1dWdI.jpg", false));
arrayList.add(new ItemModel("Item 10", "https://i.stack.imgur.com/1dWdI.jpg", false));
arrayList.add(new ItemModel("Item 11", "https://i.stack.imgur.com/1dWdI.jpg", false));
arrayList.add(new ItemModel("Item 12", "https://i.stack.imgur.com/1dWdI.jpg", false));
arrayList.add(new ItemModel("Item 13", "https://i.stack.imgur.com/1dWdI.jpg", false));
arrayList.add(new ItemModel("Item 14", "https://i.stack.imgur.com/1dWdI.jpg", false));
arrayList.add(new ItemModel("Item 15", "https://i.stack.imgur.com/1dWdI.jpg", false));
arrayList.add(new ItemModel("Item 16", "https://i.stack.imgur.com/1dWdI.jpg", false));
arrayList.add(new ItemModel("Item 17", "https://i.stack.imgur.com/1dWdI.jpg", false));
arrayList.add(new ItemModel("Item 18", "https://i.stack.imgur.com/1dWdI.jpg", false));
arrayList.add(new ItemModel("Item 19", "https://i.stack.imgur.com/1dWdI.jpg", false));
arrayList.add(new ItemModel("Item 20", "https://i.stack.imgur.com/1dWdI.jpg", false));
}
}
макет стека активности
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical">
<android.support.v7.widget.RecyclerView
android:id="@+id/myRecyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
класс FavAdapter
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import java.util.ArrayList;
public class FavAdapter extends RecyclerView.Adapter<FavAdapter.ViewHolder> {
private Context context;
private ArrayList<ItemModel> arrayList = new ArrayList<>();
public FavAdapter(Context context, ArrayList<ItemModel> arrayList) {
this.context = context;
this.arrayList = arrayList;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.custom_fav_layout, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
// here i'm check that if item is already added in favorite or not
//based on boolean flag i'm managed to set weather the item is in favorite or not
// this flag is also use full to keep state of out favorite when we scroll our recyclerview
holder.ivFavImage.setImageResource(arrayList.get(position).isFavorite()
? R.drawable.ic_favorite : R.drawable.ic_fav_white);
holder.tvProductName.setText(arrayList.get(position).getItemName());
Glide.with(context)
.load(arrayList.get(position).getImageUrl())
.apply(new RequestOptions().
placeholder(R.drawable.ic_placeholder)
.error(R.drawable.ic_error))
.into(holder.ivProductImage);
}
// this method is used to add or remove item from favorite list when use swipe the recyclerview item using ItemTouchHelper
public void addToFav(int position, boolean flag) {
arrayList.get(position).setFavorite(flag);
notifyDataSetChanged();
}
@Override
public int getItemCount() {
return arrayList.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
ImageView ivProductImage, ivFavImage;
TextView tvProductName;
public ViewHolder(View itemView) {
super(itemView);
ivProductImage = itemView.findViewById(R.id.ivProductImage);
ivFavImage = itemView.findViewById(R.id.ivFavImage);
tvProductName = itemView.findViewById(R.id.tvProductName);
ivFavImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (arrayList.get(getAdapterPosition()).isFavorite()) {
arrayList.get(getAdapterPosition()).setFavorite(false);
} else {
arrayList.get(getAdapterPosition()).setFavorite(true);
}
notifyDataSetChanged();
}
});
}
}
}
макет custom_fav_layout
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardElevation="5dp"
app:cardUseCompatPadding="true">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/ivProductImage"
android:layout_width="match_parent"
android:layout_height="150dp"
android:scaleType="fitXY"
android:adjustViewBounds="true"
android:src="@color/colorNavBar" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal">
<TextView
android:id="@+id/tvProductName"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/app_name"
android:paddingStart="5dp"
android:textColor="#FFFFFF"
android:textStyle="bold" />
<ImageView
android:id="@+id/ivFavImage"
android:layout_width="50dp"
android:layout_height="50dp"
android:src="@drawable/ic_favorite" />
</LinearLayout>
</RelativeLayout>
</android.support.v7.widget.CardView>
ItemModel damodel class
public class ItemModel {
boolean isFavorite;
String ItemName,imageUrl;
public ItemModel( String itemName, String imageUrl,boolean isFavorite) {
this.isFavorite = isFavorite;
ItemName = itemName;
this.imageUrl = imageUrl;
}
public boolean isFavorite() {
return isFavorite;
}
public void setFavorite(boolean favorite) {
isFavorite = favorite;
}
public String getItemName() {
return ItemName;
}
public void setItemName(String itemName) {
ItemName = itemName;
}
public String getImageUrl() {
return imageUrl;
}
public void setImageUrl(String imageUrl) {
this.imageUrl = imageUrl;
}
}
R.drawable.ic_favorite
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24.0"
android:viewportHeight="24.0">
<path
android:fillColor="#FF00"
android:pathData="M12,21.35l-1.45,-1.32C5.4,15.36 2,12.28 2,8.5 2,5.42 4.42,3 7.5,3c1.74,0 3.41,0.81 4.5,2.09C13.09,3.81 14.76,3 16.5,3 19.58,3 22,5.42 22,8.5c0,3.78 -3.4,6.86 -8.55,11.54L12,21.35z"/>
</vector>
drawable.ic_fav_white
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24.0"
android:viewportHeight="24.0">
<path
android:fillColor="#FFFFFF"
android:pathData="M12,21.35l-1.45,-1.32C5.4,15.36 2,12.28 2,8.5 2,5.42 4.42,3 7.5,3c1.74,0 3.41,0.81 4.5,2.09C13.09,3.81 14.76,3 16.5,3 19.58,3 22,5.42 22,8.5c0,3.78 -3.4,6.86 -8.55,11.54L12,21.35z"/>
</vector>
ВЫХОД вышеуказанного кода образца
https://www.youtube.com/watch?v=GKD-SDPWD3k&feature=youtu.be
Для получения информации вы можете проверить эту статью ниже
И если вы хотите пойти слюбая библиотека, чем проверить это