RecyclerView可以在xml文件中配置的特别属性

更新时间:2023-06-23 10:58:27 阅读: 评论:0

RecyclerView可以在xml⽂件中配置的特别属性强⼤的RecyclerView,从他的源码中发现好像只可以配置⼀个属性layoutManager,例如
app:layoutManager="GridLayoutManager"
有了这个我们不⽤在代码中控制布局⽅向横向ListView,还是竖向ListView,还是GridView了
配置了GridLayoutManager这个属性,我们翻翻他的源码,找到这个⽅法:
public static Properties getProperties(Context context, AttributeSet attrs,
int defStyleAttr, int defStyleRes) {
Properties properties = new Properties();
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.RecyclerView,
defStyleAttr, defStyleRes);
properties.spanCount = a.getInt(R.styleable.RecyclerView_spanCount, 1);
properties.stackFromEnd = a.getBoolean(R.styleable.RecyclerView_stackFromEnd, fal);
return properties;
}
发现在GridLayoutManager下还能配置
android_orientation 表⽰布局⽅向,是横向拖动还是竖向拖动
spanCount 每⾏或每列的个数,,相当于GridView的numColum
reverLayout 从后⾯往前⾯开始堆放item
stackFromEnd error
LinearLayoutManager能配置的,源码居然⼀样
android_orientation ⽅向
stackFromEnd 如果数据摆不完则在前⾯留空
reverLayout 从后⾯往前⾯开始堆放item
spanCount ⽆效
可以组合⼀些不常⽤的效果.不⽤在代码⾥⾯去实现,易修改和预览效果
RecyclerView的分割线绘制是⼀个问题,想了⼀个办法去实现在xml中随意配置分割线,⽬前只⽀持绘制颜⾊ l⽂件中增加.配置的属性如下
<declare-styleable name="divder">
<attr name="_drawBound"format="boolean" />
<attr name="_thickness"format="dimension" />
强吻女孩子<attr name="_dividerPadding"format="dimension" />
<attr name="_dividerColor"format="color" />
<attr name="_orientation"format="enum">
<enum name="horizontal"value="0" />
<enum name="vertical"value="1" />
<enum name="grid"value="2" />
</attr>
小生这厢有礼了
<attr name="_grid_col_num"format="integer" />
</declare-styleable>
空对月
_drawBound———是否画边界,作⽤于list时表⽰是否画头和尾
雷锋读后感_thickness————分割线的粗细
深深眷恋
_dividerPadding—-分割线两端留⽩
_dividerColor——–颜⾊
_orientation ———⽅向,为grid则会横竖都画
天蝎座射手座实现⼀个从xml⽂件中读取分割线配置⽅式的类
public class DividerLine extends RecyclerView.ItemDecoration {
private int mColor = Color.GRAY;
private int thickness = DEFAULT_THICKNESS;
private int padding;
private static final int DEFAULT_THICKNESS = 1;
private int orientation = HORIZONTAL;
public static final int HORIZONTAL = 0;
public static final int VERTICAL = 1;
public static final int GRID = 2;
private boolean drawBound = fal;
private int gridNum = 1;
public DividerLine(Context context, AttributeSet attrs) {
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.divder);
mColor = a.getColor(R.styleable.divder__dividerColor, mColor);
padding = (int) a.getDimension(R.styleable.divder__dividerPadding, padding);
thickness = (int) a.getDimension(R.styleable.divder__thickness, thickness);
orientation = a.getInt(R.styleable.divder__orientation, 0);
drawBound = a.getBoolean(R.styleable.divder__drawBound, fal);
gridNum = a.getInt(R.styleable.divder__grid_col_num, 1);
}
@Override
public void onDrawOver(Canvas c, RecyclerView parent) {
Paint p = new Paint();
抓住用英语怎么说p.tColor(mColor);
p.tStrokeWidth(thickness);
//画最前⾯的⼀个头部分割线
if (drawBound && ChildCount() > 0) {
View v = ChildAt(0);
if (orientation == HORIZONTAL) {
//画⽔平位置的分隔线
c.Left() + padding, v.getTop(), v.getRight() - padding, v.getTop(), p);
}
秘制烤鱼
if (orientation == VERTICAL) {
//画竖直位置的分隔线(GridView)
c.Left(), v.getTop()+ padding, v.getLeft(), v.getBottom() - padding, p);
}
if (orientation == GRID) {
for (int i = 0; i < ChildCount(); i += gridNum) {
v= ChildAt(i);
//都画
c.Left() + padding, v.getTop(), v.getRight() - padding, v.getTop(), p);
c.Left(), v.getTop()+ padding, v.getLeft(), v.getBottom() - padding, p);                }
}
}
int to = drawBound ? ChildCount() : ChildCount() - 1;
for (int i = 0; i < to; i++) {
View v = ChildAt(i);
if (orientation == 0) {
//画⽔平位置的分隔线
c.Left() + padding, v.getBottom(), v.getRight() - padding, v.getBottom(), p);
//画竖直位置的分隔线(GridView)
}
if (orientation == 1) {
c.Right(), v.getTop()+ padding, v.getRight(), v.getBottom()- padding, p);            }
}
if (orientation == GRID) {
c.Left() + padding, v.getBottom(), v.getRight() - padding, v.getBottom(), p);
c.Right(),  v.getTop()+ padding, v.getRight(), v.getBottom()- padding, p);
}
}
}
}
继承RecyclerView
需要⾃定义View,主动实现添加分割线的⽅法
public class MyRecyclerView extends RecyclerView{
public MyRecyclerView(Context context) {
super(context);
}
public MyRecyclerView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
addItemDecoration(new DividerLine(context,attrs));
}
}
在xml中配置⽤法
&MyRecyclerView
android:id="@+id/rv_home_news"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="@dimen/_18dp"
android:background="@color/background"
android:padding="@dimen/_8dp"
app:layoutManager="GridLayoutManager"
app:spanCount="3"
divider:_dividerColor="@color/dialog_red"
divider:_drawBound="true"
divider:_dividerPadding="@dimen/_0dp"
divider:_orientation="grid"
divider:_thickness="1px">
</MyRecyclerView>
这⾥可以实时观看预览效果
当然没有tAdapter,看不到实际item
任何都不配置,就画1px的灰⾊⽔平线,不包含头尾

本文发布于:2023-06-23 10:58:27,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/fan/82/1020595.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:配置   实现   分割线   属性   是否
相关文章
留言与评论(共有 0 条评论)
   
验证码:
推荐文章
排行榜
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图