做一个RecyclerView 滑动的效果

2020-03-20  本文已影响0人  向前的zz

实现效果


1.gif

做一个这种的


2.gif

代码如下:

/**
 * @author zhousaito
 * @version 1.0
 * @date 2020/3/20 07:41
 * @Dec 略
 */
public class MyRecyclerView extends RecyclerView {
    public static final String TAG = "MyRecyclerView";
    private final int dp120;

    private boolean isUpToDown;

    private boolean isAutoScrolling;
    private boolean isDownToUp;

    private int currentScroll;

    public MyRecyclerView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        dp120 = UiUtils.dp2px(context, 120);
    }

    @Override
    public void onScrollStateChanged(int state) {
        super.onScrollStateChanged(state);
//        Log.e(TAG, "onScrollStateChanged " + state + " " + getScrollY());

        if (isUpToDown && state == RecyclerView.SCROLL_STATE_IDLE && currentScroll < dp120) {
            isUpToDown = false;
            isAutoScrolling = true;
            smoothScrollBy(0, dp120 - currentScroll);
        }

        if (isDownToUp && state == RecyclerView.SCROLL_STATE_IDLE && currentScroll < dp120) {
            isDownToUp = false;
            isAutoScrolling = true;
            smoothScrollBy(0, -currentScroll);
        }

    }

    @Override
    public boolean onTouchEvent(MotionEvent e) {
        isAutoScrolling = false;
//        ToastUtils.showShortSafe("afdaf");
        return super.onTouchEvent(e);
    }

    @Override
    public void onScrolled(int dx, int dy) {
        super.onScrolled(dx, dy);

        currentScroll += dy;

        RecyclerView.LayoutManager manager = getLayoutManager();
        if (manager instanceof LinearLayoutManager) {

            Log.e(TAG, "onScrolled " + dx + " " + dy + " currentScroll: " + currentScroll);
            if (dy > 0 && currentScroll < dp120 && !isAutoScrolling) { //从上往下滑
                isUpToDown = true;
            }
            if (dy < 0 && currentScroll < dp120 && !isAutoScrolling) {
                isDownToUp = true;
            }

            calculateScrollPercent(currentScroll);
        }
    }

    /**
     * 计算百分比,然后让外面去实现透明度,等一些操作
     * @param currentScroll
     */
    private void calculateScrollPercent(int currentScroll) {
        if (onScrollPercentListener != null) {
            float percent = currentScroll / (float) dp120;
            if (percent >= 1) {
                percent = 1.0f;
            } else if(percent <=0){
                percent = 0f;
            }
            Log.e(TAG, "percent = " + percent);
            onScrollPercentListener.onPercent(percent);
        }
    }

    private OnScrollPercentListener onScrollPercentListener;

    public void setOnScrollPercentListener(OnScrollPercentListener onScrollPercentListener) {
        this.onScrollPercentListener = onScrollPercentListener;
    }

    public interface OnScrollPercentListener {
        void onPercent(float percent);
    }
}

xml

<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <!--    <include layout="@layout/home_layout" />-->

    <ImageView
        android:id="@+id/ivIcon"
        android:layout_width="120dp"
        android:layout_height="120dp"
        android:src="@mipmap/ic_launcher"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <com.mobi.clearsafe.main.widget.MyRecyclerView
        android:id="@+id/ivGrid"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />


</android.support.constraint.ConstraintLayout>

这样就可以实现类似的效果,在MyRecyclerView里面可以进行透明化监听什么的。
工作中遇到了记录一下。

上一篇 下一篇

猜你喜欢

热点阅读