diff --git a/app/src/main/java/me/yifeiyuan/flapdev/testcases/HeaderFooterTestcase.kt b/app/src/main/java/me/yifeiyuan/flapdev/testcases/HeaderFooterTestcase.kt index 195092e8..0f7a31af 100644 --- a/app/src/main/java/me/yifeiyuan/flapdev/testcases/HeaderFooterTestcase.kt +++ b/app/src/main/java/me/yifeiyuan/flapdev/testcases/HeaderFooterTestcase.kt @@ -5,11 +5,10 @@ import android.graphics.drawable.ColorDrawable import android.util.Log import android.view.LayoutInflater import android.view.View -import androidx.recyclerview.widget.GridLayoutManager import androidx.recyclerview.widget.ItemTouchHelper -import kotlinx.android.synthetic.main.debug_menu.* import me.yifeiyuan.flap.ext.HeaderFooterAdapter import me.yifeiyuan.flap.ext.SwipeDragHelper +import me.yifeiyuan.flap.widget.enableParallaxHeader import me.yifeiyuan.flapdev.R import me.yifeiyuan.flapdev.mockMultiTypeModels @@ -27,7 +26,7 @@ class HeaderFooterTestcase : BaseTestcaseFragment() { val headerFooterAdapter = HeaderFooterAdapter(adapter) - val headerView = LayoutInflater.from(activity).inflate(R.layout.header_layout, null, false) + val headerView = LayoutInflater.from(activity).inflate(R.layout.header_layout_image, null, false) headerFooterAdapter.setupHeaderView(headerView) headerView.setOnClickListener { @@ -99,6 +98,7 @@ class HeaderFooterTestcase : BaseTestcaseFragment() { // } // } + recyclerView.enableParallaxHeader() } override fun createRefreshData(size: Int): MutableList { diff --git a/app/src/main/res/drawable-xxhdpi/flap_xcz.jpg b/app/src/main/res/drawable-xxhdpi/flap_xcz.jpg new file mode 100644 index 00000000..712bb1c5 Binary files /dev/null and b/app/src/main/res/drawable-xxhdpi/flap_xcz.jpg differ diff --git a/app/src/main/res/layout/component_simple_image.xml b/app/src/main/res/layout/component_simple_image.xml index 92093d0d..c5a81c9b 100644 --- a/app/src/main/res/layout/component_simple_image.xml +++ b/app/src/main/res/layout/component_simple_image.xml @@ -2,7 +2,9 @@ + android:layout_height="wrap_content" + android:background="#BA68C8" + > diff --git a/app/src/main/res/layout/fragment_base_case.xml b/app/src/main/res/layout/fragment_base_case.xml index c54b8b8a..484a65d5 100644 --- a/app/src/main/res/layout/fragment_base_case.xml +++ b/app/src/main/res/layout/fragment_base_case.xml @@ -23,6 +23,7 @@ android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" + android:background="#ffffff" app:layoutManager="me.yifeiyuan.flap.widget.FlapLinearLayoutManager" /> diff --git a/app/src/main/res/layout/header_layout_image.xml b/app/src/main/res/layout/header_layout_image.xml new file mode 100644 index 00000000..56c9a193 --- /dev/null +++ b/app/src/main/res/layout/header_layout_image.xml @@ -0,0 +1,16 @@ + + + + + \ No newline at end of file diff --git a/flap/src/main/java/me/yifeiyuan/flap/widget/ParallaxHeaderEffect.kt b/flap/src/main/java/me/yifeiyuan/flap/widget/ParallaxHeaderEffect.kt new file mode 100644 index 00000000..f96e8a9b --- /dev/null +++ b/flap/src/main/java/me/yifeiyuan/flap/widget/ParallaxHeaderEffect.kt @@ -0,0 +1,27 @@ +package me.yifeiyuan.flap.widget + +import androidx.recyclerview.widget.RecyclerView + +/** + * Created by 程序亦非猿 on 2022/10/18. + */ + +/** + * 开启视差 Header 效果 + * + * @param factor 视差因子,默认 0.5f ,当 1.0f 效果类似固定的 Header + * @since 3.1.9 + */ +fun RecyclerView.enableParallaxHeader(factor: Float = 0.5f) { + addOnScrollListener(ParallaxHeaderHandler(factor)) +} + +internal class ParallaxHeaderHandler(private val factor: Float = 0.5f) : RecyclerView.OnScrollListener() { + override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) { + super.onScrolled(recyclerView, dx, dy) + val child = recyclerView.getChildAt(0) + if (child != null && recyclerView.getChildAdapterPosition(child) == 0) { + child.translationY = -child.top * factor + } + } +} \ No newline at end of file