可以自定义滑动速度,在滚动以后,可以自动居中显示的RecycleView

package com.trs.v7.home.view;

import android.content.Context;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

/**
 * <pre>
 * Created by zhuguohui
 * Date: 2023/4/10
 * Time: 11:09
 * Desc:可以自定义滑动速度的RecycleView
 * 在滚动以后,可以自动居中显示。
 * 通过
 * {@link #setFlingScale(double)}来设置速度因子
 * 1.0表示原速度.
 * </pre>
 */
public class CustomFlingSpeedRecyclerView extends RecyclerView {
    private double scale; //抛掷速度的缩放因子

    public CustomFlingSpeedRecyclerView(Context context) {
        this(context,null);
    }

    public CustomFlingSpeedRecyclerView(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public CustomFlingSpeedRecyclerView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init();
    }

    private void init() {
        addOnScrollListener(new OnScrollListener() {

            @Override
            public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
                if(newState== SCROLL_STATE_IDLE){
                    View child= getNeedShowChild(recyclerView);
                    if(child==null){
                        return;
                    }
                    int middleLeft=(recyclerView.getWidth()-child.getWidth())/2;
                    boolean isMiddle=child.getLeft()==middleLeft;
                    Rect rect=new Rect();
                    child.getGlobalVisibleRect(rect);
                    if(!recyclerView.canScrollHorizontally(-1)){//已经滑动到最左直接跳到最前端
                        recyclerView.smoothScrollToPosition(0);
                        return;
                    }
                    if(!isMiddle){
                        int mX = middleLeft - child.getLeft();
                        if(recyclerView.canScrollHorizontally(-mX)) {
                            recyclerView.smoothScrollBy(-mX, 0);
                        }
                    }
                }
            }

            private View getNeedShowChild(RecyclerView recyclerView) {
                //获取最大可见度的view。然后让其居中
                View showChild=null;
                float showRatio=0.0f;
                int childCount = recyclerView.getChildCount();
                for(int i=0;i<childCount;i++){
                    Rect rect=new Rect();
                    View childAt = recyclerView.getChildAt(i);
                    childAt.getGlobalVisibleRect(rect);
                    float ratio = rect.width()*1.0f / childAt.getWidth();
                    if(ratio>=showRatio){
                        showRatio=ratio;
                        showChild=childAt;
                    }
                }
                return showChild;
            }

            @Override
            public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
            }
        });
    }

    public void setFlingScale(double scale){
        this.scale = scale;
    }

    @Override
    public boolean fling(int velocityX, int velocityY) {
        velocityX *= scale;
        return super.fling(velocityX, velocityY);
    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要将RecyclerView的item居中显示,你可以使用LayoutManager来实现。以下是一种常见的方法: 1. 首先,在你的RecyclerView的布局文件中,确保为RecyclerView设置了固定的宽度和高度,以便居中显示。 2. 在你的代码中,为RecyclerView设置布局管理器LayoutManager。可以使用LinearLayoutManager或GridLayoutManager。 - LinearLayoutManager示例: ```java LinearLayoutManager layoutManager = new LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false); recyclerView.setLayoutManager(layoutManager); ``` - GridLayoutManager示例: ```java GridLayoutManager layoutManager = new GridLayoutManager(context, spanCount); layoutManager.setOrientation(RecyclerView.HORIZONTAL); // 如果是水平方向 recyclerView.setLayoutManager(layoutManager); ``` 3. 确保RecyclerView的item布局中的根布局设置了合适的宽度和高度,以便居中显示。 - 如果使用LinearLayoutManager,可以在item布局的根布局上添加以下属性: ```xml android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" ``` - 如果使用GridLayoutManager,可以在item布局的根布局上添加以下属性: ```xml android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" ``` 这样设置后,RecyclerView的item就会居中显示了。你可以根据实际需求选择适合的布局管理器和设置适当的宽高属性来实现居中显示效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值