这篇文章主要讲解了“recyclerview怎么使用”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“recyclerview怎么使用”吧!
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".activity.RecyclerViewActivity">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
public class RecyclerViewActivity extends AppCompatActivity {
RecyclerView recyclerView;
List<Sample> sampleList = new ArrayList<>();
int listSize = 100;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_recycler_view);
recyclerView = findViewById(R.id.recyclerView);
initSampleList();
ListAdapter listAdapter = new ListAdapter(sampleList, this);
// 垂直线性布局
// LinearLayoutManager layoutManager = new LinearLayoutManager(null);
// 瀑布流布局
StaggeredGridLayoutManager staggeredGridManager = new StaggeredGridLayoutManager(2, 1);
// 1、设置adapter
recyclerView.setAdapter(listAdapter);
// 2、设置布局
recyclerView.setLayoutManager(staggeredGridManager);
}
private void initSampleList() {
for (int i = 0; i < listSize; i++) {
Sample sample = new Sample();
sample.setIcon(R.drawable.ball);
sample.setTvName("ball: " + i);
sample.setTvContent("ball price: " + i * 100);
sampleList.add(sample);
Sample sample1 = new Sample();
sample1.setIcon(R.drawable.tao);
sample1.setTvName("tao: " + i);
sample1.setTvContent("tao price: " + i * 100);
sampleList.add(sample1);
Sample sample2 = new Sample();
sample2.setIcon(R.drawable.apple);
sample2.setTvName("apple: " + i);
sample2.setTvContent("apple price: " + i * 100);
sampleList.add(sample2);
}
}
}
可以看到,recyclerview使用的关键是设置好对应的adapter。
public class ListAdapter extends RecyclerView.Adapter<ListAdapter.ListHolder> {
List<Sample> sampleList;
Context context;
/**
* 1、定义Adapter首先需要一个ViewHolder
* 2、实现item监听可以放在ViewHolder中实现
*/
static class ListHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
TextView tvName;
TextView tvContent;
ImageView ivIcon;
Context context;
public ListHolder(@NonNull View itemView, Context context) {
super(itemView);
tvName = itemView.findViewById(R.id.list_name);
tvContent = itemView.findViewById(R.id.list_content);
ivIcon = itemView.findViewById(R.id.list_icon);
itemView.setOnClickListener(this);
this.context = context;
}
@Override
public void onClick(View v) {
Toast.makeText(context, getAdapterPosition() + "", Toast.LENGTH_SHORT).show();
}
/**
* 给每个控件设置对应的数据
*/
public void setData(Sample sample) {
tvContent.setText(sample.getTvContent());
tvName.setText(sample.getTvName());
ivIcon.setImageResource(sample.getIcon());
}
}
/**
* 构造函数
*/
public ListAdapter(List<Sample> sampleList, Context context) {
this.sampleList = sampleList;
this.context = context;
}
/**
* ViewHolder 首先用inflate方法解析布局,把整个布局传入,再通过ViewHolder把这个布局里的每个控件设置进来
*/
@NonNull
@Override
public ListHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View sampler = View.inflate(parent.getContext(), R.layout.layout_list,null);
return new ListHolder(sampler, context);
}
@Override
public void onBindViewHolder(@NonNull ListHolder holder, int position) {
holder.setData(sampleList.get(position));
}
@Override
public int getItemCount() {
if (sampleList != null) {
return sampleList.size();
}
return 0;
}
}
adapter中实现item的点击事件,是通过自定义ViewHolder实现的监听事件接口,对比看的话这是一种比较优雅的实现方案。
感谢各位的阅读,以上就是“recyclerview怎么使用”的内容了,经过本文的学习后,相信大家对recyclerview怎么使用这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是亿速云,小编将为大家推送更多相关知识点的文章,欢迎关注!
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。
原文链接:https://blog.51cto.com/baorant24/5717448