Android官方文档上说明是“在您的界面中显示大量数据,同时最大限度减少内存用量”。
此文中的用法适用于实现ListView的效果
效果:
工程目录
1.先创建一个用于存放数据的实体,存放图片id以及显示的文字
package com.example.myapplication.myrecyclerview.model;
public class Student {
private int imgId;
private String name;
public Student(int imgId, String name) {
this.imgId = imgId;
this.name = name;
}
public int getImgId() {
return imgId;
}
public String getName() {
return name;
}
}
2.创建每行的模板xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_margin="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/item_img"
android:layout_width="50dp"
android:layout_height="50dp"></ImageView>
<TextView
android:id="@+id/item_text"
android:layout_marginLeft="10dp"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_gravity="center_vertical"
></TextView>
</LinearLayout>
3.添加适配器
package com.example.myapplication.myrecyclerview;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.example.myapplication.R;
import com.example.myapplication.myrecyclerview.model.Student;
import java.util.List;
public class StudentAdapter extends RecyclerView.Adapter<StudentAdapter.ViewHolder> {
//存放数据
List<Student> studentList;
//点击回调接口
OnItemClickListener onItemClickListener;
//通过构造函数传入数据
public StudentAdapter(List<Student> dataList) {
this.studentList = dataList;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
//布局加载器
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item, parent, false);
return new ViewHolder(view);
}
/**
* 位置对应的数据与holder进行绑定
*
* @param holder
* @param position
*/
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
Student student = studentList.get(position);
holder.imageView.setImageResource(student.getImgId());
holder.textView.setText(student.getName());
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(onItemClickListener!=null){
Log.e("TT",holder.itemView.toString());
onItemClickListener.onClick(student,position);
}
}
});
}
/**
* 获取数据长度
* @return
*/
@Override
public int getItemCount() {
return studentList.size();
}
/**
* 缓存页面布局,页面快速滚动时不必每次都重新创建View
*/
static class ViewHolder extends RecyclerView.ViewHolder {
ImageView imageView;
TextView textView;
public ViewHolder(@NonNull View itemView) {
super(itemView);
imageView = (ImageView) itemView.findViewById(R.id.item_img);
textView = (TextView) itemView.findViewById(R.id.item_text);
}
}
/**
* 定义点击回调接口
*/
public interface OnItemClickListener{
void onClick(Student student,int position);
}
/**
* 设置点击的监听回调
* @param onItemClickListener
*/
public void setOnItemClickListener(OnItemClickListener onItemClickListener){
this.onItemClickListener=onItemClickListener;
}
}
4.在Acivity的布局文件中添加RecyclerView
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical"
tools:context=".myrecyclerview.RecyclerActivity">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/my_recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"></androidx.recyclerview.widget.RecyclerView>
</LinearLayout>
5.在Activity中添加数据以及相关的处理
package com.example.myapplication.myrecyclerview;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.Toast;
import com.example.myapplication.R;
import com.example.myapplication.myrecyclerview.model.Student;
import java.util.ArrayList;
import java.util.List;
public class RecyclerActivity extends AppCompatActivity {
List<Student> studentList = new ArrayList<Student>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_recycler);
initRecycler();
}
private void initRecycler() {
studentList.clear();
for (int i = 0; i < 100; i++) {
Student student = new Student(R.drawable.image, "老金" + i);
studentList.add(student);
}
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.my_recyclerView);
//给recyclerView创建布局方式
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
//创建适配器
StudentAdapter studentAdapter = new StudentAdapter(studentList);
//点击回调
studentAdapter.setOnItemClickListener(new StudentAdapter.OnItemClickListener() {
@Override
public void onClick(Student student, int position) {
Toast.makeText(RecyclerActivity.this,student.getName()+"-"+position,Toast.LENGTH_SHORT).show();
}
});
recyclerView.setAdapter(studentAdapter);
//设置分割线
recyclerView.addItemDecoration(new DividerItemDecoration(this,DividerItemDecoration.VERTICAL));
}
}