RecyclerView用法

1、添加依赖

在AS的build.gradle中添加依赖,然后同步一下就可以引入依赖包:

dependencies {    compile fileTree(dir: 'libs', include: ['*.jar'])    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {        exclude group: 'com.android.support', module: 'support-annotations'    })    compile 'com.android.support:appcompat-v7:25.1.0'    compile 'com.android.support:recyclerview-v7:25.1.0'    testCompile 'junit:junit:4.12'}

2、在xml布局文件中创建一个RecyclerView的布局

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:id="@+id/activity_main"    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:context="com.john.rvspacing.MainActivity">    <android.support.v7.widget.RecyclerView        android:id="@+id/grid_rv"        android:layout_width="match_parent"        android:layout_height="match_parent"/>RelativeLayout>

RecyclerView自定义行列间距

1、设置宫格布局、瀑布流布局间距

/** * 设置RecyclerView GridLayoutManager or StaggeredGridLayoutManager spacing * Created by john on 17-1-5. */public class GridSpacingItemDecoration extends RecyclerView.ItemDecoration {    private int spanCount;    private int spacing;    private boolean includeEdge;    public GridSpacingItemDecoration(int spanCount, int spacing, boolean includeEdge) {        this.spanCount = spanCount;        this.spacing = spacing;        this.includeEdge = includeEdge;    }    @Override    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {        int position = parent.getChildAdapterPosition(view); // item position        int column = position % spanCount; // item column        if (includeEdge) {            outRect.left = spacing - column * spacing / spanCount; // spacing - column * ((1f / spanCount) * spacing)            outRect.right = (column + 1) * spacing / spanCount; // (column + 1) * ((1f / spanCount) * spacing)            if (position < spanCount) { // top edge                outRect.top = spacing;            }            outRect.bottom = spacing; // item bottom        } else {            outRect.left = column * spacing / spanCount; // column * ((1f / spanCount) * spacing)            outRect.right = spacing - (column + 1) * spacing / spanCount; // spacing - (column + 1) * ((1f /    spanCount) * spacing)            if (position >= spanCount) {                outRect.top = spacing; // item top            }        }    }}

2、正确使用姿势

GridLayoutManager layoutManager = new GridLayoutManager(this, COLUMN, GridLayoutManager.VERTICAL, false);mGridRv.setLayoutManager(layoutManager);mGridRv.addItemDecoration(new GridSpacingItemDecoration(COLUMN, getResources().getDimensionPixelSize(R.dimen.padding_middle), true));mGridRv.setHasFixedSize(true);mRvGridAdapter = new RvGridAdapter(this);mRvGridAdapter.setItemList(DataMock.mockItemBean());mGridRv.setAdapter(mRvGridAdapter);

3、设置线性布局间距

/** * 设置RecyclerView LinearLayoutManager spacing * Created by john on 17-1-5. */public class SpacesItemDecoration extends RecyclerView.ItemDecoration {    private int space;    public SpacesItemDecoration(int space) {        this.space = space;    }    @Override    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {        outRect.left = space;        outRect.right = space;        outRect.bottom = space;        // Add top margin only for the first item to avoid double space between items        if (parent.getChildLayoutPosition(view) == 0) {            outRect.top = space;        } else {            outRect.top = 0;        }    }}

4、Demo效果


 
5、点击下载示例源码

更多相关文章

  1. 4.4以上android沉浸式
  2. Android底部导航栏最常用的两种写法
  3. Android(安卓)创建复合控件
  4. android   动态改变图片大小
  5. android 代码控制LinearLayout 宽度高度 报错widget.LinearLayou
  6. android使用activity切换动画效果
  7. android camer 图片回显界面照片分享到微博、人人、彩信、蓝牙的
  8. 对RecycleView的多种item布局的封装
  9. ListView嵌套ListView时发生:View too large to fit into drawing

随机推荐

  1. jquery自定义事件
  2. Symfony 2在用户站点上动态添加字段以形
  3. 如何用jQuery获取django的HttpResponse状
  4. jQuery学习笔记- focus和blur事件妙用
  5. jQuery取得select选中的值
  6. 具有嵌套元素的jQuery悬停事件
  7. jQuery插件实现网页底部自动加载-类似新
  8. 将参数传递给jQuery的select2 ajax调用
  9. 使用HTML5验证时如何绑定到提交事件?
  10. 将数据从jQuery Ajax发送到MVC COntrolle