如何在Cardview上向卡片添加刷卡删除

时间:2019-01-04 16:00:01

标签: android android-recyclerview swipe android-cardview

我有这个工作的应用程序,它可以从api解析json并将其显​​示在recyclerview的卡上。我想添加滑动动作以删除卡。我将如何以及在何处添加该动作?我还将下面的代码部分发布。我检查了有关该主题的一些答案,但找不到将它们放在哪里...

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);

    }
}
}

谢谢...

1 个答案:

答案 0 :(得分:0)

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

public static abstract class ItemTouchHelper.SimpleCallback 
extends ItemTouchHelper.Callback 

默认回调的简单包装,您可以使用拖动和滑动方向构造该回调,此类将处理标志回调。根据您的用例,您仍然应该覆盖onMove或onSwiped。

示例:

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);