Android RecyclerView实现水平、垂直方向分割线
程序员文章站
2022-04-13 22:46:29
android recyclerview不像过去的listview那样随意的设置水平方向的分割线,如果要实现recyclerview的水平/垂直分割线,则需要继承自recy...
android recyclerview不像过去的listview那样随意的设置水平方向的分割线,如果要实现recyclerview的水平/垂直分割线,则需要继承自recyclerview.itemdecoration重写getitemoffsets方法,从而增加水平/垂直分割线。
写一个例子。
mainactivity.java:
package zhangphil.app; import android.content.context; import android.graphics.color; import android.support.v7.app.appcompatactivity; import android.os.bundle; import android.support.v7.widget.recyclerview; import android.support.v7.widget.staggeredgridlayoutmanager; import android.view.view; import android.view.viewgroup; import android.widget.textview; public class mainactivity extends appcompatactivity { @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); recyclerview mrecyclerview = (recyclerview) findviewbyid(r.id.recyclerview); // 两列 int spancount = 2; // staggeredgridlayoutmanager管理recyclerview的布局。 staggeredgridlayoutmanager mlayoutmanager = new staggeredgridlayoutmanager(spancount, staggeredgridlayoutmanager.vertical); mrecyclerview.setlayoutmanager(mlayoutmanager); //为recyclerview增加分割线,水平和垂直方向都有。增加分割线值比如为32。 recyclerviewitemdecoration decoration = new recyclerviewitemdecoration(32); mrecyclerview.additemdecoration(decoration); recyclerviewadapter madapter = new recyclerviewadapter(this); mrecyclerview.setadapter(madapter); } private class itemviewholder extends recyclerview.viewholder { private textview text; public itemviewholder(view itemview) { super(itemview); text = (textview) itemview.findviewbyid(android.r.id.text1); text.settextcolor(color.white); } } public class recyclerviewadapter extends recyclerview.adapter<itemviewholder> { private context context; public recyclerviewadapter(context context) { super(); this.context = context; } @override public itemviewholder oncreateviewholder(viewgroup viewgroup, int viewtype) { view view = view.inflate(context, android.r.layout.simple_list_item_1, null); view.setbackgroundcolor(color.red); itemviewholder holder = new itemviewholder(view); return holder; } @override public void onbindviewholder(itemviewholder viewholder, int pos) { viewholder.text.settext(string.valueof(pos)); } @override public int getitemcount() { return 15; } } }
布局文件,很简单,就放一个recyclerview,注意背景颜色的设置:
<?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@android:color/holo_orange_light"> <android.support.v7.widget.recyclerview android:id="@+id/recyclerview" android:layout_width="match_parent" android:layout_height="match_parent"> </android.support.v7.widget.recyclerview> </relativelayout>
最关键的recyclerviewitemdecoration.java:
package zhangphil.app; /** * created by phil on 2016/10/8. */ import android.graphics.rect; import android.support.v7.widget.recyclerview; import android.view.view; /** * 为recyclerview增加间距 * 预设2列,如果是3列,则左右值不同 */ public class recyclerviewitemdecoration extends recyclerview.itemdecoration { private int space = 0; private int pos; public recyclerviewitemdecoration(int space) { this.space = space; } @override public void getitemoffsets(rect outrect, view view, recyclerview parent, recyclerview.state state) { outrect.top = space; //该view在整个recyclerview中位置。 pos = parent.getchildadapterposition(view); //取模 //两列的左边一列 if (pos % 2 == 0) { outrect.left = space; outrect.right = space / 2; } //两列的右边一列 if (pos % 2 == 1) { outrect.left = space / 2; outrect.right = space; } } }
代码运行结果:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
上一篇: 吵架分手后男生拉黑我是什么心态
推荐阅读
-
android中RecyclerView自定义分割线实现
-
Android实现RecyclerView添加分割线的简便方法
-
Android Recyclerview实现水平分页GridView效果示例
-
Android使用Recyclerview实现图片水平自动循环滚动效果
-
android中RecyclerView自定义分割线实现
-
Android实现RecyclerView添加分割线的简便方法
-
Android RecyclerView实现水平、垂直方向分割线
-
有焦点 item 的水平/垂直滚动 RecyclerView-LayoutManager。仿 Android 豆瓣书影音“推荐“频道列表布局
-
Android RecyclerView实现水平、垂直方向分割线
-
Android自定义分割线_水平及垂直分割线的两种实现方法