recyclerview 横向滑动 缩放动画工具类

public class CardScaleHelper {
    private RecyclerView mRecyclerView;
    private Context mContext;

    private float mScale = 0.8f; // 两边卡片的缩放量


    private int mCardWidth; // 卡片宽度

    private int mCurrentItemPos;//卡片当前位置

    private int mCurrentItemOffset;//当前偏移量

    private PagerSnapHelper pagerSnapHelper = new PagerSnapHelper();

    public void attachToRecyclerView(final RecyclerView mRecyclerView) {
        this.mRecyclerView = mRecyclerView;
        mContext = mRecyclerView.getContext();
        mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);

                // dx>0则表示右滑, dx<0表示左滑, dy<0表示上滑, dy>0表示下滑
                if(dx != 0){
                    mCurrentItemOffset += dx;

                    //计算当前位置
                    mCurrentItemPos = Math.round(mCurrentItemOffset * 1.0f / mCardWidth);

                    changedCardSize();
                }
            }
        });

        mRecyclerView.post(new Runnable() {
            @Override
            public void run() {
                //求出卡片宽度
                mCardWidth = mRecyclerView.getWidth() - DisplayUtil.dip2px(mContext, 2 * (CardScaleConstant.PAGER_PADDING + CardScaleConstant.PAGER_MARGIN));
                mRecyclerView.smoothScrollToPosition(mCurrentItemPos);
                changedCardSize();
            }
        });

        pagerSnapHelper.attachToRecyclerView(mRecyclerView);
    }


    public void setCurrentItemPos(int currentItemPos) {
        this.mCurrentItemPos = currentItemPos;
    }

    public int getCurrentItemPos() {
        return mCurrentItemPos;
    }

    /**
     * 改变卡片大小
     */
    private void changedCardSize() {

        //求出当前Item滑动偏移量,它的绝对值的取值变化时从小到大再变小
        //变化范围是[0,mCardWidth/2],[-mCardWidth/2,0]
        int offset = mCurrentItemOffset - mCurrentItemPos * mCardWidth;
        //求出缩放百分比,最小值为0.0001,最大值为1/2,百分比的变化是从小变大,再变小
        float percent = (float) Math.max(Math.abs(offset) * 1.0 / mCardWidth, 0.0001);

        View leftView = null;
        View currentView;
        View rightView = null;
        if (mCurrentItemPos > 0) {
            leftView = mRecyclerView.getLayoutManager().findViewByPosition(mCurrentItemPos - 1);
        }
        currentView = mRecyclerView.getLayoutManager().findViewByPosition(mCurrentItemPos);
        if (mCurrentItemPos < mRecyclerView.getAdapter().getItemCount() - 1) {
            rightView = mRecyclerView.getLayoutManager().findViewByPosition(mCurrentItemPos + 1);
        }

        if (leftView != null) {
            leftView.setScaleY((1 - mScale) * percent + mScale);
        }
        if (currentView != null) {
            currentView.setScaleY((mScale - 1) * percent + 1);
        }
        if (rightView != null) {

        }
    }

    public void setScale(float scale) {
        mScale = scale;
    }

    public void setPagePadding(int pagePadding) {
        CardScaleConstant.PAGER_PADDING = pagePadding;
    }

    public void setPageMarin(int pagerMargin) {
        CardScaleConstant.PAGER_MARGIN = pagerMargin;
    }
}
public class CardScaleConstant {

    public static int PAGER_PADDING = 25;

    public static int PAGER_MARGIN = 25;
}

使用

        CardScaleHelper cardScaleHelper = new CardScaleHelper();
        cardScaleHelper.setCurrentItemPos(2);
        cardScaleHelper.attachToRecyclerView(vipListRv);
RecyclerView 是 Android 开发中常用的列表控件,它可以用于展示大量数据,并且支持灵活的布局和交互方式。要实现 RecyclerView横向滑动,可以通过设置 RecyclerView 的布局管理器来实现。 首先,需要在布局文件中添加 RecyclerView 控件: ```xml <androidx.recyclerview.widget.RecyclerView android:id="@+id/recyclerView" android:layout_width="match_parent" android:layout_height="wrap_content" /> ``` 接下来,在代码中找到 RecyclerView 控件,并设置其布局管理器为 LinearLayoutManager,并指定滑动方向为横向: ```java RecyclerView recyclerView = findViewById(R.id.recyclerView); LinearLayoutManager layoutManager = new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false); recyclerView.setLayoutManager(layoutManager); ``` 然后,创建适配器并将其设置给 RecyclerView: ```java RecyclerViewAdapter adapter = new RecyclerViewAdapter(dataList); // dataList 是你的数据集合 recyclerView.setAdapter(adapter); ``` 最后,根据需要可以添加滑动效果或者监听滑动事件: ```java // 添加滑动效果 SnapHelper snapHelper = new LinearSnapHelper(); snapHelper.attachToRecyclerView(recyclerView); // 监听滑动事件 recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() { @Override public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) { super.onScrolled(recyclerView, dx, dy); // 在这里处理滑动事件 } }); ``` 这样就实现了 RecyclerView横向滑动效果。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值