【发布时间】:2019-01-04 16:00:01
【问题描述】:
我有这个工作应用程序,它从 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);
}
}
}
谢谢...
【问题讨论】:
-
答案非常接近:stackoverflow.com/a/40374244/7352988 祝你好运!=)
-
感谢您的快速回复。我检查了这些,但我有点新,我无法找到将这些代码放在哪里。你们也可以帮我吗?再次感谢..
标签: android android-recyclerview swipe android-cardview