博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
RecycleView的简单使用
阅读量:6846 次
发布时间:2019-06-26

本文共 6537 字,大约阅读时间需要 21 分钟。

简介

该控件用于在有限的窗口中展示大量数据集,其实这样功能的控件我们并不陌生,例如:ListView、GridView、瀑布流布局。

有了ListView、GridView为什么还需要RecyclerView这样的控件呢?整体上看RecyclerView架构,提供了一种插拔式的体验,高度的解耦,异常的灵活,通过设置它提供的不同LayoutManager,ItemDecoration , ItemAnimator实现酷炫的效果。

提供的几个关键方法有:

  1. setLayoutManager:设置布局方式

  2. setAdapter:设置Item的Adapter

  3. setItemAnimator:设置Item的动画效果

  4. addItemDecoration:分割线的样式

实现ListView功能

public class RecycleActivity extends AppCompatActivity {    @BindView(R.id.recycle_view)    RecyclerView recycleView;    private List
mDatas = new ArrayList<>(); @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_recycle); ButterKnife.bind(this); initData(); recycleView.setAdapter(new MyAdapter(mDatas)); recycleView.setLayoutManager(new LinearLayoutManager(this)); DividerItemDecoration dividerItemDecorationV = new DividerItemDecoration( RecycleActivity.this, DividerItemDecoration.VERTICAL); recycleView.addItemDecoration(dividerItemDecorationV); } private void initData() { for (int i = 0; i < 100; i++) { mDatas.add(String.valueOf(i)); } } private static class MyViewHolder extends RecyclerView.ViewHolder { private TextView tv; public MyViewHolder(View itemView) { super(itemView); tv = (TextView) itemView.findViewById(R.id.holder_view_tv); } } private static class MyAdapter extends RecyclerView.Adapter
{ private List
mDatas; public MyAdapter(List
datas) { mDatas = datas; } @Override public int getItemCount() { return mDatas.size(); } @Override public void onBindViewHolder(MyViewHolder holder, int position) { holder.tv.setText(mDatas.get(position)); } @Override public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.holder_view, parent, false); MyViewHolder myViewHolder = new MyViewHolder(itemView); return myViewHolder; } }}

运行截图

480362-20170504142419914-1957347401.png

实现GridView功能

public class RecycleActivity extends AppCompatActivity {    @BindView(R.id.recycle_view)    RecyclerView recycleView;    private List
mDatas = new ArrayList<>(); @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_recycle); ButterKnife.bind(this); initData(); recycleView.setAdapter(new MyAdapter(mDatas)); recycleView.setLayoutManager(new GridLayoutManager(this, 3)); DividerItemDecoration dividerItemDecorationV = new DividerItemDecoration( RecycleActivity.this, DividerItemDecoration.VERTICAL); DividerItemDecoration dividerItemDecorationH = new DividerItemDecoration( RecycleActivity.this, DividerItemDecoration.HORIZONTAL); recycleView.addItemDecoration(dividerItemDecorationV); recycleView.addItemDecoration(dividerItemDecorationH); } private void initData() { for (int i = 0; i < 100; i++) { mDatas.add(String.valueOf(i)); } } private static class MyViewHolder extends RecyclerView.ViewHolder { private TextView tv; public MyViewHolder(View itemView) { super(itemView); tv = (TextView) itemView.findViewById(R.id.holder_view_tv); } } private static class MyAdapter extends RecyclerView.Adapter
{ private List
mDatas; public MyAdapter(List
datas) { mDatas = datas; } @Override public int getItemCount() { return mDatas.size(); } @Override public void onBindViewHolder(MyViewHolder holder, int position) { holder.tv.setText(mDatas.get(position)); } @Override public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.holder_view, parent, false); MyViewHolder myViewHolder = new MyViewHolder(itemView); return myViewHolder; } }}

运行截图

480362-20170504144853507-1035584709.png

实现瀑布流

public class RecycleActivity extends AppCompatActivity {    @BindView(R.id.recycle_view)    RecyclerView recycleView;    private List
mDatas = new ArrayList<>(); private List
mHeights = new ArrayList<>(); @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_recycle); ButterKnife.bind(this); initData(); recycleView.setAdapter(new MyAdapter(mDatas, mHeights)); StaggeredGridLayoutManager staggeredGridLayoutManager = new StaggeredGridLayoutManager(3, StaggeredGridLayoutManager.VERTICAL); recycleView.setLayoutManager(staggeredGridLayoutManager); } private void initData() { for (int i = 0; i < 100; i++) { mDatas.add(String.valueOf(i)); mHeights.add((int) (100 + Math.random() * 300)); } } private static class MyViewHolder extends RecyclerView.ViewHolder { private TextView tv; public MyViewHolder(View itemView) { super(itemView); tv = (TextView) itemView.findViewById(R.id.holder_view_tv); } } private static class MyAdapter extends RecyclerView.Adapter
{ private List
mDatas; private List
mHeights; public MyAdapter(List
datas, List
heights) { mDatas = datas; mHeights = heights; } @Override public int getItemCount() { return mDatas.size(); } @Override public void onBindViewHolder(MyViewHolder holder, int position) { holder.tv.setText(mDatas.get(position)); ViewGroup.LayoutParams params = holder.tv.getLayoutParams(); params.height = mHeights.get(position); holder.tv.setLayoutParams(params); } @Override public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.holder_view, parent, false); MyViewHolder myViewHolder = new MyViewHolder(itemView); return myViewHolder; } }}

运行截图

480362-20170504154421257-1440130371.png

转载于:https://www.cnblogs.com/jyx140521/p/6806923.html

你可能感兴趣的文章