RecyclerView.ViewHolder、Adapter 「爱情、让人受尽委屈。」 2023-01-07 07:08 125阅读 0赞 # RecyclerView.ViewHolder、Adapter # ### //今天同事在使用BaseQuickAdapter(万能适配器)时想了解下原生adapter怎么写 ### ## RecyclerView ## ### 用户在滑动时会**收回**隐藏的listitem RecyclerView 做的就是**回收再利用**的工作 ### 在listview的基础上添加了manager(管理者) LinerLayoutManager StaggeredGridLayoutManager(瀑布流布局) GridLayoutManager(网格布局) 在这些管理者中制定了一套可扩展的布局排列接口,子类只要按照接口的规范来实现,就能定制出所需的布局了 ## ViewHolder ## ### view容器 ### 就是子项itemlist最外层布局 ## Adapter ## ### 适配器 ### Adapter从模型层获取数据,然后提供给RecyclerView显示,是沟通的桥梁。创建ViewHolder和将模型层的数据绑 定到ViewHolder上 ### demo ### 实体类 import lombok.Data; /** * @program: TestRecycleView * @description: 实体类 * @author: tkx * @create: 2021-01-13 14:55 **/ @Data public class Fruit { private String name; private int id; public Fruit(String name, int id) { this.name = name; this.id = id; } } ### Adapter ### 适配器 import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ImageView; import android.widget.TextView; import android.widget.Toast; import androidx.annotation.NonNull; import androidx.recyclerview.widget.RecyclerView; import java.util.List; /** * @program: TestRecycleView * @description: * @author: tkx * @create: 2021-01-13 14:30 **/ public class FruitAdater extends RecyclerView.Adapter<FruitAdater.ViewHolder> { private List<Fruit> mFruitList; //先写个静态内部类Viewholder 继承于RecyclerView.ViewHolder static class ViewHolder extends RecyclerView.ViewHolder { View fruitView; ImageView fruitImage; TextView fruitName; //ViewHolder的构造函数 itemview就是要传入的子项itemlist最外层布局 public ViewHolder(@NonNull View itemView) { super(itemView); fruitView = itemView; fruitImage = itemView.findViewById(R.id.image); fruitName = itemView.findViewById(R.id.text); } } //写个构造函数方便传数据 public FruitAdater(List<Fruit> fruitList) { mFruitList = fruitList; } @NonNull @Override public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { View view = LayoutInflater.from(parent.getContext()) .inflate(R.layout.fruit_item, parent, false); final ViewHolder holder = new ViewHolder(view); holder.fruitView.setOnClickListener((View v) -> { int position = holder.getAdapterPosition(); Fruit fruit = mFruitList.get(position); Toast.makeText(v.getContext(), "你点了view" + fruit.getName(), Toast.LENGTH_SHORT).show(); }); holder.fruitImage.setOnClickListener((View v) -> { int position = holder.getAdapterPosition(); Fruit fruit = mFruitList.get(position); Toast.makeText(v.getContext(), "你点了image" + fruit.getName(), Toast.LENGTH_SHORT).show(); }); return holder; } @Override public void onBindViewHolder(@NonNull ViewHolder holder, int position) { Fruit fruit = mFruitList.get(position); holder.fruitImage.setImageResource(fruit.getId()); holder.fruitName.setText(fruit.getName()); } @Override public int getItemCount() { return mFruitList.size(); } } ### Activity ### 活动类 package com.example.testrecycleview; import androidx.appcompat.app.AppCompatActivity; import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.RecyclerView; import androidx.recyclerview.widget.StaggeredGridLayoutManager; import android.os.Bundle; import java.util.ArrayList; import java.util.List; import java.util.Random; /** * @author tkx */ public class MainActivity extends AppCompatActivity { private List<Fruit> fruitList = new ArrayList<>(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initFruit(); //瀑布流布局 可通过下面切换注释切换manager更换布局 RecyclerView recyclerView = findViewById(R.id.rv_list); //实例化manager 设为三列 纵向布局 StaggeredGridLayoutManager staggeredGridLayoutManager =new StaggeredGridLayoutManager(3,StaggeredGridLayoutManager.VERTICAL); // LinearLayoutManager layoutManager = new LinearLayoutManager(this); // layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL); // recyclerView.setLayoutManager(layoutManager); recyclerView.setLayoutManager(staggeredGridLayoutManager); FruitAdater fruitAdater = new FruitAdater(fruitList); //给recyclerview设置适配器 recyclerView.setAdapter(fruitAdater); } //随便来点数据 private void initFruit() { fruitList.add(new Fruit(getRandomLength("apple"), R.drawable.ic_launcher_foreground)); fruitList.add(new Fruit(getRandomLength("asdasd"), R.drawable.ic_launcher_foreground)); fruitList.add(new Fruit(getRandomLength("qwe"), R.drawable.ic_launcher_foreground)); fruitList.add(new Fruit(getRandomLength("aqweqwe"), R.drawable.ic_launcher_foreground)); fruitList.add(new Fruit(getRandomLength("aqweqw"), R.drawable.ic_launcher_foreground)); fruitList.add(new Fruit(getRandomLength("banana"), R.drawable.ic_launcher_foreground)); fruitList.add(new Fruit(getRandomLength("qweqwe"), R.drawable.ic_launcher_foreground)); fruitList.add(new Fruit(getRandomLength("aqwe"), R.drawable.ic_launcher_foreground)); fruitList.add(new Fruit(getRandomLength("appledfsdf"), R.drawable.ic_launcher_foreground)); fruitList.add(new Fruit(getRandomLength("appledfsdf"), R.drawable.ic_launcher_foreground)); fruitList.add(new Fruit(getRandomLength("appledfsdf"), R.drawable.ic_launcher_foreground)); fruitList.add(new Fruit(getRandomLength("appledfsdf"), R.drawable.ic_launcher_foreground)); fruitList.add(new Fruit(getRandomLength("appledfsdf"), R.drawable.ic_launcher_foreground)); fruitList.add(new Fruit(getRandomLength("appledfsdf"), R.drawable.ic_launcher_foreground)); fruitList.add(new Fruit(getRandomLength("appledfsdf"), R.drawable.ic_launcher_foreground)); fruitList.add(new Fruit(getRandomLength("appledfsdf"), R.drawable.ic_launcher_foreground)); fruitList.add(new Fruit(getRandomLength("appledfsdf"), R.drawable.ic_launcher_foreground)); fruitList.add(new Fruit(getRandomLength("appledfsdf"), R.drawable.ic_launcher_foreground)); } //数据要长短不一瀑布流才明显 随便写个重复随机数据的方法 随机的次数是1-20的随机数 private String getRandomLength(String name){ Random random = new Random(); int length =random.nextInt(20)+1; StringBuilder builder =new StringBuilder(); for (int j = 0; j <length ; j++) { builder.append(name); } return builder.toString(); } } ### 创建ViewHolder或者与已创建的ViewHolder相关联时就要用到Adapter ### ### 在Adapter中我们通常要实现三个方法 ### ## 1.用来创建ViewHolder ## public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) ## 2.用来绑定数据 ## public void onBindViewHolder(@NonNull ViewHolder holder, int position) ## 3.返回总共要显示的列表数量 ## public int getItemCount() ## 先,调用Adapter.getItemCount(),RecyclerView会知道数组列表中包含多少个待展示的视图。然后,RecyclerView调用Adapter.onCreateViewHolder(ViewGroup, int)创建ViewHolder。 ## ## 最后,RecyclerView会传入ViewHolder及其位置,onBindViewHolder(ViewHolder, int)来绑定数据。有了够用的ViewHolder,RecyclerView就会停止调用onCreateViewHolder()方法。回收利用旧的ViewHolder来节约内存。 ##
还没有评论,来说两句吧...