SnapHelper是一个出色的功能,可以在RecyclerView中看到。借助此功能,我们可以使RecyclerView的项目正确可见。大多数应用程序都具有Recycler View的此功能,但是它不可见。此功能通常在Google Play应用程序中看到,当我们滚动水平的回收站视图时,将只能看到可见的特定数量的回收站视图项目。当我们使用普通的回收站视图时,我们可以在滚动查看时看到一些项目出现在设备屏幕中的一半。因此,要在Android Recycler View中解决此问题。我们必须在“ Recycler View”中使用“ Snap Helper”。在本文中,我们将看一下在我们的应用程序中实现此RecyclerView的过程。
什么是SnapHelper?
SnapHelper是一个帮助程序类,用于捕捉我们RecyclerView的任何子级。借助此类,我们可以在屏幕上显示特定数量的RecyclerView项,并且可以避免RecyclerView子级显示在RecyclerView中。通常在Google Play商店中使用的RecyclerView中可以看到此功能。
我们将在本文中构建什么?
我们将构建一个简单的应用程序,在其中将在RecyclerView中显示课程列表。在此RecyclerView中,我们将实现SnapHelper类功能,以避免用户设备中部分存在的元素。下面是视频,我们将在其中观看我们将要构建的内容。注意,我们将使用Java语言实现该项目。
分步实施
步骤1:创建一个新项目
要在Android Studio中创建新项目,请参阅如何在Android Studio中创建/启动新项目。请注意,选择Java作为编程语言。
步骤2:使用activity_main.xml文件
导航到应用程序> res>布局> activity_main.xml,然后将以下代码添加到该文件中。以下是activity_main.xml文件的代码。
XML
Java
public class CourseModal {
// variables for our course name, description and duration
private String courseName;
private String courseDescription;
private String courseDuration;
// constructor class.
public CourseModal(String courseName, String courseDescription, String courseDuration) {
this.courseName = courseName;
this.courseDescription = courseDescription;
this.courseDuration = courseDuration;
}
// getter and setter methods.
public String getCourseName() {
return courseName;
}
public void setCourseName(String courseName) {
this.courseName = courseName;
}
public String getCourseDescription() {
return courseDescription;
}
public void setCourseDescription(String courseDescription) {
this.courseDescription = courseDescription;
}
public String getCourseDuration() {
return courseDuration;
}
public void setCourseDuration(String courseDuration) {
this.courseDuration = courseDuration;
}
}
XML
Java
import android.content.Context;
import android.content.Intent;
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 CourseRVAdapter extends RecyclerView.Adapter {
private Context context;
private ArrayList courseModalArrayList;
// creating a constructor class.
public CourseRVAdapter(Context context, ArrayList courseModalArrayList) {
this.context = context;
this.courseModalArrayList = courseModalArrayList;
}
@NonNull
@Override
public CourseRVAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
// passing our layout file for displaying our card item
return new ViewHolder(LayoutInflater.from(context).inflate(R.layout.course_rv_item, parent, false));
}
@Override
public void onBindViewHolder(@NonNull CourseRVAdapter.ViewHolder holder, int position) {
// setting data to our text views from our modal class.
CourseModal courses = courseModalArrayList.get(position);
holder.courseNameTV.setText(courses.getCourseName());
holder.courseDurationTV.setText(courses.getCourseDuration());
holder.courseDescTV.setText(courses.getCourseDescription());
}
@Override
public int getItemCount() {
return courseModalArrayList.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
// creating variables for our text views.
private final TextView courseNameTV;
private final TextView courseDurationTV;
private final TextView courseDescTV;
public ViewHolder(@NonNull View itemView) {
super(itemView);
// initializing our text views.
courseNameTV = itemView.findViewById(R.id.idTVCourseName);
courseDurationTV = itemView.findViewById(R.id.idTVCourseDuration);
courseDescTV = itemView.findViewById(R.id.idTVCourseDescription);
}
}
}
Java
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.LinearSnapHelper;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.SnapHelper;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
// creating variables for our recycler view, array list and adapter class.
private RecyclerView coursesRV;
private ArrayList courseArrayList;
private CourseRVAdapter courseRVAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// initializing our recycler view and array list.
coursesRV = findViewById(R.id.idRVCourses);
courseArrayList = new ArrayList<>();
// on below line we are adding data to our array list.
courseArrayList.add(new CourseModal("DSA", "DSA Self Paced Course", "90 days"));
courseArrayList.add(new CourseModal("C++", "C++ Self Paced Course", "60 days"));
courseArrayList.add(new CourseModal("Java", "Java Self Paced Course", "60 days"));
courseArrayList.add(new CourseModal("Python", "Python Self Paced Course", "90 days"));
courseArrayList.add(new CourseModal("PHP", "PHP Self Paced Course", "40 days"));
courseArrayList.add(new CourseModal("DSA", "DSA Self Paced Course", "90 days"));
courseArrayList.add(new CourseModal("C++", "C++ Self Paced Course", "60 days"));
courseArrayList.add(new CourseModal("Java", "Java Self Paced Course", "60 days"));
courseArrayList.add(new CourseModal("Python", "Python Self Paced Course", "90 days"));
courseArrayList.add(new CourseModal("PHP", "PHP Self Paced Course", "40 days"));
// calling a method to prepare our recycler view.
prepareCourseRV();
// on below line we are creating a new variable for
// our snap helper class and initializing it for our Linear Snap Helper.
SnapHelper snapHelper = new LinearSnapHelper();
// on below line we are attaching this snap helper to our recycler view.
snapHelper.attachToRecyclerView(coursesRV);
}
private void prepareCourseRV() {
// on below line we are initializing our adapter class.
courseRVAdapter = new CourseRVAdapter(this, courseArrayList);
// on below line we are creating a variable for our linear layout manager.
LinearLayoutManager layoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
// on below line we are setting layout manager to our recycler view.
coursesRV.setLayoutManager(layoutManager);
// on below line we are setting adapter to our recycler view.
coursesRV.setAdapter(courseRVAdapter);
}
}
第3步:创建用于存储数据的模式类
导航到应用程序> Java >应用程序的程序包名称>右键单击它>新建> Java类,并将其命名为CourseModal,然后将以下代码添加到其中。在代码中添加了注释,以便更详细地了解。
Java
public class CourseModal {
// variables for our course name, description and duration
private String courseName;
private String courseDescription;
private String courseDuration;
// constructor class.
public CourseModal(String courseName, String courseDescription, String courseDuration) {
this.courseName = courseName;
this.courseDescription = courseDescription;
this.courseDuration = courseDuration;
}
// getter and setter methods.
public String getCourseName() {
return courseName;
}
public void setCourseName(String courseName) {
this.courseName = courseName;
}
public String getCourseDescription() {
return courseDescription;
}
public void setCourseDescription(String courseDescription) {
this.courseDescription = courseDescription;
}
public String getCourseDuration() {
return courseDuration;
}
public void setCourseDuration(String courseDuration) {
this.courseDuration = courseDuration;
}
}
步骤4:为我们的RecyclerView项目创建一个新的布局文件
导航到应用程序> res>布局>右键单击它>新建>布局资源文件,并将其命名为course_rv_item,并在其中添加以下代码。
XML格式
步骤5:创建适配器类
导航到应用程序> Java >应用程序的程序包名称>右键单击它>新建> Java类,并将其命名为CourseRVAdapter,然后将以下代码添加到其中。在代码中添加了注释,以便更详细地了解。
Java
import android.content.Context;
import android.content.Intent;
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 CourseRVAdapter extends RecyclerView.Adapter {
private Context context;
private ArrayList courseModalArrayList;
// creating a constructor class.
public CourseRVAdapter(Context context, ArrayList courseModalArrayList) {
this.context = context;
this.courseModalArrayList = courseModalArrayList;
}
@NonNull
@Override
public CourseRVAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
// passing our layout file for displaying our card item
return new ViewHolder(LayoutInflater.from(context).inflate(R.layout.course_rv_item, parent, false));
}
@Override
public void onBindViewHolder(@NonNull CourseRVAdapter.ViewHolder holder, int position) {
// setting data to our text views from our modal class.
CourseModal courses = courseModalArrayList.get(position);
holder.courseNameTV.setText(courses.getCourseName());
holder.courseDurationTV.setText(courses.getCourseDuration());
holder.courseDescTV.setText(courses.getCourseDescription());
}
@Override
public int getItemCount() {
return courseModalArrayList.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
// creating variables for our text views.
private final TextView courseNameTV;
private final TextView courseDurationTV;
private final TextView courseDescTV;
public ViewHolder(@NonNull View itemView) {
super(itemView);
// initializing our text views.
courseNameTV = itemView.findViewById(R.id.idTVCourseName);
courseDurationTV = itemView.findViewById(R.id.idTVCourseDuration);
courseDescTV = itemView.findViewById(R.id.idTVCourseDescription);
}
}
}
步骤6:使用MainActivity。 Java文件
转到MainActivity。 Java文件并参考以下代码。下面是MainActivity的代码。 Java文件。在代码内部添加了注释,以更详细地了解代码。
Java
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.LinearSnapHelper;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.SnapHelper;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
// creating variables for our recycler view, array list and adapter class.
private RecyclerView coursesRV;
private ArrayList courseArrayList;
private CourseRVAdapter courseRVAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// initializing our recycler view and array list.
coursesRV = findViewById(R.id.idRVCourses);
courseArrayList = new ArrayList<>();
// on below line we are adding data to our array list.
courseArrayList.add(new CourseModal("DSA", "DSA Self Paced Course", "90 days"));
courseArrayList.add(new CourseModal("C++", "C++ Self Paced Course", "60 days"));
courseArrayList.add(new CourseModal("Java", "Java Self Paced Course", "60 days"));
courseArrayList.add(new CourseModal("Python", "Python Self Paced Course", "90 days"));
courseArrayList.add(new CourseModal("PHP", "PHP Self Paced Course", "40 days"));
courseArrayList.add(new CourseModal("DSA", "DSA Self Paced Course", "90 days"));
courseArrayList.add(new CourseModal("C++", "C++ Self Paced Course", "60 days"));
courseArrayList.add(new CourseModal("Java", "Java Self Paced Course", "60 days"));
courseArrayList.add(new CourseModal("Python", "Python Self Paced Course", "90 days"));
courseArrayList.add(new CourseModal("PHP", "PHP Self Paced Course", "40 days"));
// calling a method to prepare our recycler view.
prepareCourseRV();
// on below line we are creating a new variable for
// our snap helper class and initializing it for our Linear Snap Helper.
SnapHelper snapHelper = new LinearSnapHelper();
// on below line we are attaching this snap helper to our recycler view.
snapHelper.attachToRecyclerView(coursesRV);
}
private void prepareCourseRV() {
// on below line we are initializing our adapter class.
courseRVAdapter = new CourseRVAdapter(this, courseArrayList);
// on below line we are creating a variable for our linear layout manager.
LinearLayoutManager layoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
// on below line we are setting layout manager to our recycler view.
coursesRV.setLayoutManager(layoutManager);
// on below line we are setting adapter to our recycler view.
coursesRV.setAdapter(courseRVAdapter);
}
}
现在运行您的应用程序,并查看该应用程序的输出。
输出: