Skip to content

Commit

Permalink
Merge pull request #122 from AlanCheen/feature/parallax
Browse files Browse the repository at this point in the history
Feature/parallax
  • Loading branch information
AlanCheen authored Oct 21, 2022
2 parents 9ae2d1d + 41c35c4 commit dfc789d
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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 {
Expand Down Expand Up @@ -99,6 +98,7 @@ class HeaderFooterTestcase : BaseTestcaseFragment() {
// }
// }

recyclerView.enableParallaxHeader()
}

override fun createRefreshData(size: Int): MutableList<Any> {
Expand Down
Binary file added app/src/main/res/drawable-xxhdpi/flap_xcz.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 3 additions & 1 deletion app/src/main/res/layout/component_simple_image.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
android:layout_height="wrap_content"
android:background="#BA68C8"
>

<ImageView
android:id="@+id/logo"
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/layout/flap_item_simple_text.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#0ff0ff"
android:background="#0288D1"
android:orientation="horizontal"
android:paddingVertical="12dp"
>
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/layout/fragment_base_case.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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" />
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>

Expand Down
16 changes: 16 additions & 0 deletions app/src/main/res/layout/header_layout_image.xml
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>
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
}
}
}

0 comments on commit dfc789d

Please sign in to comment.