-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #122 from AlanCheen/feature/parallax
Feature/parallax
- Loading branch information
Showing
7 changed files
with
51 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<FrameLayout | ||
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" | ||
android:layout_height="wrap_content" | ||
android:background="#E6DB7C"> | ||
|
||
<ImageView | ||
android:src="@drawable/flap_xcz" | ||
android:layout_width="match_parent" | ||
android:layout_height="240dp" | ||
android:text="Header" | ||
android:gravity="center" | ||
android:scaleType="centerCrop" | ||
android:textColor="#FFFFFF" | ||
/> | ||
</FrameLayout> |
27 changes: 27 additions & 0 deletions
27
flap/src/main/java/me/yifeiyuan/flap/widget/ParallaxHeaderEffect.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} | ||
} | ||
} |