Android-RecyclerView系列 Item居中效果

Posted 彭老希

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android-RecyclerView系列 Item居中效果相关的知识,希望对你有一定的参考价值。

一、创建RecyclerView居中管理的Manager

// 默认线性布局
public class CenterLayoutManager extends LinearLayoutManager 

    public CenterLayoutManager(Context context) 
        super(context);
    

    public CenterLayoutManager(Context context, int orientation, boolean reverseLayout) 
        super(context, orientation, reverseLayout);


    

    public CenterLayoutManager(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) 
        super(context, attrs, defStyleAttr, defStyleRes);

    

    @Override
    public void smoothScrollToPosition(RecyclerView recyclerView, RecyclerView.State state, int position) 
        RecyclerView.SmoothScroller smoothScroller = new CenterSmoothScroller(recyclerView.getContext());
        smoothScroller.setTargetPosition(position);
        startSmoothScroll(smoothScroller);
    

    private static class CenterSmoothScroller extends LinearSmoothScroller 

        CenterSmoothScroller(Context context) 
            super(context);
        

        @Override
        public int calculateDtToFit(int viewStart, int viewEnd, int boxStart, int boxEnd, int snapPreference) 
            return (boxStart + (boxEnd - boxStart) / 2) - (viewStart + (viewEnd - viewStart) / 2);
        
    


二、创建Item的Decoration

DeviceUtils 主要使用px2dip进行换算距离,可以将DeviceUtils.px2dip替换成网上任意的工具类换算

public class GalleryItemDecoration extends RecyclerView.ItemDecoration 
    /**
     * 自定义默认的Item的边距
     */
    private int mPageMargin = 10;
    /**
     * 第一张图片的左边距
     */
    private int mLeftPageVisibleWidth;
    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) 

        //计算一下第一中图片距离屏幕左边的距离:(屏幕的宽度-item的宽度)/2。其中item的宽度=实际ImagView的宽度+margin。
        //我这里设置的ImageView的宽度为100+margin=110
        if (mLeftPageVisibleWidth ==0) 
            //计算一次就好了
            mLeftPageVisibleWidth = DeviceUtils.px2dip(view.getContext(),getScreenWidth(view.getContext()) - DeviceUtils.dip2px(view.getContext(), 110)) / 2;

        

        //获取当前Item的position
        int position = parent.getChildAdapterPosition(view);
        //获得Item的数量
        int itemCount = parent.getAdapter().getItemCount();
        int leftMargin;
        if (position == 0)
            leftMargin= dpToPx(mLeftPageVisibleWidth);
        else
            leftMargin=dpToPx(mPageMargin);
        
        int rightMagin;
        if (position == itemCount-1) 
            rightMagin=dpToPx(mLeftPageVisibleWidth);
        else
            rightMagin=dpToPx(mPageMargin);
        
        RecyclerView.LayoutParams layoutParams = (RecyclerView.LayoutParams) view.getLayoutParams();

        //10,10分别是item到上下的margin
        layoutParams.setMargins(leftMargin,10,rightMagin,10);
        view.setLayoutParams(layoutParams);

        super.getItemOffsets(outRect, view, parent, state);


    

    /**
     * d p转换成px
     * @param dp:
     */
    private int dpToPx(int dp)
        return (int) (dp * Resources.getSystem().getDisplayMetrics().density + 0.5f);

    

    /**
     * 获取屏幕的宽度
     * @param context:
     * @return :
     */
    public static int getScreenWidth(Context context) 
        WindowManager manager = (WindowManager) context
                .getSystemService(Context.WINDOW_SERVICE);
        Display display = manager.getDefaultDisplay();
        return display.getWidth();
    



三、具体使用

	// 列表
    @BindView(R.id.rlxxx)
    RecyclerView mRecyclerView;
    private XXXXAdapter mXXXXAdapter ; // 原生Adapter就行
    private LinearSnapHelper mLinearSnapHelper;
    private CenterLayoutManager mCenterLayoutManager;

----------------------------------------------------------------------

	mLinearSnapHelper = new LinearSnapHelper();
    mLinearSnapHelper.attachToRecyclerView(mRecyclerView);
    
    mXXXXAdapter = new XXXXAdapter (addData());
    mCenterLayoutManager =  new CenterLayoutManager(getContext(), LinearLayoutManager.HORIZONTAL, false);
    mRecyclerView.setLayoutManager(mCenterLayoutManager);

    mRecyclerView.setAdapter(mSignInAdapter);
    mRecyclerView.addItemDecoration(new GalleryItemDecoration());

四、XML

        <android.support.v7.widget.RecyclerView
            android:id="@+id/rlxxx"
            android:orientation="horizontal"
            tools:itemCount="7"
            tools:layoutManager="LinearLayoutManager"
            tools:listitem="@layout/item_xxx_layout"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>

以上是关于Android-RecyclerView系列 Item居中效果的主要内容,如果未能解决你的问题,请参考以下文章

Android-RecyclerView系列 Item自动吸顶

Android-RecyclerView系列 RecyclerView滚动指定位置到屏幕中间

Android-RecyclerView系列 notifyItemChanged() - 实现单选选中状态更新

Android-RecyclerView系列 RecyclerView滑动后数据显示错乱

Android-RecyclerView系列 获取可见与不可见的View(亲测有效)

Android-RecyclerView系列 RecyclerView实现Item居中效果