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"/>
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

其子昱舟

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值