100多行实现嵌套滚动

效果

在这里插入图片描述

源码

基于5.1系统的嵌套滚动机制实现。
可以嵌套滚动,惯性滑动。

MyLinearLayout

随便取的名字

package com.example.myapplication.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.widget.LinearLayout;
import android.widget.Scroller;

import androidx.annotation.Nullable;

/**
 * <pre>
 * Created by zhuguohui
 * Date: 2023/5/16
 * Time: 16:44
 * Desc:嵌套滚动的demo
 * </pre>
 */
public class MyLinearLayout extends LinearLayout {

    private int imgHeight;
    private Scroller scroller;

    public MyLinearLayout(Context context) {
        super(context);
    }

    public MyLinearLayout(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        scroller = new Scroller(context, new AccelerateDecelerateInterpolator());
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        imgHeight = getChildAt(0).getMeasuredHeight();
        int recycleViewHeight = getChildAt(2).getMeasuredHeight();
        int measuredWidth = getChildAt(2).getMeasuredWidth();

        int hs = MeasureSpec.makeMeasureSpec(imgHeight + recycleViewHeight, MeasureSpec.EXACTLY);
        int ws = MeasureSpec.makeMeasureSpec(measuredWidth, MeasureSpec.EXACTLY);
        getChildAt(2).measure(ws, hs);
    }

    @Override
    public boolean onStartNestedScroll(View child, View target, int nestedScrollAxes) {
        return true;
    }

    @Override
    public void onNestedPreScroll(View target, int dx, int dy, int[] consumed) {
        super.onNestedPreScroll(target, dx, dy, consumed);
        if (dy > 0) {
            //向上滚动
            int leftHeight = imgHeight - getScrollY();
            if (canScroll(target, dy)) {
                int my = Math.min(dy, leftHeight);
                consumed[1] = my;
                scroller.abortAnimation();
                scrollBy(0, my);
            }
        } else {
            //向下滚动
            if (canScroll(target, dy)) {
                int leftHeight = getScrollY();
                int my = 0;
                if (Math.abs(dy) > leftHeight) {
                    my = -leftHeight;
                } else {
                    my = dy;
                }
                consumed[1] = my;
                scroller.abortAnimation();
                scrollBy(0, my);

            }
        }
    }

    @Override
    public boolean onNestedPreFling(View target, float velocityX, float velocityY) {
        if (canScroll(target, velocityY)) {
            //这里很重要
            //minY要等于 Integer.MIN_VALUE
            //maxY要等于  Integer.MAX_VALUE
            //否则滑动起来感觉不惯性。
            scroller.fling(getScrollX(), getScrollY(), (int) velocityX, (int) velocityY, 0, 0, Integer.MIN_VALUE, Integer.MAX_VALUE);
            invalidate();
            //return false 可以实现嵌套滑动
            return false;
        } else {
            return super.onNestedPreFling(target, velocityX, velocityY);
        }

    }

    @Override
    public void scrollTo(int x, int y) {
        y = Math.min(y, imgHeight);
        y = Math.max(0, y);

        super.scrollTo(x, y);
    }

    @Override
    public void computeScroll() {
        super.computeScroll();
        if (scroller.computeScrollOffset()) {
            scrollTo(scroller.getCurrX(), scroller.getCurrY());
            invalidate();
        }
    }

    private boolean canScroll(View target, float direction) {
        //
        if (direction > 0) {
            //向上滚动
            return getScrollY() < imgHeight;
        } else {
            //向下滚动
            return !target.canScrollVertically((int) direction) && getScrollY() > 0;
        }
    }

}

布局文件

<?xml version="1.0" encoding="utf-8"?>
<com.example.myapplication.view.MyLinearLayout 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"
    android:dividerPadding="10dp"
    android:gravity="center_horizontal"
    android:orientation="vertical"
    android:showDividers="middle"
    tools:context=".MainActivity">

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:scaleType="fitXY"
        android:src="@mipmap/m" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:background="#FFC31B"
        android:gravity="center"
        android:text="我是文字"
        android:textColor="#fff"
        android:textSize="20sp" />

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recycleView"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</com.example.myapplication.view.MyLinearLayout>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值