我们已经看到许多应用程序中都包含RecyclerView,并且与此同时,我们还看到了RecyclerView中的许多功能,可通过滑动删除这些功能以及更多其他功能。我们已经在Gmail应用程序中看到了这种类型的功能,可以在其中滑动或向右或向左滑动项目以删除或添加到存档中。在本文中,我们将研究其中具有撤消功能的“滑动以删除RecyclerView”项目的实现。
我们将在本文中构建什么?
我们将构建一个简单的应用程序,在其中将显示一个简单的RecyclerView,其中显示课程列表及其说明,并且将添加用于删除和撤消功能的功能。下面给出了一个示例GIF,以了解我们将在本文中做些什么。注意,我们将使用Java语言实现该项目。
分步实施
步骤1:创建一个新项目
要在Android Studio中创建新项目,请参阅如何在Android Studio中创建/启动新项目。请注意,选择Java作为编程语言。
要实现Recycler View,需要三个子部分,它们有助于控制RecyclerView。这三个子部分包括:
- 卡片布局:卡片布局是一个XML文件,它将代表“回收者”视图内的每个单独的网格项目。
- View Holder :View Holder类是一种Java类,用于在Card Layout中存储对UI元素的引用,并且可以在程序执行期间通过数据列表动态修改它们。
- 数据类:数据类是一个对象类,其中包含要在“回收者视图”中显示的每个“回收者”视图项中显示的信息。
步骤2:为RecyclerView卡项目创建卡布局
转到应用程序> res>布局>右键单击>新建>布局资源文件,并将文件命名为card_layout 。在此文件中,写入了与RecyclerView中的卡片项目有关的所有XML代码。以下是card_layout.xml文件的代码。
XML
Java
public class RecyclerData {
// string for displaying
// title and description.
private String title;
private String description;
// constructor for our title and description.
public RecyclerData(String title, String description) {
this.title = title;
this.description = description;
}
// creating getter and setter methods.
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}
Java
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;
public class RecyclerViewAdapter extends RecyclerView.Adapter {
// creating a variable for our array list and context.
private ArrayList courseDataArrayList;
private Context mcontext;
// creating a constructor class.
public RecyclerViewAdapter(ArrayList recyclerDataArrayList, Context mcontext) {
this.courseDataArrayList = recyclerDataArrayList;
this.mcontext = mcontext;
}
@NonNull
@Override
public RecyclerViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
// Inflate Layout
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.card_layout, parent, false);
return new RecyclerViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull RecyclerViewHolder holder, int position) {
// Set the data to textview from our modal class.
RecyclerData recyclerData = courseDataArrayList.get(position);
holder.courseNameTV.setText(recyclerData.getTitle());
holder.courseDescTV.setText(recyclerData.getDescription());
}
@Override
public int getItemCount() {
// this method returns
// the size of recyclerview
return courseDataArrayList.size();
}
// View Holder Class to handle Recycler View.
public class RecyclerViewHolder extends RecyclerView.ViewHolder {
// creating a variable for our text view.
private TextView courseNameTV;
private TextView courseDescTV;
public RecyclerViewHolder(@NonNull View itemView) {
super(itemView);
// initializing our text views.
courseNameTV = itemView.findViewById(R.id.idTVCourseName);
courseDescTV = itemView.findViewById(R.id.idTVCourseDesc);
}
}
}
XML
Java
import android.os.Bundle;
import android.view.View;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.ItemTouchHelper;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.google.android.material.snackbar.Snackbar;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
// creating a variable for recycler view,
// array list and adapter class.
private RecyclerView courseRV;
private ArrayList recyclerDataArrayList;
private RecyclerViewAdapter recyclerViewAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// initializing our variables.
courseRV = findViewById(R.id.idRVCourse);
// creating new array list.
recyclerDataArrayList = new ArrayList<>();
// in below line we are adding data to our array list.
recyclerDataArrayList.add(new RecyclerData("DSA Course", "DSA Self Paced Course"));
recyclerDataArrayList.add(new RecyclerData("C++ Course", "C++ Self Paced Course"));
recyclerDataArrayList.add(new RecyclerData("Java Course", "Java Self Paced Course"));
recyclerDataArrayList.add(new RecyclerData("Python Course", "Python Slef Paced Course"));
recyclerDataArrayList.add(new RecyclerData("Fork CPP", "Fork CPP Self Paced Course"));
recyclerDataArrayList.add(new RecyclerData("Amazon SDE", "Amazon SDE Test Questions"));
// initializing our adapter class with our array list and context.
recyclerViewAdapter = new RecyclerViewAdapter(recyclerDataArrayList, this);
// below line is to set layout manager for our recycler view.
LinearLayoutManager manager = new LinearLayoutManager(this);
// setting layout manager for our recycler view.
courseRV.setLayoutManager(manager);
// below line is to set adapter
// to our recycler view.
courseRV.setAdapter(recyclerViewAdapter);
// on below line we are creating a method to create item touch helper
// method for adding swipe to delete functionality.
// in this we are specifying drag direction and position to right
new ItemTouchHelper(new ItemTouchHelper.SimpleCallback(0, ItemTouchHelper.RIGHT) {
@Override
public boolean onMove(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, @NonNull RecyclerView.ViewHolder target) {
// this method is called
// when the item is moved.
return false;
}
@Override
public void onSwiped(@NonNull RecyclerView.ViewHolder viewHolder, int direction) {
// this method is called when we swipe our item to right direction.
// on below line we are getting the item at a particular position.
RecyclerData deletedCourse = recyclerDataArrayList.get(viewHolder.getAdapterPosition());
// below line is to get the position
// of the item at that position.
int position = viewHolder.getAdapterPosition();
// this method is called when item is swiped.
// below line is to remove item from our array list.
recyclerDataArrayList.remove(viewHolder.getAdapterPosition());
// below line is to notify our item is removed from adapter.
recyclerViewAdapter.notifyItemRemoved(viewHolder.getAdapterPosition());
// below line is to display our snackbar with action.
Snackbar.make(courseRV, deletedCourse.getTitle(), Snackbar.LENGTH_LONG).setAction("Undo", new View.OnClickListener() {
@Override
public void onClick(View v) {
// adding on click listener to our action of snack bar.
// below line is to add our item to array list with a position.
recyclerDataArrayList.add(position, deletedCourse);
// below line is to notify item is
// added to our adapter class.
recyclerViewAdapter.notifyItemInserted(position);
}
}).show();
}
// at last we are adding this
// to our recycler view.
}).attachToRecyclerView(courseRV);
}
}
步骤3:为模态数据创建Java类
转到应用程序> Java >右键单击应用程序的包名称>新建> Java类,并将文件命名为RecyclerData 。此类将处理要显示的每个“回收者”项目的数据。以下是RecyclerData的代码。 Java文件。
Java
public class RecyclerData {
// string for displaying
// title and description.
private String title;
private String description;
// constructor for our title and description.
public RecyclerData(String title, String description) {
this.title = title;
this.description = description;
}
// creating getter and setter methods.
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}
步骤4:为适配器创建一个新的Java类
同样,创建一个新的Java类并将文件命名为RecyclerViewAdapter。适配器是负责RecyclerView的主要类。它包含在RecyclerView中有用的所有方法。
Note: View Holder Class is also implemented in Adapter Class itself.
这些处理“回收者视图”的方法包括:
- onCreateViewHolder :此方法为Recycler View扩充卡片布局项目。
- onBindViewHolder :此方法将数据设置为卡片项目的特定视图。它还处理与单击“回收者”视图中的项目有关的方法。
- getItemCount :此方法返回RecyclerView的长度。
下面是RecyclerViewAdapter的代码。 Java文件。在代码内部添加了注释,以更详细地了解代码。
Java
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;
public class RecyclerViewAdapter extends RecyclerView.Adapter {
// creating a variable for our array list and context.
private ArrayList courseDataArrayList;
private Context mcontext;
// creating a constructor class.
public RecyclerViewAdapter(ArrayList recyclerDataArrayList, Context mcontext) {
this.courseDataArrayList = recyclerDataArrayList;
this.mcontext = mcontext;
}
@NonNull
@Override
public RecyclerViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
// Inflate Layout
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.card_layout, parent, false);
return new RecyclerViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull RecyclerViewHolder holder, int position) {
// Set the data to textview from our modal class.
RecyclerData recyclerData = courseDataArrayList.get(position);
holder.courseNameTV.setText(recyclerData.getTitle());
holder.courseDescTV.setText(recyclerData.getDescription());
}
@Override
public int getItemCount() {
// this method returns
// the size of recyclerview
return courseDataArrayList.size();
}
// View Holder Class to handle Recycler View.
public class RecyclerViewHolder extends RecyclerView.ViewHolder {
// creating a variable for our text view.
private TextView courseNameTV;
private TextView courseDescTV;
public RecyclerViewHolder(@NonNull View itemView) {
super(itemView);
// initializing our text views.
courseNameTV = itemView.findViewById(R.id.idTVCourseName);
courseDescTV = itemView.findViewById(R.id.idTVCourseDesc);
}
}
}
步骤5:使用activity_main.xml文件
导航到应用程序> res>布局> activity_main.xml,然后将以下代码添加到该文件中。以下是activity_main.xml文件的代码。
XML格式
步骤6:使用MainActivity。 Java文件
转到MainActivity。 Java文件并参考以下代码。下面是MainActivity的代码。 Java文件。在代码内部添加了注释,以更详细地了解代码。
Java
import android.os.Bundle;
import android.view.View;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.ItemTouchHelper;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.google.android.material.snackbar.Snackbar;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
// creating a variable for recycler view,
// array list and adapter class.
private RecyclerView courseRV;
private ArrayList recyclerDataArrayList;
private RecyclerViewAdapter recyclerViewAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// initializing our variables.
courseRV = findViewById(R.id.idRVCourse);
// creating new array list.
recyclerDataArrayList = new ArrayList<>();
// in below line we are adding data to our array list.
recyclerDataArrayList.add(new RecyclerData("DSA Course", "DSA Self Paced Course"));
recyclerDataArrayList.add(new RecyclerData("C++ Course", "C++ Self Paced Course"));
recyclerDataArrayList.add(new RecyclerData("Java Course", "Java Self Paced Course"));
recyclerDataArrayList.add(new RecyclerData("Python Course", "Python Slef Paced Course"));
recyclerDataArrayList.add(new RecyclerData("Fork CPP", "Fork CPP Self Paced Course"));
recyclerDataArrayList.add(new RecyclerData("Amazon SDE", "Amazon SDE Test Questions"));
// initializing our adapter class with our array list and context.
recyclerViewAdapter = new RecyclerViewAdapter(recyclerDataArrayList, this);
// below line is to set layout manager for our recycler view.
LinearLayoutManager manager = new LinearLayoutManager(this);
// setting layout manager for our recycler view.
courseRV.setLayoutManager(manager);
// below line is to set adapter
// to our recycler view.
courseRV.setAdapter(recyclerViewAdapter);
// on below line we are creating a method to create item touch helper
// method for adding swipe to delete functionality.
// in this we are specifying drag direction and position to right
new ItemTouchHelper(new ItemTouchHelper.SimpleCallback(0, ItemTouchHelper.RIGHT) {
@Override
public boolean onMove(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, @NonNull RecyclerView.ViewHolder target) {
// this method is called
// when the item is moved.
return false;
}
@Override
public void onSwiped(@NonNull RecyclerView.ViewHolder viewHolder, int direction) {
// this method is called when we swipe our item to right direction.
// on below line we are getting the item at a particular position.
RecyclerData deletedCourse = recyclerDataArrayList.get(viewHolder.getAdapterPosition());
// below line is to get the position
// of the item at that position.
int position = viewHolder.getAdapterPosition();
// this method is called when item is swiped.
// below line is to remove item from our array list.
recyclerDataArrayList.remove(viewHolder.getAdapterPosition());
// below line is to notify our item is removed from adapter.
recyclerViewAdapter.notifyItemRemoved(viewHolder.getAdapterPosition());
// below line is to display our snackbar with action.
Snackbar.make(courseRV, deletedCourse.getTitle(), Snackbar.LENGTH_LONG).setAction("Undo", new View.OnClickListener() {
@Override
public void onClick(View v) {
// adding on click listener to our action of snack bar.
// below line is to add our item to array list with a position.
recyclerDataArrayList.add(position, deletedCourse);
// below line is to notify item is
// added to our adapter class.
recyclerViewAdapter.notifyItemInserted(position);
}
}).show();
}
// at last we are adding this
// to our recycler view.
}).attachToRecyclerView(courseRV);
}
}
现在运行您的应用程序,并查看该应用程序的输出。