当前位置:网站首页>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;
}
}
}
边栏推荐
- [hardware] origin of standard resistance value
- Learning records of data analysis (II) -- simple use of response surface method and design expert
- P1007 single log bridge
- Minimum spanning tree
- Jerry's charge unplugged, unable to touch the boot [chapter]
- MySQL reset password, forget password, reset root password, reset MySQL password
- Rails 3 activerecord: sort by association count - rails 3 activerecord: order by count on Association
- 泛型与反射,看这篇就够了
- 1px pixel compatibility of mobile terminal, 1px border
- [favorite poems] OK, song
猜你喜欢
數據分析學習記錄--用EXCEL完成簡單的單因素方差分析
Qt QProgressBar详解
Share 10 JS closure interview questions (diagrams), come in and see how many you can answer correctly
设置单击右键可以选择用VS Code打开文件
Innovation strength is recognized again! Tencent security MSS was the pioneer of cloud native security guard in 2022
Xshell configuration xforward forwarding Firefox browser
P7072 [CSP-J2020] 直播获奖
海思3559万能平台搭建:在截获的YUV图像上旋转操作
[leetcode] reverse the word III in the string [557]
Kubernetes uses the host name to allocate the pod on the specified node
随机推荐
编辑卡顿
海思3559万能平台搭建:在截获的YUV图像上旋转操作
Editor Caton
Stop slave is stuck -- the event of the transaction is not copied completely
Easyclick, EC Quanlang network verification source code
Array advanced improvement
Jerry's built-in shutdown current is 1.2ua, and then it can't be turned on by long pressing [chapter]
Go condition variable
Innovation strength is recognized again! Tencent security MSS was the pioneer of cloud native security guard in 2022
[羊城杯2020]easyphp
QT qsplitter splitter
MySQL queries nearby data And sort by distance
【硬件】标准阻值的由来
【板栗糖GIS】arcscene—如何做出有高度的高程图
Lambda表达式:一篇文章带你通透
20220527_数据库过程_语句留档
[Luogu p1541] tortoise chess [DP]
1px pixel compatibility of mobile terminal, 1px border
Construction of Hisilicon 3559 universal platform: draw a frame on the captured YUV image
Higher order operation of bits