美文网首页
工具类二、RecyclerView常用设置

工具类二、RecyclerView常用设置

作者: 让你独一无二 | 来源:发表于2016-09-13 12:04 被阅读0次
    package com.johnwharton.quanmintv.utils;
    
    import android.content.Context;
    import android.support.v7.widget.DefaultItemAnimator;
    import android.support.v7.widget.GridLayoutManager;
    import android.support.v7.widget.LinearLayoutManager;
    import android.support.v7.widget.RecyclerView;
    
    import com.johnwharton.quanmintv.decoration.SpacesItemDecoration;
    
    /**
     * Created by My on 2016/9/8.
     */
    public class RecyclerViewSettingsUtil {
        public static final int HORIZONTAL = 0;
        public static final int VERTICAL = 1;
        //分割线的类型,1线性  2 间隙
        public static final int DECORATION_TYPE_1 = 0;
        public static final int DECORATION_TYPE_2 = 1;
    
        //线性布局固定的间隙
        public static void setHorVerOrientationRecyclerView(Context context, RecyclerView recyclerView, int orientation) {
            recyclerView.setHasFixedSize(true);
            //设置动画
            recyclerView.setItemAnimator(new DefaultItemAnimator());
            //设置分割线
            recyclerView.addItemDecoration(new SpacesItemDecoration(4));
            LinearLayoutManager manager = null;
            switch (orientation) {
                case HORIZONTAL:
                    manager = new LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false);
                    break;
                case VERTICAL:
                    manager = new LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false);
                    break;
            }
            recyclerView.setLayoutManager(manager);
        }
    
    
        //线性布局可定制的的间隙
        public static void setHorVerDefSpaceRecyclerView(Context context, RecyclerView recyclerView, int orientation,int space) {
            recyclerView.setHasFixedSize(true);
            //设置动画
            recyclerView.setItemAnimator(new DefaultItemAnimator());
            //设置分割线
            recyclerView.addItemDecoration(new SpacesItemDecoration(space));
            LinearLayoutManager manager = null;
            switch (orientation) {
                case HORIZONTAL:
                    manager = new LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false);
                    break;
                case VERTICAL:
                    manager = new LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false);
                    break;
            }
            recyclerView.setLayoutManager(manager);
        }
    
        /**
         * 上下文环境
         *
         * @param context
         * @param recyclerView
         * @param spanCount    跨的列数目
         */
        //网格布局
        public static void setGridRecyclerView(Context context, RecyclerView recyclerView, int spanCount) {
            recyclerView.setHasFixedSize(true);
            //设置动画
            recyclerView.setItemAnimator(new DefaultItemAnimator());
            //设置分割线
            recyclerView.addItemDecoration(new SpacesItemDecoration(4));
            GridLayoutManager manager = new GridLayoutManager(context, spanCount);
            //设置布局管理器
            recyclerView.setLayoutManager(manager);
        }
    
    
        /**
         * 网格布局,传入GridLayoutManager
         * @param recyclerView
         * @param manager
         */
        public static void setGridRecyclerView(RecyclerView recyclerView, GridLayoutManager manager) {
            recyclerView.setHasFixedSize(true);
            //设置动画
            recyclerView.setItemAnimator(new DefaultItemAnimator());
            //设置分割线
            recyclerView.addItemDecoration(new SpacesItemDecoration(4));
            //设置布局管理器
            recyclerView.setLayoutManager(manager);
        }
    }
    
    
    
    
    
    
    
    
    

    相关文章

      网友评论

          本文标题:工具类二、RecyclerView常用设置

          本文链接:https://www.haomeiwen.com/subject/xkymettx.html