当前位置:   article > 正文

Android RecyclerView基础使用方法_安卓recyclerview

安卓recyclerview
RecyclerView是一种使用广泛的列表操作,本文仅为基础的使用方法

首先,在主布局文件里使用 RecyclerView

  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:app="http://schemas.android.com/apk/res-auto"
  3. xmlns:tools="http://schemas.android.com/tools"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent"
  6. tools:context=".RecyclerViewActivity">
  7. <androidx.recyclerview.widget.RecyclerView
  8. android:id="@+id/rv"
  9. android:layout_width="match_parent"
  10. android:layout_height="match_parent"></androidx.recyclerview.widget.RecyclerView>
  11. </LinearLayout>

其次,需要在bulid.gradle文件中,添加依赖

implementation("androidx.recyclerview:recyclerview:1.2.1")

然后,需要先建一个单条消息的示例,比如我在这里就只是最简单的显示一行文本

  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. android:layout_width="match_parent"
  3. android:layout_height="match_parent">
  4. <TextView
  5. android:id="@+id/tv"
  6. android:layout_width="match_parent"
  7. android:layout_height="wrap_content"
  8. android:padding="5dp"
  9. android:text="name"
  10. android:textSize="25sp"></TextView>
  11. </LinearLayout>

这里还创建了一个实体类Person,用来描述数据,这里就只给了一个属性name

  1. public class Person {
  2. private String name;
  3. public Person() {
  4. }
  5. public Person(String name) {
  6. this.name = name;
  7. }
  8. public String getname() {
  9. return name;
  10. }
  11. public void setname(String name) {
  12. this.name = name;
  13. }
  14. }

最后就是java代码实现模块:

先通过findViewById找到RecyclerView,然后给他一个适配器,接下来就是实现RecyclerView.Adapter,并构造它的方法,这里用的是LinearLayoutManager,并给了一个加横线样式DividerItemDecoration,下面是完整的代码:

  1. package com.example.myapplication;
  2. import androidx.annotation.NonNull;
  3. import androidx.appcompat.app.AppCompatActivity;
  4. import androidx.recyclerview.widget.DividerItemDecoration;
  5. import androidx.recyclerview.widget.LinearLayoutManager;
  6. import androidx.recyclerview.widget.RecyclerView;
  7. import android.os.Bundle;
  8. import android.view.View;
  9. import android.view.ViewGroup;
  10. import android.widget.Adapter;
  11. import android.widget.TextView;
  12. import com.example.myapplication.entity.Person;
  13. import java.util.ArrayList;
  14. import java.util.List;
  15. public class RecyclerViewActivity extends AppCompatActivity {
  16. List<Person> personList = new ArrayList<>();
  17. @Override
  18. protected void onCreate(Bundle savedInstanceState) {
  19. super.onCreate(savedInstanceState);
  20. setContentView(R.layout.activity_recycler_view);
  21. Person person = new Person("张三");
  22. Person person1 = new Person("李四");
  23. Person person2 = new Person("王五");
  24. personList.add(person);
  25. personList.add(person1);
  26. personList.add(person2);
  27. /*for (int i = 1; i <= 30; i++) {
  28. Person person = new Person();
  29. person.setname("张三" + i + "号");
  30. personList.add(person);
  31. }*/
  32. RecyclerView rv = findViewById(R.id.rv);
  33. Adapter adapter = new Adapter();
  34. rv.setAdapter(adapter);
  35. // 设置列表样式
  36. LinearLayoutManager layoutManager = new LinearLayoutManager(this);
  37. rv.setLayoutManager(layoutManager);
  38. // 加横线隔开样式
  39. DividerItemDecoration decoration = new DividerItemDecoration(this, DividerItemDecoration.VERTICAL);
  40. rv.addItemDecoration(decoration);
  41. }
  42. public class Adapter extends RecyclerView.Adapter<Adapter.MyHolder> {
  43. public class MyHolder extends RecyclerView.ViewHolder {
  44. private final TextView tv;
  45. public MyHolder(@NonNull View itemView) {
  46. super(itemView);
  47. tv = itemView.findViewById(R.id.tv);
  48. }
  49. }
  50. @NonNull
  51. @Override
  52. public Adapter.MyHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
  53. View view = View.inflate(RecyclerViewActivity.this, R.layout.rv_item, null);
  54. MyHolder myHolder = new MyHolder(view);
  55. return myHolder;
  56. }
  57. @Override
  58. public void onBindViewHolder(@NonNull Adapter.MyHolder holder, int position) {
  59. Person person = personList.get(position);
  60. holder.tv.setText(person.getname());
  61. }
  62. @Override
  63. public int getItemCount() {
  64. return personList.size();
  65. }
  66. }
  67. }

运行效果图

 把文中给Person添加数据的注释,把下面的for循环取消注释后,效果就变成了

 文章结束,本文的存在,方便了CV编程,CV便捷生活!

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Monodyee/article/detail/286700
推荐阅读
相关标签
  

闽ICP备14008679号