当前位置:网站首页>RecyclerView结合ViewBinding的使用
RecyclerView结合ViewBinding的使用
2022-07-02 22:09:00 【自动2004郝金辉】
RecyclerView结合ViewBinding的使用
引入ViewBinding

android {
compileSdk 32
defaultConfig {
... ...
viewBinding {
enabled = true
}
}
添加Adapter子布局

<?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" android:background="#D05C5C" android:orientation="vertical" android:padding="16dp">
<TextView android:id="@+id/main_name" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="小王" />
<TextView android:id="@+id/main_phone" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="16dp" android:text="123543123" />
</LinearLayout>
RecyclerViewAdapter实现

import android.annotation.SuppressLint;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.example.recyclerview.databinding.ViewMainBinding;
import java.util.List;
public class MainAdapter extends RecyclerView.Adapter<MainAdapter.MainViewHolder> {
private static final String TAG = "MainAdapter";
private List<String> mList;
@SuppressLint("NotifyDataSetChanged")
public void setList(List<String> list) {
mList = list;
Log.e(TAG, "setList: " + mList);
notifyDataSetChanged();
}
@NonNull
@Override
public MainViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
ViewMainBinding binding = ViewMainBinding
.inflate(LayoutInflater.from(parent.getContext()), parent, false);
return new MainViewHolder(binding);
}
@SuppressLint("SetTextI18n")
@Override
public void onBindViewHolder(@NonNull MainViewHolder holder, int position) {
holder.binding.mainName.setText("小明: " + position);
holder.binding.mainPhone.setText(String.valueOf(position * 31321));
}
@Override
public int getItemCount() {
return mList == null ? 0 : mList.size();
}
static class MainViewHolder extends RecyclerView.ViewHolder {
final ViewMainBinding binding;
MainViewHolder(ViewMainBinding binding) {
super(binding.getRoot());
this.binding = binding;
}
}
}
边栏推荐
- Golang's learning route
- STM32串口DAM接收253字节就死机原因排查
- Set right click to select vs code to open the file
- 静态文件显示问题
- 情感对话识别与生成简述
- Stop slave is stuck -- the event of the transaction is not copied completely
- To myself who is about to work
- Mask R-CNN
- [leetcode] reverse string [344]
- 1px pixel compatibility of mobile terminal, 1px border
猜你喜欢

Application of containerization technology in embedded field

【板栗糖GIS】arcscene—如何做出有高度的高程图

Boot actuator - Prometheus use

PMP project integration management

Analyse des données dossiers d'apprentissage - - analyse simple de la variance à facteur unique avec Excel

Qt QSplitter拆分器
![Jielizhi, production line assembly link [chapter]](/img/0d/102596ad13aafd9c6133509d9064dd.png)
Jielizhi, production line assembly link [chapter]

Lambda expression: an article takes you through
![The kth largest element in the [leetcode] array [215]](/img/72/d3e46a820796a48b458cd2d0a18f8f.png)
The kth largest element in the [leetcode] array [215]
![[Yangcheng cup 2020] easyphp](/img/12/da28f738e50e625b0a66a94af3703d.png)
[Yangcheng cup 2020] easyphp
随机推荐
Addition, deletion, modification and query of handwritten ORM (object relationship mapping)
P1007 独木桥
odoo13搭建医院HRP环境(详细步骤)
QT qsplitter splitter
情感对话识别与生成简述
[chestnut sugar GIS] ArcMap - why should the tick of classic capture be removed when using custom capture?
解决Chrome浏览器和Edeg浏览器主页被篡改的方法
设置单击右键可以选择用VS Code打开文件
STM32串口DAM接收253字节就死机原因排查
[Solved] Splunk: Cannot get username when all users are selected“
在SOUI里使用真窗口时使用SOUI的滚动条
创新实力再获认可!腾讯安全MSS获2022年度云原生安全守护先锋
20220527_数据库过程_语句留档
MySQL reset password, forget password, reset root password, reset MySQL password
Learning Websites commonly used by circuit designers
成功改变splunk 默认URL root path
ServletContext learning diary 1
容器化技术在嵌入式领域的应用
QT qpprogressbar details
Configuration clic droit pour choisir d'ouvrir le fichier avec vs Code