栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Android-RecyclerView系列 Item居中效果

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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 {
    
    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);


    }

    
    private int dpToPx(int dp){
        return (int) (dp * Resources.getSystem().getDisplayMetrics().density + 0.5f);

    }

    
    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
        
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/821545.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号