RecyclerView可以在xml文件中配置的特别属性
来源:互联网 发布:python正则表达式大全 编辑:程序博客网 时间:2024/05/16 17:41
强大的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.orientation = a.getInt(R.styleable.RecyclerView_android_orientation, VERTICAL); properties.spanCount = a.getInt(R.styleable.RecyclerView_spanCount, 1); properties.reverseLayout = a.getBoolean(R.styleable.RecyclerView_reverseLayout, false); properties.stackFromEnd = a.getBoolean(R.styleable.RecyclerView_stackFromEnd, false); a.recycle(); return properties; }
发现在GridLayoutManager下还能配置
android_orientation 表示布局方向,是横向拖动还是竖向拖动
spanCount 每行或每列的个数,,相当于GridView的numColum
reverseLayout 从后面往前面开始堆放item
stackFromEnd error
LinearLayoutManager能配置的,源码居然一样
android_orientation 方向
stackFromEnd 如果数据摆不完则在前面留空
reverseLayout 从后面往前面开始堆放item
spanCount 无效
可以组合一些不常用的效果.不用在代码里面去实现,易修改和预览效果
RecyclerView的分割线绘制是一个问题,想了一个办法去实现在xml中随意配置分割线,目前只支持绘制颜色
attrs.xml文件中增加.配置的属性如下
<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 = 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); a.recycle(); } @Override public void onDrawOver(Canvas c, RecyclerView parent) { Paint p = new Paint(); p.setColor(mColor); p.setStrokeWidth(thickness); //画最前面的一个头部分割线 if (drawBound && parent.getChildCount() > 0) { View v = parent.getChildAt(0); if (orientation == HORIZONTAL) { //画水平位置的分隔线 c.drawLine(v.getLeft() + padding, v.getTop(), v.getRight() - padding, v.getTop(), p); } if (orientation == VERTICAL) { //画竖直位置的分隔线(GridView) c.drawLine(v.getLeft(), v.getTop()+ padding, v.getLeft(), v.getBottom() - padding, p); } if (orientation == GRID) { for (int i = 0; i < parent.getChildCount(); i += gridNum) { v= parent.getChildAt(i); //都画 c.drawLine(v.getLeft() + padding, v.getTop(), v.getRight() - padding, v.getTop(), p); c.drawLine(v.getLeft(), v.getTop()+ padding, v.getLeft(), v.getBottom() - padding, p); } } } int to = drawBound ? parent.getChildCount() : parent.getChildCount() - 1; for (int i = 0; i < to; i++) { View v = parent.getChildAt(i); if (orientation == 0) { //画水平位置的分隔线 c.drawLine(v.getLeft() + padding, v.getBottom(), v.getRight() - padding, v.getBottom(), p); //画竖直位置的分隔线(GridView) } if (orientation == 1) { c.drawLine(v.getRight(), v.getTop()+ padding, v.getRight(), v.getBottom()- padding, p); } if (orientation == GRID) { c.drawLine(v.getLeft() + padding, v.getBottom(), v.getRight() - padding, v.getBottom(), p); c.drawLine(v.getRight(), 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中配置用法
<com.xxxxx.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"></com.xxxxx.MyRecyclerView>
这里可以实时观看预览效果
当然没有setAdapter,看不到实际item
任何都不配置,就画1px的灰色水平线,不包含头尾
- RecyclerView可以在xml文件中配置的特别属性
- TabActivity可以显示实现多页显示效果,在xml布局文件中需要有一些特殊的配置属性
- Xml文件解析过程中需要特别处理的字符串
- 1、看看你struts.xml文件中action中对应的class属性的值是否可以在Spring的配置文件中找到对应的id值。
- 在自定义控件中读取XML配置属性的值
- 4. 在XML中配置对象的属性
- EditText的InputType属性,可以在代码中设置,也可以预先在xml中定义对应关系
- 解决RecyclerView 在XML文件中,不能preview
- 如何在XML文件中配置servlet的映射文件
- Android .XML 常用文件的配置属性
- 在Spring的xml文件中配置数据源主要代码
- 在tomcat的web.xml文件中配置error-page
- DWR在web.xml中配置的文件
- 在Spring的 applicationContext.xml文件中配置数据源
- 在web.xml 文件中servlet的配置
- 在testng.xml文件中配置TestNG 的依赖关系
- Flex4 的AS文件中定义的属性,可以在mxml中作为自定义标签的属性使用
- ListView中几个特别的属性
- 调用手机震动
- JAVA语言之归并排序
- private,public,protected
- bnu 51644 Whalyzh's Problem(网络流,最大密度图) (北师16校赛)
- 利用Python下的draw_net.py画网络结构图
- RecyclerView可以在xml文件中配置的特别属性
- log4net记录日志,在IIS发布后不记录日志问题解决方案
- Dynamic clock
- dwr多人聊天室实用技术详解(保证成功)
- poj 1066 Treasure Hunt (线段交)
- android 控件 下拉刷新 JellyRefreshLayout
- JMS 都有哪些消息体
- Linux内核学习总结
- ros中使用串口