Swipe to delete in favorite screen

This commit is contained in:
Nezumi
2023-03-02 17:26:43 +07:00
parent d895d7e4da
commit 71d24ef9dd
10 changed files with 375 additions and 124 deletions
@@ -1,20 +1,92 @@
package com.capstone.foodify.Fragment;
import android.graphics.Color;
import android.os.Bundle;
import androidx.core.widget.NestedScrollView;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.ItemTouchHelper;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.capstone.foodify.ItemTouchHelperListener;
import com.capstone.foodify.Model.Food.Food;
import com.capstone.foodify.Model.Food.FoodFavoriteAdapter;
import com.capstone.foodify.R;
import com.capstone.foodify.RecyclerViewItemTouchHelper;
import com.google.android.material.snackbar.Snackbar;
public class FavouriteFragment extends Fragment {
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
public class FavouriteFragment extends Fragment implements ItemTouchHelperListener {
private RecyclerView recyclerView_favorite_food;
private FoodFavoriteAdapter adapter;
private List<Food> listFavoriteFood = new ArrayList<>();
private NestedScrollView listFavoriteFoodView;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_favourite, container, false);
View rootView = inflater.inflate(R.layout.fragment_favourite, container, false);
recyclerView_favorite_food = rootView.findViewById(R.id.recycler_view_favorite_food);
listFavoriteFoodView = rootView.findViewById(R.id.list_favorite_food_view);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
recyclerView_favorite_food.setLayoutManager(linearLayoutManager);
listFavoriteFood = getListFavoriteFood();
adapter = new FoodFavoriteAdapter(getListFavoriteFood());
recyclerView_favorite_food.setAdapter(adapter);
RecyclerView.ItemDecoration itemDecoration = new DividerItemDecoration(requireContext(), DividerItemDecoration.VERTICAL);
recyclerView_favorite_food.addItemDecoration(itemDecoration);
ItemTouchHelper.SimpleCallback simpleCallback = new RecyclerViewItemTouchHelper(0, ItemTouchHelper.LEFT, this);
new ItemTouchHelper(simpleCallback).attachToRecyclerView(recyclerView_favorite_food);
return rootView;
}
private List<Food> getListFavoriteFood() {
List<Food> listFoodTemp = new ArrayList<>();
for(int i = 1; i <= 20; i++){
listFoodTemp.add(new Food(String.valueOf(i), "https://cdn.netspace.edu.vn/images/2022/09/29/hoc-nau-mi-quang-1-1024.jpg", "Food "+ i, "25000", "Shop " + i));
}
return listFoodTemp;
}
@Override
public void onSwiped(RecyclerView.ViewHolder viewHolder) {
if(viewHolder instanceof FoodFavoriteAdapter.FoodFavoriteViewHolder){
String foodNameDelete = listFavoriteFood.get(viewHolder.getAdapterPosition()).getName();
Food foodDelete = listFavoriteFood.get(viewHolder.getAdapterPosition());
int indexDelete = viewHolder.getAdapterPosition();
//Remove Item
adapter.removeItem(indexDelete);
//Show notification food has been delete
Snackbar snackbar = Snackbar.make(listFavoriteFoodView, foodNameDelete + "remove!", Snackbar.LENGTH_LONG);
snackbar.setAction("Undo", new View.OnClickListener() {
@Override
public void onClick(View view) {
adapter.undoItem(foodDelete, indexDelete);
}
});
snackbar.setActionTextColor(Color.YELLOW);
snackbar.show();
}
}
}
@@ -0,0 +1,8 @@
package com.capstone.foodify;
import androidx.recyclerview.widget.RecyclerView;
public interface ItemTouchHelperListener {
void onSwiped(RecyclerView.ViewHolder viewHolder);
}
@@ -8,6 +8,7 @@ public class Food {
private String discount;
private String description;
private String shopName;
private String reviewCount;
public String getReviewCount() {
@@ -21,6 +22,14 @@ public class Food {
public Food() {
}
public Food(String id, String img, String name, String price, String shopName) {
this.id = id;
this.img = img;
this.name = name;
this.price = price;
this.shopName = shopName;
}
public String getId() {
return id;
}
@@ -68,4 +77,11 @@ public class Food {
public void setDescription(String description) {
this.description = description;
}
public String getShopName() {
return shopName;
}
public void setShopName(String shopName) {
this.shopName = shopName;
}
}
@@ -0,0 +1,81 @@
package com.capstone.foodify.Model.Food;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.cardview.widget.CardView;
import androidx.recyclerview.widget.RecyclerView;
import com.capstone.foodify.R;
import com.squareup.picasso.Picasso;
import java.util.List;
public class FoodFavoriteAdapter extends RecyclerView.Adapter<FoodFavoriteAdapter.FoodFavoriteViewHolder> {
private List<Food> listFavoriteFood;
public FoodFavoriteAdapter(List<Food> listFavoriteFood) {
this.listFavoriteFood = listFavoriteFood;
}
@NonNull
@Override
public FoodFavoriteViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_food_favorite, parent, false);
return new FoodFavoriteViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull FoodFavoriteViewHolder holder, int position) {
Food food = listFavoriteFood.get(position);
if(food == null)
return;
holder.foodName.setText(food.getName());
holder.shopName.setText(food.getShopName());
holder.price.setText(food.getPrice());
Picasso.get().load(food.getImg()).into(holder.imageView);
}
@Override
public int getItemCount() {
if(listFavoriteFood != null)
return listFavoriteFood.size();
return 0;
}
public static class FoodFavoriteViewHolder extends RecyclerView.ViewHolder{
TextView foodName, shopName, price;
public LinearLayout layoutForeGround;
ImageView imageView;
public FoodFavoriteViewHolder(@NonNull View itemView) {
super(itemView);
foodName = itemView.findViewById(R.id.food_name_text_view);
shopName = itemView.findViewById(R.id.shop_name_text_view);
price = itemView.findViewById(R.id.price_text_view);
imageView = itemView.findViewById(R.id.image_view);
layoutForeGround = itemView.findViewById(R.id.layout_foreground);
}
}
public void removeItem(int index){
listFavoriteFood.remove(index);
notifyItemRemoved(index);
}
public void undoItem(Food food, int index){
listFavoriteFood.add(index, food);
notifyItemInserted(index);
}
}
@@ -0,0 +1,60 @@
package com.capstone.foodify;
import android.graphics.Canvas;
import android.view.View;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.ItemTouchHelper;
import androidx.recyclerview.widget.RecyclerView;
import com.capstone.foodify.Model.Food.FoodFavoriteAdapter;
public class RecyclerViewItemTouchHelper extends ItemTouchHelper.SimpleCallback{
private ItemTouchHelperListener listener;
public RecyclerViewItemTouchHelper(int dragDirs, int swipeDirs, ItemTouchHelperListener listener) {
super(dragDirs, swipeDirs);
this.listener = listener;
}
@Override
public boolean onMove(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, @NonNull RecyclerView.ViewHolder target) {
return true;
}
@Override
public void onSwiped(@NonNull RecyclerView.ViewHolder viewHolder, int direction) {
if(listener != null)
listener.onSwiped(viewHolder);
}
@Override
public void onSelectedChanged(@Nullable RecyclerView.ViewHolder viewHolder, int actionState) {
if(viewHolder != null){
View foreGroundView = ((FoodFavoriteAdapter.FoodFavoriteViewHolder) viewHolder).layoutForeGround;
getDefaultUIUtil().onSelected(foreGroundView);
}
}
@Override
public void onChildDrawOver(@NonNull Canvas c, @NonNull RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive) {
assert viewHolder != null;
View foreGroundView = ((FoodFavoriteAdapter.FoodFavoriteViewHolder) viewHolder).layoutForeGround;
getDefaultUIUtil().onDrawOver(c, recyclerView, foreGroundView, dX, dY, actionState, isCurrentlyActive);
}
@Override
public void onChildDraw(@NonNull Canvas c, @NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive) {
View foreGroundView = ((FoodFavoriteAdapter.FoodFavoriteViewHolder) viewHolder).layoutForeGround;
getDefaultUIUtil().onDraw(c, recyclerView, foreGroundView, dX, dY, actionState, isCurrentlyActive);
}
@Override
public void clearView(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder) {
View foreGroundView = ((FoodFavoriteAdapter.FoodFavoriteViewHolder) viewHolder).layoutForeGround;
getDefaultUIUtil().clearView(foreGroundView);
}
}
@@ -0,0 +1,5 @@
<vector android:height="32dp" android:tint="#FFFFFF"
android:viewportHeight="24" android:viewportWidth="24"
android:width="32dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="@android:color/white" android:pathData="M6,19c0,1.1 0.9,2 2,2h8c1.1,0 2,-0.9 2,-2V7H6v12zM19,4h-3.5l-1,-1h-5l-1,1H5v2h14V4z"/>
</vector>
@@ -4,7 +4,9 @@
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context=".Fragment.FavouriteFragment">
tools:context=".Fragment.FavouriteFragment"
android:id="@+id/list_favorite_food_view"
>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
@@ -26,7 +28,7 @@
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler_view_favorite_food"
tools:listitem="@layout/item_food_horizontal"
tools:listitem="@layout/item_food_favorite"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
@@ -0,0 +1,126 @@
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/red"
android:layout_margin="10dp"
>
<TextView
android:textColor="@color/white"
android:fontFamily="@font/opensans"
android:text="@string/delete"
android:textSize="20sp"
android:textStyle="bold"
android:layout_toStartOf="@id/img_delete"
android:layout_centerVertical="true"
android:layout_marginEnd="5dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<ImageView
android:id="@+id/img_delete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/baseline_delete_24"
android:layout_alignParentEnd="true"
android:layout_centerVertical="true"
android:layout_marginEnd="10dp"
tools:ignore="ContentDescription" />
</RelativeLayout>
<!--Layout foreground-->
<LinearLayout
android:id="@+id/layout_foreground"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:background="@color/white"
android:layout_margin="5dp"
>
<ImageView
android:id="@+id/image_view"
android:layout_width="110dp"
android:layout_height="110dp"
android:src="@drawable/food"
android:scaleType="centerCrop"
app:riv_corner_radius="10dip"
tools:ignore="ContentDescription" />
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_weight="1"
>
<TextView
android:id="@+id/food_name_text_view"
android:text="Food Name"
android:textSize="18sp"
android:textColor="@color/black"
android:fontFamily="@font/opensans"
android:layout_width="wrap_content"
android:layout_height="47dp"
android:layout_marginStart="12dp"
android:layout_marginTop="5dp"
app:layout_constraintStart_toEndOf="@id/image_view"
app:layout_constraintTop_toTopOf="parent"
/>
<TextView
android:id="@+id/shop_name_text_view"
android:text="Shop Name"
android:textSize="16sp"
android:textColor="@color/secondary"
android:fontFamily="@font/opensans"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="12dp"
android:layout_marginTop="2dp"
app:layout_constraintStart_toEndOf="@id/image_view"
app:layout_constraintTop_toBottomOf="@id/text_view_1"
/>
<TextView
android:id="@+id/price_text_view"
android:text="$5.00"
android:textSize="25sp"
android:textColor="@color/primaryColor"
android:fontFamily="@font/bebas"
android:layout_width="220dp"
android:layout_height="wrap_content"
android:layout_marginStart="12dp"
android:layout_marginTop="2dp"
/>
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.1"
>
<ImageView
android:id="@+id/basket_icon"
android:layout_width="20dp"
android:layout_height="match_parent"
android:contentDescription="@string/basket_icon"
app:layout_constraintEnd_toEndOf="parent"
android:src="@drawable/basket_icon"
/>
</LinearLayout>
</LinearLayout>
</FrameLayout>
@@ -1,120 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
>
<androidx.cardview.widget.CardView
android:layout_margin="5dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:cardMaxElevation="10dp"
app:cardPreventCornerOverlap="true"
app:cardElevation="3dp"
app:cardCornerRadius="10dp"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
>
<com.makeramen.roundedimageview.RoundedImageView
android:id="@+id/image_view"
android:layout_width="110dp"
android:layout_height="110dp"
android:src="@drawable/food"
android:scaleType="centerCrop"
app:riv_corner_radius="10dip"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
/>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_weight="1"
>
<TextView
android:id="@+id/text_view_1"
android:text="Food Name"
android:textSize="18sp"
android:textColor="@color/black"
android:fontFamily="@font/opensans"
android:layout_width="wrap_content"
android:layout_height="47dp"
android:layout_marginStart="12dp"
android:layout_marginTop="5dp"
app:layout_constraintStart_toEndOf="@id/image_view"
app:layout_constraintTop_toTopOf="parent"
/>
<TextView
android:id="@+id/text_view_2"
android:text="Shop Name"
android:textSize="16sp"
android:textColor="@color/secondary"
android:fontFamily="@font/opensans"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="12dp"
android:layout_marginTop="2dp"
app:layout_constraintStart_toEndOf="@id/image_view"
app:layout_constraintTop_toBottomOf="@id/text_view_1"
/>
<TextView
android:id="@+id/text_price"
android:text="$5.00"
android:textSize="25sp"
android:textColor="@color/primaryColor"
android:fontFamily="@font/bebas"
android:layout_width="220dp"
android:layout_height="wrap_content"
android:layout_marginStart="12dp"
android:layout_marginTop="2dp"
app:layout_constraintStart_toEndOf="@id/image_view"
app:layout_constraintTop_toBottomOf="@id/text_view_2"
/>
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.1"
>
<ImageView
android:id="@+id/basket_icon"
android:layout_width="20dp"
android:layout_height="match_parent"
android:contentDescription="@string/basket_icon"
app:layout_constraintEnd_toEndOf="parent"
android:src="@drawable/basket_icon"
/>
</LinearLayout>
</LinearLayout>
</androidx.cardview.widget.CardView>
</RelativeLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
+1
View File
@@ -46,4 +46,5 @@
<string name="old_password">Mật khẩu cũ</string>
<string name="change_password">Đổi mật khẩu</string>
<string name="account_and_profile">Thông tin tài khoản</string>
<string name="delete">Delete</string>
</resources>