RecyclerView 19软工

本文详细介绍了在Android中使用RecyclerView来展示学生信息的步骤,包括:添加RecyclerView到布局文件,创建Student类,实现StudentViewHolder,设计student_item.xml布局,创建StudentAdapter,以及在MainActivity中设置适配器和数据。最后,通过ViewHolder复用验证,展示了RecyclerView的高效性能。
摘要由CSDN通过智能技术生成

RecyclerView

1.修改主界面布局 添加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"
    tools:context=".MainActivity"
    android:orientation="vertical">

    <androidx.recyclerview.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/rcv_stu"/>

</LinearLayout>
2.创建学生类
package com.example.demo;

public class Student {
    private int headImg;
    private String name;
    private int age;

    public Student(int headImg, String name, int age) {
        this.headImg = headImg;
        this.name = name;
        this.age = age;
    }

    public int getHeadImg() {
        return headImg;
    }

    public void setHeadImg(int headImg) {
        this.headImg = headImg;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}

3 创建StudentViewHolder
package com.example.demo;

import android.view.View;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

public class StudentViewHolder extends RecyclerView.ViewHolder {
    public StudentViewHolder(@NonNull View itemView) {
        super(itemView);
    }
}

4 画出每一行学生的布局 student_item.xml
<?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="50dp"
    tools:context=".MainActivity"
    android:orientation="horizontal"
    android:gravity="center_vertical">

   <ImageView
       android:layout_width="50dp"
       android:layout_height="50dp"
       android:padding="5dp"
       android:src="@drawable/dog1"/>

    <TextView
        android:layout_marginLeft="50dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="姓名"
        />

    <TextView
        android:layout_marginLeft="50dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="年龄"
        />
</LinearLayout>
5 创建适配器 StudentAdapter
package com.example.demo;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

public class StudentAdapter extends RecyclerView.Adapter<StudentViewHolder> {
    private Context context;
    private List<Student> studentList;

    public StudentAdapter(Context context, List<Student> studentList) {
        this.context = context;
        this.studentList = studentList;
    }

    @NonNull
    @Override
    public StudentViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(context).inflate(R.layout.student_item,
                parent, false);

        StudentViewHolder studentViewHolder = new StudentViewHolder(itemView);
        return studentViewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull StudentViewHolder holder, int position) {

    }

    @Override
    public int getItemCount() {
        return studentList.size();
    }
}

6 在主界面MainActivity中使用列表和适配器
package com.example.demo;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Bundle;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private List<Student> studentList = new ArrayList<>();
    private RecyclerView rcv_stu;
    private StudentAdapter studentAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initStudentList();

        rcv_stu = findViewById(R.id.rcv_stu);

        studentAdapter = new StudentAdapter(MainActivity.this, studentList);

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(MainActivity.this);
        rcv_stu.setLayoutManager(linearLayoutManager);

        rcv_stu.setAdapter(studentAdapter);

    }

    /**
     * 初始化学生列表
     */
    private void initStudentList() {
        Student s1 = new Student(R.drawable.dog1,"方群峰", 18);
        Student s2 = new Student(R.drawable.dog2,"古志萌", 19);
        Student s3 = new Student(R.drawable.dog3,"黄修茹", 20);
        Student s4 = new Student(R.drawable.dog4,"林立鹏", 21);
        Student s5 = new Student(R.drawable.dog5,"谭浩", 22);

        studentList.add(s1);
        studentList.add(s2);
        studentList.add(s3);
        studentList.add(s4);
        studentList.add(s5);
    }
}
7 将学生和每一行绑定
第1步 为student_item.xml添加每个组件的id
<?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="100dp"
    tools:context=".MainActivity"
    android:orientation="horizontal"
    android:gravity="center_vertical">

   <ImageView
       android:layout_width="50dp"
       android:layout_height="50dp"
       android:padding="5dp"
       android:src="@drawable/dog1"
       android:id="@+id/iv_head"/>

    <TextView
        android:layout_marginLeft="50dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="姓名"
        android:id="@+id/tv_name"
        />

    <TextView
        android:layout_marginLeft="50dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="年龄"
        android:id="@+id/tv_age"
        />
</LinearLayout>
第2步 在StudentViewHolde中创建组件并且绑定
package com.example.demo;

import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

public class StudentViewHolder extends RecyclerView.ViewHolder {
    ImageView iv_head;
    TextView tv_name;
    TextView tv_age;

    public StudentViewHolder(@NonNull View itemView) {
        super(itemView);

        iv_head = itemView.findViewById(R.id.iv_head);
        tv_name = itemView.findViewById(R.id.tv_name);
        tv_age = itemView.findViewById(R.id.tv_age);
    }
}

第3步 在StudentAdapter中实现BindViewHolder函数
 @Override
    public void onBindViewHolder(@NonNull StudentViewHolder holder, int position) {
        Student student = studentList.get(position);
        holder.iv_head.setImageResource(student.getHeadImg());
        holder.tv_name.setText(student.getName());
        holder.tv_age.setText(String.valueOf(student.getAge()));
    }
8 验证ViewHolder的复用

在onCreateViewHolder函数上面定义变量count 记录onCreateViewHolder的调用次数并打印日志
代码如下

 int count = 0;
    @NonNull
    @Override
    public StudentViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(context).inflate(R.layout.student_item,
                parent, false);

        StudentViewHolder studentViewHolder = new StudentViewHolder(itemView);
        count++;
        Log.e("onCreateViewHolder", "count = " + count);
        return studentViewHolder;
    }

输入日志如下:
在这里插入图片描述
会发现无论学生数量如何增加,count的值达到某个固定值后不再发生变化,一般是屏幕显示数量+5 左右
说明viewHolder实现了复用,而且是系统自动实现的。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值