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, false);
return properties;
}
发现在GridLayoutManager下还能配置
android_orientation 表⽰布局⽅向,是横向拖动还是竖向拖动
spanCount 每⾏或每列的个数,,相当于GridView的numColum
reverseLayout 从后⾯往前⾯开始堆放item
stackFromEnd error
LinearLayoutManager能配置的,源码居然⼀样
android_orientation ⽅向
stackFromEnd 如果数据摆不完则在前⾯留空
reverseLayout 从后⾯往前⾯开始堆放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" />
索尼xml文件可以删除吗
</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 = false;
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, false);
gridNum = a.getInt(R.styleable.divder__grid_col_num, 1);
}
@Override
public void onDrawOver(Canvas c, RecyclerView parent) {
Paint p = new Paint();
p.setColor(mColor);
p.setStrokeWidth(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>
这⾥可以实时观看预览效果
当然没有setAdapter,看不到实际item
任何都不配置,就画1px的灰⾊⽔平线,不包含头尾

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