效果
源码
基于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>