赞
踩
RecyclerView是一种使用广泛的列表操作,本文仅为基础的使用方法
首先,在主布局文件里使用 RecyclerView
- <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=".RecyclerViewActivity">
-
- <androidx.recyclerview.widget.RecyclerView
- android:id="@+id/rv"
- android:layout_width="match_parent"
- android:layout_height="match_parent"></androidx.recyclerview.widget.RecyclerView>
-
- </LinearLayout>
其次,需要在bulid.gradle文件中,添加依赖
implementation("androidx.recyclerview:recyclerview:1.2.1")
然后,需要先建一个单条消息的示例,比如我在这里就只是最简单的显示一行文本
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent">
-
- <TextView
- android:id="@+id/tv"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:padding="5dp"
- android:text="name"
- android:textSize="25sp"></TextView>
-
- </LinearLayout>
这里还创建了一个实体类Person,用来描述数据,这里就只给了一个属性name
- public class Person {
- private String name;
-
- public Person() {
- }
-
- public Person(String name) {
- this.name = name;
- }
-
- public String getname() {
- return name;
- }
-
- public void setname(String name) {
- this.name = name;
- }
-
- }
最后就是java代码实现模块:
先通过findViewById找到RecyclerView,然后给他一个适配器,接下来就是实现RecyclerView.Adapter,并构造它的方法,这里用的是LinearLayoutManager,并给了一个加横线样式DividerItemDecoration,下面是完整的代码:
- package com.example.myapplication;
-
- import androidx.annotation.NonNull;
- 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.view.ViewGroup;
- import android.widget.Adapter;
- import android.widget.TextView;
-
- import com.example.myapplication.entity.Person;
-
- import java.util.ArrayList;
- import java.util.List;
-
- public class RecyclerViewActivity extends AppCompatActivity {
-
- List<Person> personList = new ArrayList<>();
-
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_recycler_view);
-
- Person person = new Person("张三");
- Person person1 = new Person("李四");
- Person person2 = new Person("王五");
- personList.add(person);
- personList.add(person1);
- personList.add(person2);
-
- /*for (int i = 1; i <= 30; i++) {
- Person person = new Person();
- person.setname("张三" + i + "号");
- personList.add(person);
- }*/
-
- RecyclerView rv = findViewById(R.id.rv);
- Adapter adapter = new Adapter();
- rv.setAdapter(adapter);
-
- // 设置列表样式
- LinearLayoutManager layoutManager = new LinearLayoutManager(this);
- rv.setLayoutManager(layoutManager);
- // 加横线隔开样式
- DividerItemDecoration decoration = new DividerItemDecoration(this, DividerItemDecoration.VERTICAL);
- rv.addItemDecoration(decoration);
- }
-
- public class Adapter extends RecyclerView.Adapter<Adapter.MyHolder> {
- public class MyHolder extends RecyclerView.ViewHolder {
- private final TextView tv;
- public MyHolder(@NonNull View itemView) {
- super(itemView);
- tv = itemView.findViewById(R.id.tv);
- }
- }
-
- @NonNull
- @Override
- public Adapter.MyHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
- View view = View.inflate(RecyclerViewActivity.this, R.layout.rv_item, null);
- MyHolder myHolder = new MyHolder(view);
- return myHolder;
- }
-
- @Override
- public void onBindViewHolder(@NonNull Adapter.MyHolder holder, int position) {
- Person person = personList.get(position);
- holder.tv.setText(person.getname());
- }
-
- @Override
- public int getItemCount() {
- return personList.size();
- }
- }
-
- }
运行效果图
把文中给Person添加数据的注释,把下面的for循环取消注释后,效果就变成了
文章结束,本文的存在,方便了CV编程,CV便捷生活!
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。