Skip to content

Commit eedc2fc

Browse files
committed
支持视差Header效果
1 parent 9ae2d1d commit eedc2fc

File tree

7 files changed

+52
-4
lines changed

7 files changed

+52
-4
lines changed

app/src/main/java/me/yifeiyuan/flapdev/testcases/HeaderFooterTestcase.kt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,17 @@ package me.yifeiyuan.flapdev.testcases
22

33
import android.graphics.Color
44
import android.graphics.drawable.ColorDrawable
5+
import android.os.Build
56
import android.util.Log
67
import android.view.LayoutInflater
78
import android.view.View
8-
import androidx.recyclerview.widget.GridLayoutManager
9+
import android.view.animation.TranslateAnimation
910
import androidx.recyclerview.widget.ItemTouchHelper
11+
import androidx.recyclerview.widget.RecyclerView
1012
import kotlinx.android.synthetic.main.debug_menu.*
1113
import me.yifeiyuan.flap.ext.HeaderFooterAdapter
1214
import me.yifeiyuan.flap.ext.SwipeDragHelper
15+
import me.yifeiyuan.flap.widget.enableParallaxHeader
1316
import me.yifeiyuan.flapdev.R
1417
import me.yifeiyuan.flapdev.mockMultiTypeModels
1518

@@ -27,7 +30,7 @@ class HeaderFooterTestcase : BaseTestcaseFragment() {
2730

2831
val headerFooterAdapter = HeaderFooterAdapter(adapter)
2932

30-
val headerView = LayoutInflater.from(activity).inflate(R.layout.header_layout, null, false)
33+
val headerView = LayoutInflater.from(activity).inflate(R.layout.header_layout_image, null, false)
3134
headerFooterAdapter.setupHeaderView(headerView)
3235

3336
headerView.setOnClickListener {
@@ -99,6 +102,7 @@ class HeaderFooterTestcase : BaseTestcaseFragment() {
99102
// }
100103
// }
101104

105+
recyclerView.enableParallaxHeader()
102106
}
103107

104108
override fun createRefreshData(size: Int): MutableList<Any> {
1.97 MB
Loading

app/src/main/res/layout/component_simple_image.xml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22
<FrameLayout
33
xmlns:android="http://schemas.android.com/apk/res/android"
44
android:layout_width="match_parent"
5-
android:layout_height="wrap_content">
5+
android:layout_height="wrap_content"
6+
android:background="#BA68C8"
7+
>
68

79
<ImageView
810
android:id="@+id/logo"

app/src/main/res/layout/flap_item_simple_text.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
33
android:layout_width="match_parent"
44
android:layout_height="wrap_content"
5-
android:background="#0ff0ff"
5+
android:background="#0288D1"
66
android:orientation="horizontal"
77
android:paddingVertical="12dp"
88
>

app/src/main/res/layout/fragment_base_case.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
android:layout_width="match_parent"
2424
android:layout_height="wrap_content"
2525
android:orientation="vertical"
26+
android:background="#ffffff"
2627
app:layoutManager="me.yifeiyuan.flap.widget.FlapLinearLayoutManager" />
2728
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
2829

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<FrameLayout
3+
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
4+
android:layout_height="wrap_content"
5+
android:background="#E6DB7C">
6+
7+
<ImageView
8+
android:src="@drawable/flap_xcz"
9+
android:layout_width="match_parent"
10+
android:layout_height="240dp"
11+
android:text="Header"
12+
android:gravity="center"
13+
android:scaleType="centerCrop"
14+
android:textColor="#FFFFFF"
15+
/>
16+
</FrameLayout>
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package me.yifeiyuan.flap.widget
2+
3+
import androidx.recyclerview.widget.RecyclerView
4+
5+
/**
6+
* Created by 程序亦非猿 on 2022/10/18.
7+
*/
8+
9+
/**
10+
* 开启视差 Header 效果
11+
* @since 3.1.9
12+
*/
13+
fun RecyclerView.enableParallaxHeader(factor: Float = 0.5f) {
14+
addOnScrollListener(ParallaxHeaderHandler(factor))
15+
}
16+
17+
internal class ParallaxHeaderHandler(private val factor: Float = 0.5f) : RecyclerView.OnScrollListener() {
18+
override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
19+
super.onScrolled(recyclerView, dx, dy)
20+
val child = recyclerView.getChildAt(0)
21+
if (child != null && recyclerView.getChildAdapterPosition(child) == 0) {
22+
child.translationY = -child.top * factor
23+
}
24+
}
25+
}

0 commit comments

Comments
 (0)