android开发_003:RecyclerView控件使用

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




显示数据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);
        }
    }
}

运行截图:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值