RecyclerView轻松实现瀑布流效果
来源:互联网 发布:保安腰刀淘宝店 编辑:程序博客网 时间:2024/04/28 17:28
今天项目中有一个界面需要用到瀑布流效果,经过考虑后决定使用RecyclerView实现。
RecyclerView的Adapter比ListView更加简洁,它封装了ViewHolder的回收利用,编写Adapter面向的ViewHolder而不再是View,代码如下:
public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.MyViewHolder>{ private ArrayList<String> mData; public RecyclerAdapter(ArrayList<String> mData) { this.mData = mData; } @Override public RecyclerAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { MyViewHolder myViewHolder = new MyViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.test_item , parent ,false)); //return一个可供重复利用的ViewHolder return myViewHolder; } @Override public void onBindViewHolder(RecyclerAdapter.MyViewHolder holder, int position) { //对Item中的内容所做操作直接面向ViewHolder holder.tv_tests.setText(mData.get(position)); } @Override public int getItemCount() { //与ListView一样返回数据条数 return mData.size(); }//继承自ViewHolder类 class MyViewHolder extends RecyclerView.ViewHolder{ public TextView tv_tests; public MyViewHolder(View itemView) { super(itemView); tv_tests = (TextView) itemView.findViewById(R.id.tv_test); } }}
RecyclerView并没有为我们提供默认的分割线,如果要想在item之间添加分割线的话,还需要实现ItemDecoration这个抽象类。
public class DividerItemDecoration extends RecyclerView.ItemDecoration { private Drawable mDrawable; public DividerItemDecoration(Context context) { TypedArray typedArray = context.obtainStyledAttributes(new int[]{android.R.attr.listDivider}); mDrawable = typedArray.getDrawable(0); typedArray.recycle(); } public int getSapnCount(RecyclerView parent) { return ((StaggeredGridLayoutManager) parent.getLayoutManager()).getSpanCount(); } @Override public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) { drawHorizontal(c, parent); drawVertical(c, parent); } public void drawHorizontal(Canvas c, RecyclerView parent) { int childCount = parent.getChildCount(); for (int i = 0; i < childCount; i++) { final View child = parent.getChildAt(i); final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child .getLayoutParams(); final int left = child.getLeft() - params.leftMargin; final int right = child.getRight() + params.rightMargin + mDrawable.getIntrinsicWidth(); final int top = child.getBottom() + params.bottomMargin; final int bottom = top + mDrawable.getIntrinsicHeight(); mDrawable.setBounds(left, top, right, bottom); mDrawable.draw(c); } } public void drawVertical(Canvas c, RecyclerView parent) { final int childCount = parent.getChildCount(); for (int i = 0; i < childCount; i++) { final View child = parent.getChildAt(i); final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child .getLayoutParams(); final int top = child.getTop() - params.topMargin; final int bottom = child.getBottom() + params.bottomMargin; final int left = child.getRight() + params.rightMargin; final int right = left + mDrawable.getIntrinsicWidth(); mDrawable.setBounds(left, top, right, bottom); mDrawable.draw(c); } } private boolean isLastColum(RecyclerView parent, int pos, int spanCount, int childCount) { RecyclerView.LayoutManager layoutManager = parent.getLayoutManager(); int orientation = ((StaggeredGridLayoutManager) layoutManager) .getOrientation(); if (orientation == StaggeredGridLayoutManager.VERTICAL) { if ((pos + 1) % spanCount == 0)// 如果是最后一列,则不需要绘制右边 { return true; } } else { childCount = childCount - childCount % spanCount; if (pos >= childCount)// 如果是最后一列,则不需要绘制右边 return true; } return false; } private boolean isLastRaw(RecyclerView parent, int pos, int spanCount, int childCount) { RecyclerView.LayoutManager layoutManager = parent.getLayoutManager(); int orientation = ((StaggeredGridLayoutManager) layoutManager) .getOrientation(); // StaggeredGridLayoutManager 且纵向滚动 if (orientation == StaggeredGridLayoutManager.VERTICAL) { childCount = childCount - childCount % spanCount; // 如果是最后一行,则不需要绘制底部 if (pos >= childCount) return true; } else // StaggeredGridLayoutManager 且横向滚动 { // 如果是最后一行,则不需要绘制底部 if ((pos + 1) % spanCount == 0) { return true; } } return false; } @Override public void getItemOffsets(Rect outRect, int itemPosition, RecyclerView parent) { int spanCount = getSapnCount(parent); int childCount = parent.getAdapter().getItemCount(); if (isLastRaw(parent, itemPosition, spanCount, childCount))// 如果是最后一行,则不需要绘制底部 { outRect.set(0, 0, mDrawable.getIntrinsicWidth(), 0); } else if (isLastColum(parent, itemPosition, spanCount, childCount))// 如果是最后一列,则不需要绘制右边 { outRect.set(0, 0, 0, mDrawable.getIntrinsicHeight()); } else { outRect.set(0, 0, mDrawable.getIntrinsicWidth(), mDrawable.getIntrinsicHeight()); } }}
RecyclerView.LayoutManager是一个抽象类,系统为我们提供了三个实现类
①LinearLayoutManager即线性布局,这个是在上面的例子中我们用到的布局
②GridLayoutManager即表格布局
③StaggeredGridLayoutManager即流式布局,如瀑布流效果
这里我们直接使用StaggeredGridLayoutManager就可以了
recyclerView = (RecyclerView)findViewById(R.id.rv_test); staggeredGridLayoutManager = new StaggeredGridLayoutManager(2 , StaggeredGridLayoutManager.VERTICAL);// //设置瀑布流布局 recyclerView.setLayoutManager(staggeredGridLayoutManager);// //设置适配器 recyclerView.setAdapter(new RecyclerAdapter(mData)); //添加分割线 recyclerView.addItemDecoration(new DividerItemDecoration(this));
到这里瀑布流效果就实现了
源码地址:https://github.com/GameT/RecyclerViewFlow
0 0
- RecyclerView轻松实现瀑布流效果
- RecyclerView 实现瀑布流效果
- RecyclerView实现瀑布流效果
- RecyclerView--实现 ListView,GridView,瀑布流 效果
- RecyclerView实现瀑布流效果(二)
- 利用RecyclerView实现瀑布流效果
- Android RecyclerView 实现瀑布流效果
- 使用RecyclerView实现瀑布流效果
- RecyclerView实现瀑布流的效果
- Recyclerview 瀑布流效果
- RecyclerView实现瀑布流
- recyclerView实现瀑布流
- RecyclerView实现瀑布流效果(图文详解+源码奉送)
- RecyclerView介绍(四)----实现瀑布流效果
- android RecyclerView 实现瀑布流带图库浏览效果
- Android开发之实现瀑布流效果(RecyclerView)
- Android中利用RecyclerView实现瀑布流效果
- RecyclerView的简单使用以及实现瀑布流效果
- JavaWeb模拟键盘
- Scala基础
- postgresql 关于geo 按距离排序返回 性能测试
- 1099. Build A Binary Search Tree (30)
- js闭包实现私有变量和单例模式
- RecyclerView轻松实现瀑布流效果
- 最全的常用正则表达式大全
- Java变量以及内存分配(非常重要)
- 文章标题
- Java基础 - 排序二叉树(二叉查找树)
- kail linux 安装及更新
- html基础-css样式设计
- 51nod 1464 半回文
- 1050. String Subtraction (20)