RecyclerView使用资源文件作为分割线divider
2016-02-25 00:35
399 查看
在listview中有个很好的东西就是
这种比较简单的方案,但是无法满足当前我的需求啊,我特么的就是想要每个控件都有不同的分割线,谁知道脑残的ui和产品想要怎么折腾,这样子统一设置肯定无法满足我的需求啊!!!!
这里的
android:divider,但是recyclerview中取消了这个属性,但是像画个分割线都变得困难,必须要用Decoration类才行。我依旧想用自定义的drawable文件来作为分割线,所以我想到如下方案:
有一种方案是 统一设置分割线
你可以在theme.xml中找到该属性的使用情况。那么,使用系统的listDivider有什么好处呢?就是方便我们去随意的改变,该属性我们可以直接声明在:<!-- Application theme. --> <style name="AppTheme" parent="AppBaseTheme"> <item name="android:listDivider">@drawable/divider_bg</item> </style>
这种比较简单的方案,但是无法满足当前我的需求啊,我特么的就是想要每个控件都有不同的分割线,谁知道脑残的ui和产品想要怎么折腾,这样子统一设置肯定无法满足我的需求啊!!!!
所以,我需要单独设置分割线:
看代码,我是这么使用的:
@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); initData(); //最关键的三行代码 mRecyclerView = (RecyclerView) findViewById(R.id.id_recyclerview); mRecyclerView.setLayoutManager(new LinearLayoutManager(this)); mRecyclerView.setAdapter(mAdapter = new HomeAdapter()); //设置分割线 mRecyclerView.addItemDecoration(new ResourceItemDivider(MyActivity.this,R.color.bl)); }
这里的
R.color.bl我就用了一个color,但如果用自定义的drawable也是没有问题的。
ResourceItemDivider类定义是这么定义的
public class ResourceItemDivider extends RecyclerView.ItemDecoration { private Drawable mDrawable; /** *作为Divider的Drawable对象 * @param context 当前上下文用于获取资源 * @param resId color drawable等类型资源文件 */ public ResourceItemDivider(Context context, int resId) { mDrawable = context.getResources().getDrawable(resId); } public void onDrawOver(Canvas c, RecyclerView parent) { final int left = parent.getPaddingLeft(); final int right = parent.getWidth() - parent.getPaddingRight(); 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.getBottom() + params.bottomMargin; final int bottom = top + mDrawable.getIntrinsicHeight(); mDrawable.setBounds(left, top, right, bottom); mDrawable.draw(c); } } public void getItemOffsets(Rect outRect, int position, RecyclerView parent) { outRect.set(0, 0, 0, mDrawable.getIntrinsicWidth()); }
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- Android IPC进程间通讯机制
- Android Manifest 用法
- [转载]Activity中ConfigChanges属性的用法
- Android之获取手机上的图片和视频缩略图thumbnails
- Android之使用Http协议实现文件上传功能
- Android学习笔记(二九):嵌入浏览器
- android string.xml文件中的整型和string型代替
- i-jetty环境搭配与编译
- android之定时器AlarmManager
- android wifi 无线调试
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- android 代码实现控件之间的间距
- android FragmentPagerAdapter的“标准”配置
- Android"解决"onTouch和onClick的冲突问题
- android:installLocation简析
- android searchView的关闭事件
- SourceProvider.getJniDirectories