首先自定义ScrollView
MyScrollView
import android.content.Context;
import android.util.AttributeSet;
import android.widget.ScrollView;
/**
* @Author:Administrator
* @E-mail: victory52@163.com
* @Date:2020/8/6 9:43
* @Description:描述信息
*/
public class MyScrollView extends ScrollView {
private OnScrollListener onScrollListener;
public MyScrollView(Context context) {
this(context, null);
}
public MyScrollView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public MyScrollView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public void setOnScrollListener(OnScrollListener onScrollListener) {
this.onScrollListener = onScrollListener;
}
@Override
protected void onScrollChanged(int l, int t, int oldl, int oldt) {
super.onScrollChanged(l, t, oldl, oldt);
if (onScrollListener != null) {
onScrollListener.onScroll(t);
}
}
public interface OnScrollListener {
public void onScroll(int scrollY);
}
}
在布局中使用MyScrollView
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/ll_parent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<com.utils.MyScrollView
android:id="@+id/scrollView"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:descendantFocusability="blocksDescendants"
android:orientation="vertical">
<View
android:id="@+id/imageview"
android:layout_width="match_parent"
android:layout_height="500dp"
android:background="@android:color/holo_blue_dark" />
<!--滑动时展示的title(内固定view)-->
<LinearLayout
android:id="@+id/ll_filter"
android:layout_width="match_parent"
android:layout_height="45dp"
android:background="#00EAFF"
android:orientation="horizontal"
android:gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center" />
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="1000dp"
android:background="@android:color/holo_green_light" />
<View
android:layout_width="match_parent"
android:layout_height="500dp"
android:background="#f00" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</RelativeLayout>
</LinearLayout>
<!--滑动之后在最上面展示的title(外固定view)-->
<LinearLayout
android:id="@+id/ll_top"
android:layout_width="match_parent"
android:layout_height="45dp"
android:background="#FF0000"
android:orientation="horizontal">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="外固定View" />
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler_view_home_title"
android:layout_width="match_parent"
android:layout_height="@dimen/dp_50"
android:layout_centerHorizontal="true"
android:layout_marginLeft="@dimen/dp_24"
android:layout_marginTop="@dimen/dp_12"
>
</androidx.recyclerview.widget.RecyclerView>
</LinearLayout>
</FrameLayout>
</com.utils.MyScrollView>
</LinearLayout>
kotlin
先写ScrollView监听
scrollView.setOnScrollListener(this)
颜色渐变,获取imageview的高度来设置渐变
override fun onScroll(scrollY: Int) {
val mTop = scrollY.coerceAtLeast(ll_filter.top)
ll_top.layout(0, mTop, ll_top.width, mTop + ll_top.height)
/*
* 颜色渐变(不需要渐变可以把下面的删掉)
* */
var imageHeight = imageview.getHeight()
if (scrollY <= imageHeight) {
val scale: Float = scrollY.toFloat() / imageHeight
val alpha = 255 * scale
// 只是layout背景透明(仿知乎滑动效果)白色透明
ll_top.setBackgroundColor(Color.argb(alpha.toInt(), 255, 255, 255))
// 设置文字颜色,黑色,加透明度
//textView.setTextColor(Color.argb(alpha.toInt(), 0, 0, 0))
Log.e("111", "y > 0 && y <= imageHeight")
}
}
以上就是悬浮头部,加渐变(本文只用于本人记录)