介绍
RecyclerView是Android一个更强大的列表控件,其不仅可以实现和ListView同样的效果,还有优化了ListView中的各种不足。其可以实现数据纵向滚动,也可以实现横向滚动。但是使用RecyclerView需要我们额外导入依赖
简单使用
1.导入依赖。点击 “Sync Project” 下载一下依赖包。
dependencies {
implementation 'androidx.recyclerview:recyclerview:1.1.0'
}
代码实现
// MainActivity
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;
import android.os.Bundle;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
public class MainActivity extends AppCompatActivity {
private List<Student> studentList = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 模拟生成数据
generateData();
RecyclerView recyclerView = findViewById(R.id.recycler_view_one);
/**
* RecyclerView将布局抽取出来了,所以这边需要给RecyclerView设置布局
* RecyclerView 支持LinearLayoutManager,GridLayoutManager和StaggeredGridLayoutManager
*/
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
recyclerView.setLayoutManager(linearLayoutManager);
// 网格显示
// GridLayoutManager gridLayoutManager= new GridLayoutManager(this,2);
// recyclerView.setLayoutManager(gridLayoutManager);
// 瀑布网格方式
// 构造参数: 一行多少个, 排布方向
// StaggeredGridLayoutManager staggeredGridLayoutManager =
// new StaggeredGridLayoutManager(2,LinearLayout.VERTICAL);
// recyclerView.setLayoutManager(staggeredGridLayoutManager);
MyAdapter myAdapter = new MyAdapter(studentList,this);
recyclerView.setAdapter(myAdapter);
// 点击事件
myAdapter.setOnRecyclerItemClickListener(new MyAdapter.OnRecyclerItemClickListener() {
@Override
public void onRecyclerItemClick(int position) {
Toast.makeText(getApplicationContext(),"点击了第"+(position+1)+"条",Toast.LENGTH_SHORT).show();
}
});
}
private void generateData() {
Random random = new Random();
for(int i = 0 ;i<100;i++){
Student student = new Student();
student.setId((i+1) +"");
student.setName("测试"+(i+1));
student.setScore(random.nextInt()+"");
studentList.add(student);
}
}
}
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import java.util.List;
/**
* 继承RecyclerView.Adapter<VH>。
* 这样设计的好处就是强制我们传入优化的方法,避免在开发时忘记优化
*/
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {
private List<Student> studentList ;
private Context context;
public MyAdapter(List<Student> studentList, Context context) {
this.studentList = studentList;
this.context = context;
}
/**
* 用来创建ViewHolder方法的,返回布局的view
* 然后利用MyViewHolder的构造方法去findViewById()
*
* @param parent
* @param viewType
* @return
*/
@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = View.inflate(context,R.layout.list_item,null);
return new MyViewHolder(view);
}
/**
* 将数据绑定到布局上,就是通过这个
* @param holder
* @param position
*/
@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
holder.textView_one.setText(studentList.get(position).getId());
holder.textView_two.setText(studentList.get(position).getName());
holder.textView_three.setText(studentList.get(position).getScore());
}
@Override
public int getItemCount() {
return studentList==null?0:studentList.size();
}
/**
* 优化方法:
* 获取view
*/
public class MyViewHolder extends RecyclerView.ViewHolder{
private TextView textView_one;
private TextView textView_two;
private TextView textView_three;
public MyViewHolder(@NonNull View itemView) {
super(itemView);
textView_one = itemView.findViewById(R.id.textView);
textView_two = itemView.findViewById(R.id.textView2);
textView_three = itemView.findViewById(R.id.textView3);
// 4.设置监听处理回调方法
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (onRecyclerItemClickListener!=null){
onRecyclerItemClickListener.onRecyclerItemClick(getAdapterPosition());
}
}
});
}
}
/**
* 因为RecyclerView没有提供监听点击事件,所以要自己设置
* 我们一般在Adapter上设置
*/
// 2 创建接口对象
private OnRecyclerItemClickListener onRecyclerItemClickListener;
// 1.创建接口,给外界实现,然后回调
public interface OnRecyclerItemClickListener{
void onRecyclerItemClick(int position);
}
// 3。给外部创建一个设置监听的方法
public void setOnRecyclerItemClickListener(OnRecyclerItemClickListener onRecyclerItemClickListener){
this.onRecyclerItemClickListener = onRecyclerItemClickListener;
}
}
public class Student {
private String id;
private String name;
private String score;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getScore() {
return score;
}
public void setScore(String score) {
this.score = score;
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="#706B6B"
android:orientation="vertical"
tools:context=".MainActivity">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler_view_one"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="40dp"
android:text="TextView"
android:textSize="24sp"
android:textColor="#535255"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="52dp"
android:text="TextView"
android:textSize="32sp"
android:textColor="#0C0C0C"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toEndOf="@+id/textView"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
android:textSize="20sp"
android:textColor="#53486A"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/textView2"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>