当前位置:网站首页>Use of recyclerview with viewbinding
Use of recyclerview with viewbinding
2022-07-02 23:07:00 【Automatic 2004 haojinhui】
RecyclerView combination ViewBinding Use
introduce ViewBinding
android {
compileSdk 32
defaultConfig {
... ...
viewBinding {
enabled = true
}
}
add to Adapter Sub layout
<?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=" Xiao Wang " />
<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 Realization
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(" Xiao Ming : " + 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;
}
}
}
边栏推荐
- 海思 VI接入视频流程
- How does Jerry test the wrong touch rate of keys [chapter]
- 创新实力再获认可!腾讯安全MSS获2022年度云原生安全守护先锋
- xshell配置xforward转发火狐浏览器
- Kubernetes uses the host name to allocate the pod on the specified node
- Chow-Liu Tree
- [leetcode] reverse the word III in the string [557]
- 力扣刷题(2022-6-28)
- Jerry's prototype has no touch, and the reinstallation becomes normal after dismantling [chapter]
- 設置單擊右鍵可以選擇用VS Code打開文件
猜你喜欢
容器化技术在嵌入式领域的应用
Lambda表达式:一篇文章带你通透
Kubernetes uses the host name to allocate the pod on the specified node
[Solved] Splunk: Cannot get username when all users are selected“
PotPlayer设置最小化的快捷键
Webrtc audio and video capture and playback examples and mediastream media stream analysis
RecyclerView结合ViewBinding的使用
密码技术---分组密码的模式
实现BottomNavigationView和Navigation联动
Introduction to the latest plan of horizon in April 2022
随机推荐
pytorch训练CPU占用持续增长(bug)
Strictly abide by the construction period and ensure the quality, this AI data annotation company has done it!
Go multithreaded data search
Detailed explanation and application of merging and sorting
AES高級加密協議的動機闡述
Kubernetes uses the host name to allocate the pod on the specified node
Jerry's fast touch does not respond [chapter]
antd组件upload上传xlsx文件,并读取文件内容
Mask R-CNN
Golang interview finishing three resumes how to write
深度剖析数据在内存中的存储----C语言篇
Jerry's built-in short press and long press, no matter how long it is, it is a short press [chapter]
BBR 遭遇 CUBIC
QT qsplitter splitter
海思调用接口之Makefile配置
Go language sqlx library operation SQLite3 database addition, deletion, modification and query
Jatpack------LiveData
easyclick,ec权朗网络验证源码
[羊城杯2020]easyphp
密码技术---密钥和SSL/TLS