当前位置:网站首页>Usage of RecyclerView
Usage of RecyclerView
2022-08-04 06:05:00 【N_Y_S】
首先RecyclerViewIt is a container-type control.
is a new control for large data display,可以用来代替传统的ListView,更加强大和灵活.”
什么时候使用RecyclerView?
当数据结构(列表)足够大的时候,You need to use the container nature of the control
从android5.0Beginning Google released a new control for the display of large amounts of data,can be stored dynamically.
There are three layouts,线性布局,Grid layout and waterfall flow
重点
RecyclerView的创建以及使用,And how to switch the trying mode
RecyclerView的使用方法
1.现在activity的xml文件中使用RecycleView控件
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rv_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />2.创建RecycleView的子项目

3.Add the controls that need to be added to the container and layout them in the subproject(Because this sub-project is equivalent to the existence of a template,This template will be put into the container as a whole)
<ImageView
android:id="@+id/iv_img"
android:layout_width="55dp"
android:layout_height="55dp"
app:srcCompat="@mipmap/list" />
<TextView
android:id="@+id/tv_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="5dp"
android:text="TextView" />4.The next step is to use an adapter,创建一个.Java类并且继承RecyclerView.Adapter,Note that methods of abstract classes cannot be implemented directly here,需要自己创建一个ViewHoldermethod and inheritRecyclerView.ViewHolder
public class MailAdapter extends RecyclerView.Adapter<MailAdapter.ViewHolder> {
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
return null;
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
}
@Override
public int getItemCount() {
return 0;
}
public class ViewHolder extends RecyclerView.ViewHolder {
public ViewHolder(@NonNull View itemView) {
super(itemView);
}
}
}5.在ViewHodlerBind the control in the method
public class ViewHolder extends RecyclerView.ViewHolder {
View view;
ImageView mailImg;
TextView mailName;
public ViewHolder(@NonNull View itemView) {
super(itemView);
view=itemView;
mailImg=itemView.findViewById(R.id.iv_img);
mailName=itemView.findViewById(R.id.tv_name);
}
}6.Get which page is called
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view=LayoutInflater.from(parent.getContext()).inflate(R.layout.mail_item,parent,false);
ViewHolder viewHolder=new ViewHolder(view);
return viewHolder;
}7.Because there are several pages to be determined according to the incoming data,So to create with list参数的构造方法
private List<Mail_item> mList;
public MailAdapter(List<Mail_item> mList) {
this.mList = mList;
}
//这个方法就是根据list的长度来确定,Create several screens
@Override
public int getItemCount() {
return mList.size();
}8.设置控件属性
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
Mail_item mail=mList.get(position);
holder.mailImg.setImageResource(mail.getImg());
holder.mailName.setText(mail.getName());
}9.在acticaty设置控件属性
private String[] names={"张一","张二","张三","李一","李二","李三","李四","王一","王二","王三","王四","王五","小明","小亮"};
private int[] imgs={R.mipmap.a_1,R.mipmap.a_2,R.mipmap.a_3,R.mipmap.a_4,R.mipmap.a_5,R.mipmap.a_6,R.mipmap.a_7,R.mipmap.a_8,R.mipmap.a_9,R.mipmap.a_10,R.mipmap.a_11,R.mipmap.a_12,R.mipmap.a_13,R.mipmap.a_14};
10.The loop puts the value into the control
private void initDate() {
for (int i=0;i<names.length;i++){
Mail_item mail=new Mail_item(names[i],imgs[i]);
list.add(mail);
}
}11.设置RecycleView
initDate();
rv_view=findViewById(R.id.rv_view);
RecyclerView recyclerView=findViewById(R.id.rv_view);
LinearLayoutManager layoutManager=new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
MailAdapter mailAdapter=new MailAdapter(list);
recyclerView.setAdapter(mailAdapter);扩展
ViewPager2轮播图,是继承与RecycleViewA new container control for
使用方法和RecycleView雷同
主要是在ActivityThe implementation in the code-behind is different
vr_swiper=findViewById(R.id.vr_swiper);
ListSwiper listSwiper=new ListSwiper();
list=listSwiper.initDate();
SwiperAdapter adapter=new SwiperAdapter(list);
CompositePageTransformer cpt=new CompositePageTransformer();
//Add spacing when sliding
cpt.addTransformer(new MarginPageTransformer(20));
//Added zoom on swipe
cpt.addTransformer(new ScaleInTransformer());
vr_swiper.setOffscreenPageLimit(1);
//一屏多页
View view = vr_swiper.getChildAt(0);
if(view != null && view instanceof RecyclerView){
view.setPadding(100, 0, 100, 0);
((RecyclerView) view).setClipToPadding(false);
}
vr_swiper.setPageTransformer(cpt);
vr_swiper.setAdapter(adapter);initDate()代码如下:

边栏推荐
- Logistic Regression --- Introduction, API Introduction, Case: Cancer Classification Prediction, Classification Evaluation, and ROC Curve and AUC Metrics
- 进程、线程、协程的区别和联系?
- 攻防世界MISC———Dift
- k3s-轻量级Kubernetes
- MySql的concat和group_concat的区别
- 剑指 Offer 2022/7/1
- 字典特征提取,文本特征提取。
- 对象存储-分布式文件系统-MinIO-1:概念
- 剑指 Offer 2022/7/5
- ReentrantLock(公平锁、非公平锁)可重入锁原理
猜你喜欢
随机推荐
将两个DataTable合并——DataTable.Merge 方法
自动化运维工具Ansible(1)基础
Android connects to mysql database using okhttp
Commons Collections2
网络大作业心得笔记
自动化运维工具Ansible(7)roles
Postgresql 快照
剑指 Offer 2022/7/8
线性回归简介01---API使用案例
进程、线程、协程的区别和联系?
sklearn中的pipeline机制
SQL练习 2022/6/30
判断字符串是否有子字符串重复出现
Androd Day02
k3s-轻量级Kubernetes
flink sql left join数据倾斜问题解决
IvNWJVPMLt
flink onTimer定时器实现定时需求
读研碎碎念
flink-sql大量使用案例









