-2

I have this working app which parse json from api and show them on cards on recyclerview. I want to add swipe action to delete the cards. How and where am I gonna add that action? I also post the part of my codes below. I checked some answers about this topic but couldnt find where I put those...

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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">

<android.support.v7.widget.RecyclerView
    android:id="@+id/recyclerview"
    android:layout_width="match_parent"
    android:layout_height="match_parent"> 
</android.support.v7.widget.RecyclerView>

</android.support.constraint.ConstraintLayout>

item_row.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView 
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_gravity="center"
card_view:cardCornerRadius="12dp">

<LinearLayout
    android:padding="15dp"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <TextView
        android:text="deneme"
        android:gravity="left"
        android:textSize="20sp"
        android:id="@+id/name"
        android:layout_weight="1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <TextView
        android:gravity="right"
        android:textSize="20sp"
        android:id="@+id/city"
        android:layout_weight="1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</LinearLayout>

</android.support.v7.widget.CardView>

RecyclerviewAdapter.java

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

private Context c;
private ArrayList<itemBrewery> list;

public RecyclerviewAdapter(Context c, ArrayList<itemBrewery> list) {
    this.c = c;
    this.list = list;
    notifyDataSetChanged();
}

@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) 
{
    View v = 
LayoutInflater.from(parent.getContext()).inflate(R.layout.item_row, parent, 
false);
    ViewHolder vh = new ViewHolder(v);
    return vh;
}

@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
    holder.city.setText(list.get(position).getCity());
    holder.name.setText(list.get(position).getName());
}

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

public class ViewHolder extends RecyclerView.ViewHolder{
    TextView name;
    TextView city;
    public ViewHolder(@NonNull View itemView) {
        super(itemView);
        name = (TextView) itemView.findViewById(R.id.name);
        city = (TextView) itemView.findViewById(R.id.city);

    }
}
}

Thanks...

Akin
  • 1
  • 3
  • Possible duplicate of [Android - Swipe to delete RecyclerView](https://stackoverflow.com/questions/33985719/android-swipe-to-delete-recyclerview) – TheWanderer Jan 04 '19 at 16:03
  • 1
    The answer is pretty close: https://stackoverflow.com/a/40374244/7352988 good luck!=) – tim_baton Jan 04 '19 at 16:05
  • Thanks for fast reply.. I checked those but I am kinda new and I couldnt find out where to put those codes. Can you guys help me with that too? Thanks again.. – Akin Jan 04 '19 at 16:13

1 Answers1

0

Ref : https://developer.android.com/reference/android/support/v7/widget/helper/ItemTouchHelper.SimpleCallback

public static abstract class ItemTouchHelper.SimpleCallback 
extends ItemTouchHelper.Callback 

A simple wrapper to the default Callback which you can construct with drag and swipe directions and this class will handle the flag callbacks. You should still override onMove or onSwiped depending on your use case.

Example :

ItemTouchHelper.SimpleCallback simpleItemTouchCallback = new ItemTouchHelper.SimpleCallback(0, ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT | ItemTouchHelper.DOWN | ItemTouchHelper.UP) {
        @Override
        public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
           //do something
            return false;
        }

        @Override
        public void onSwiped(RecyclerView.ViewHolder viewHolder, int swipeDir) {      
            int position = viewHolder.getAdapterPosition();
            // code remove swiped item
            //notify the recyclerview changes 

        }
 };

ItemTouchHelper itemTouchHelper = new ItemTouchHelper(simpleItemTouchCallback);
itemTouchHelper.attachToRecyclerView(recyclerView);
Ashvin solanki
  • 4,802
  • 3
  • 25
  • 65
  • Thanks for your reply. It kinda helps but what I'm asking is where should I put these codes? Should I create another class or something? – Akin Jan 05 '19 at 09:23
  • @Akin place this code where u initialized your recyclerview and adapter – Ashvin solanki Jan 05 '19 at 10:57