Y_DividerItemDecoration

Introduction: 一个通用的 RecyclerView 的分割线,支持 LinearLayoutManager 和 GridViewLayoutManager
More: Author   ReportBugs   DemoAPK   
Tags:
recyclerview-divider-itemdecoration-分割线-

一个通用的 RecyclerView 的分割线,支持 LinearLayoutManager 和 GridLayoutManager。只要你能描述清楚每个 position 上下左右分割线的情况,原则上支持任意 LayoutManager

图 1 LinearLayoutManager 图 2 大分割线 GridViewLayoutManager 图 2 普通 GridViewLayoutManager 图 3 异型 GridViewLayoutManager

Features

  • LinearLayoutManager 和 GridViewLayoutManager 通用
  • 以每个 item 的上、下、左、右每条边为控制单位
  • 可以单独控制每边是否显示
  • 可以单独控制每边的颜色
  • 可以单独控制每边的宽度
  • 可以单独控制每边的 startPadding 和 endPadding

Usage

Step 1

将依赖加入你项目的 build.gradle 中:

compile 'com.yanyusong.y_divideritemdecoration:y_divideritemdecoration:2.0'

Step 2

新建一个对象实现抽象类 Y_DividerItemDecoration ,在 getDivider(int itemPosition) 中定义在 itemPosition 时条目的分割线情况,有无、颜色、宽度、startpadding 和 endpadding 。

    private class DividerItemDecoration extends Y_DividerItemDecoration {

        private DividerItemDecoration(Context context) {
            super(context);
        }

        @Override
        public Y_Divider getDivider(int itemPosition) {
            Y_Divider divider = null;
            switch (itemPosition % 2) {
                case 0:
                    //每一行第一个显示 rignt 和 bottom
                    divider = new Y_DividerBuilder()
                            .setRightSideLine(true, 0xff666666, 10, 0, 0)
                            .setBottomSideLine(true, 0xff666666, 20, 0, 0)
                            .create();
                    break;
                case 1:
                    //第二个显示 Left 和 bottom
                    divider = new Y_DividerBuilder()
                            .setLeftSideLine(true, 0xff666666, 10, 0, 0)
                            .setBottomSideLine(true, 0xff666666, 20, 0, 0)
                            .create();
                    break;
                default:
                    break;
            }
            return divider;
        }
    }

Step 3

给 RecyclerView 添加刚才实现的 ItemDecoration

 recyclerView.addItemDecoration(new DividerItemDecoration(this));

License

   Copyright  2017  yanyusong

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
Support Me
Apps
About Me
Google+: Trinea trinea
GitHub: Trinea