欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

android开发_003:RecyclerView控件使用

程序员文章站 2022-05-14 19:32:22
...

使用RecyclerView前要在app/build.gradlez中dependencies中添加依赖:

android开发_003:RecyclerView控件使用

android开发_003:RecyclerView控件使用

android开发_003:RecyclerView控件使用

显示数据Java Bean Student.java

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

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

    public String getName() {
        return name;
    }

    public int getAge() {
        return age;
    }

    public String getClass_name() {
        return class_name;
    }
}

适配器布局文件  student_item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">


            <TextView
                 android:id="@+id/student_name"
                 android:gravity="center"
                 android:layout_weight="1"
                 android:text="姓名"
                 android:layout_width="0dp"
                 android:layout_height="50dp" />

            <TextView
                 android:id="@+id/student_age"
                 android:gravity="center"
                 android:layout_weight="1"
                 android:text="年龄"
                 android:layout_width="0dp"
                 android:layout_height="50dp" />

            <TextView
                 android:id="@+id/student_class_name"
                 android:gravity="center"
                 android:layout_weight="1"
                 android:text="班级"
                 android:layout_width="0dp"
                android:layout_height="50dp" />


</LinearLayout>

RecyclerView适配器     StudentAdapter.java

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.List;

public class StudentAdapter extends RecyclerView.Adapter<StudentAdapter.ViewHolder>{
    private List<Student> mList;

    static class ViewHolder extends RecyclerView.ViewHolder{
        TextView student_name;
        TextView student_age;
        TextView student_class_name;
        public ViewHolder(View view){
            super(view);
            student_name=(TextView)view.findViewById(R.id.student_name);
            student_age=(TextView)view.findViewById(R.id.student_age);
            student_class_name=(TextView)view.findViewById(R.id.student_class_name);
        }
    }
    public StudentAdapter(List<Student>studentList){
        mList=studentList;
    }
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.student_item,parent,false);
        ViewHolder holder=new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        Student student=mList.get(position);
        holder.student_name.setText(student.getName());
        holder.student_age.setText(String.valueOf(student.getAge()));
        holder.student_class_name.setText(student.getClass_name());
    }

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


}

main布局文件 activity_main.xml

<?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"
    android:orientation="vertical">

    <include layout="@layout/student_item">

    </include>

    <android.support.v7.widget.RecyclerView
        android:id="@+id/Recycler_View"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    </LinearLayout>

MainActivity.java

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

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

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);
        initStudent();
        RecyclerView recyclerView=(RecyclerView)findViewById(R.id.Recycler_View);
        LinearLayoutManager linearLayoutManager=new LinearLayoutManager(this);
        recyclerView.setLayoutManager(linearLayoutManager);
        StudentAdapter studentAdapter=new StudentAdapter(studentList);
        recyclerView.setAdapter(studentAdapter);
    }
    private void initStudent(){
        for(int i=0;i<6;i++){
            Student s1=new Student("zzq",18,"三年一班");
            studentList.add(s1);
            Student s2=new Student("q",17,"三年一班");
            studentList.add(s2);
            Student s3=new Student("zq",19,"三年一班");
            studentList.add(s3);
        }
    }
}

运行截图:android开发_003:RecyclerView控件使用