Skip to content

Commit

Permalink
resolve issue #8 and issue #12
Browse files Browse the repository at this point in the history
  • Loading branch information
valkriaine committed May 9, 2022
1 parent 7afbb99 commit d98ac37
Show file tree
Hide file tree
Showing 9 changed files with 121 additions and 22 deletions.
43 changes: 38 additions & 5 deletions bouncy/src/main/java/com/factor/bouncy/BouncyNestedScrollView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ class BouncyNestedScrollView @JvmOverloads constructor(context: Context, attrs:
private var mEdgeGlowTop: BouncyEdgeEffect? = null
private var mEdgeGlowBottom: BouncyEdgeEffect? = null


private var touched = false

/**
* Position of the last motion event.
*/
Expand Down Expand Up @@ -624,6 +627,7 @@ class BouncyNestedScrollView @JvmOverloads constructor(context: Context, attrs:

val vMotionEvent = MotionEvent.obtain(ev)
vMotionEvent.offsetLocation(0f, mNestedYOffset.toFloat())
touched = true
when (actionMasked)
{
MotionEvent.ACTION_DOWN ->
Expand Down Expand Up @@ -711,15 +715,15 @@ class BouncyNestedScrollView @JvmOverloads constructor(context: Context, attrs:
{
EdgeEffectCompat.onPull(mEdgeGlowBottom!!, deltaY.toFloat() / height, ev.getX(activePointerIndex) / width)

if (!mEdgeGlowBottom!!.isFinished)
if (!mEdgeGlowBottom!!.isFinished && !touched)
mEdgeGlowBottom!!.onRelease()

}
else if (pulledToY > range)
{
EdgeEffectCompat.onPull(mEdgeGlowBottom!!, deltaY.toFloat() / height, 1f - ev.getX(activePointerIndex) / width)

if (!mEdgeGlowTop!!.isFinished)
if (!mEdgeGlowTop!!.isFinished && !touched)
mEdgeGlowTop!!.onRelease()

}
Expand All @@ -732,6 +736,7 @@ class BouncyNestedScrollView @JvmOverloads constructor(context: Context, attrs:
}
MotionEvent.ACTION_UP ->
{
touched = false
val velocityTracker = mVelocityTracker
velocityTracker!!.computeCurrentVelocity(1000, mMaximumVelocity.toFloat())
val initialVelocity = velocityTracker.getYVelocity(mActivePointerId).toInt()
Expand All @@ -751,6 +756,7 @@ class BouncyNestedScrollView @JvmOverloads constructor(context: Context, attrs:
}
MotionEvent.ACTION_CANCEL ->
{
touched = false
if (mIsBeingDragged && childCount > 0 && mScroller!!.springBack(scrollX, scrollY, 0, 0, 0, scrollRange))
ViewCompat.postInvalidateOnAnimation(this)

Expand Down Expand Up @@ -1652,15 +1658,42 @@ class BouncyNestedScrollView @JvmOverloads constructor(context: Context, attrs:
}
}


/**
* Option to bind overscroll effect to parent instead of self.
*
* Default is false, set this to true will bind SpringAnimation to parent view.
*/
var bindSpringToParent = false
set(value)
{
field = value
ensureGlows()
}

private fun ensureGlows()
{
if (overScrollMode != OVER_SCROLL_NEVER)
{
if (mEdgeGlowTop == null)
{
val context = context
mEdgeGlowTop = BouncyEdgeEffect(context, spring, this, EdgeEffectFactory.DIRECTION_TOP, flingAnimationSize, overscrollAnimationSize)
mEdgeGlowBottom = BouncyEdgeEffect(context, spring, this, EdgeEffectFactory.DIRECTION_BOTTOM, flingAnimationSize, overscrollAnimationSize)

val viewToAnimate : View = if (!bindSpringToParent)
this
else
this.parent as View

val spring = SpringAnimation(viewToAnimate, SpringAnimation.TRANSLATION_Y)
.setSpring(
SpringForce()
.setFinalPosition(0f)
.setDampingRatio(dampingRatio)
.setStiffness(stiffness)
)
mEdgeGlowTop = BouncyEdgeEffect(context, spring, viewToAnimate, EdgeEffectFactory.DIRECTION_TOP, flingAnimationSize, overscrollAnimationSize)
mEdgeGlowBottom = BouncyEdgeEffect(context, spring, viewToAnimate, EdgeEffectFactory.DIRECTION_BOTTOM, flingAnimationSize, overscrollAnimationSize)


}
}
else
Expand Down
23 changes: 23 additions & 0 deletions bouncy/src/main/java/com/factor/bouncy/BouncyRecyclerView.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package com.factor.bouncy

import android.annotation.SuppressLint
import android.content.Context
import android.graphics.Canvas
import android.util.AttributeSet
import android.view.MotionEvent
import android.widget.EdgeEffect
import androidx.dynamicanimation.animation.SpringAnimation
import androidx.dynamicanimation.animation.SpringForce
Expand Down Expand Up @@ -77,6 +79,22 @@ class BouncyRecyclerView(context: Context, attrs: AttributeSet?) : RecyclerView(
)


var touched: Boolean = false


@SuppressLint("ClickableViewAccessibility")
override fun onTouchEvent(e: MotionEvent?): Boolean
{

touched = when (e?.actionMasked)
{
MotionEvent.ACTION_UP, MotionEvent.ACTION_CANCEL -> false
else -> true
}
return super.onTouchEvent(e)
}


override fun setAdapter(adapter: RecyclerView.Adapter<*>?)
{
super.setAdapter(adapter)
Expand Down Expand Up @@ -215,6 +233,11 @@ class BouncyRecyclerView(context: Context, attrs: AttributeSet?) : RecyclerView(
override fun onRelease()
{
super.onRelease()

if (touched)
return


onOverPullListener?.onRelease()
spring.start()

Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ buildscript {
}
dependencies {
classpath 'com.android.tools.build:gradle:7.0.4'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.6.20"

// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
Expand Down
9 changes: 8 additions & 1 deletion example/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ android {
buildToolsVersion "30.0.3"

defaultConfig {
minSdkVersion 14
minSdkVersion 16
targetSdkVersion 31
versionCode 1
versionName "1.0"
Expand All @@ -25,6 +25,12 @@ android {
sourceCompatibility JavaVersion.VERSION_11
targetCompatibility JavaVersion.VERSION_11
}
buildFeatures {
viewBinding true
}
kotlinOptions {
jvmTarget = '1.8'
}
}

dependencies {
Expand All @@ -36,6 +42,7 @@ dependencies {
implementation 'androidx.constraintlayout:constraintlayout:2.1.3'
implementation 'androidx.recyclerview:recyclerview:1.2.1'
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
implementation 'com.google.android.material:material:1.6.0'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
Expand Down
10 changes: 6 additions & 4 deletions example/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,17 @@
package="com.factor.example">

<application
android:label="@string/app_name"
android:allowBackup="false">
android:allowBackup="false"
android:label="@string/app_name">

<activity
android:name=".MainActivity"
android:exported="true"
android:label="@string/app_name"
android:theme="@style/Theme.AppCompat.DayNight.NoActionBar"
android:exported="true">
android:theme="@style/Theme.AppCompat.DayNight.DarkActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>

<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
Expand Down
6 changes: 4 additions & 2 deletions example/src/main/java/com/factor/example/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.factor.example
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.recyclerview.widget.GridLayoutManager
import com.factor.bouncy.BouncyNestedScrollView
import com.factor.bouncy.BouncyRecyclerView


Expand All @@ -13,8 +14,9 @@ class MainActivity : AppCompatActivity()
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

findViewById<BouncyNestedScrollView>(R.id.scrollView).bindSpringToParent = true

findViewById<BouncyRecyclerView>(R.id.rc).adapter = MyAdapter(30)
findViewById<BouncyRecyclerView>(R.id.rc).layoutManager = GridLayoutManager(this, 2, GridLayoutManager.VERTICAL, false)
//findViewById<BouncyRecyclerView>(R.id.rc).adapter = MyAdapter(30)
//findViewById<BouncyRecyclerView>(R.id.rc).layoutManager = GridLayoutManager(this, 2, GridLayoutManager.VERTICAL, false)
}
}
33 changes: 24 additions & 9 deletions example/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,32 @@
android:layout_height="match_parent"
tools:context=".MainActivity">

<com.google.android.material.appbar.AppBarLayout
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent"
android:layout_width="match_parent">

<androidx.appcompat.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"/>

</com.google.android.material.appbar.AppBarLayout>

<com.factor.bouncy.BouncyRecyclerView
android:id="@+id/rc"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:allow_item_swipe="true"
app:allow_drag_reorder="true"/>

<!--BouncyNestedScrollView-->
<!--
<com.factor.bouncy.BouncyRecyclerView
android:id="@+id/rc"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:allow_item_swipe="true"
app:allow_drag_reorder="true"/>
-->

<!--BouncyNestedScrollView-->

<com.factor.bouncy.BouncyNestedScrollView
android:id="@+id/scrollView"
android:layout_width="match_parent"
android:layout_height="match_parent">

Expand All @@ -29,7 +44,7 @@

<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_height="200dp"
app:cardCornerRadius="15dp"
android:layout_margin="10dp"
app:cardBackgroundColor="#9C27B0"/>
Expand Down Expand Up @@ -157,6 +172,6 @@
</androidx.appcompat.widget.LinearLayoutCompat>

</com.factor.bouncy.BouncyNestedScrollView>
-->


</androidx.constraintlayout.widget.ConstraintLayout>
9 changes: 9 additions & 0 deletions example/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
<resources>
<string name="app_name">Factor Example</string>
<string name="title_activity_main2">MainActivity2</string>
<!-- Strings used for fragments for navigation -->
<string name="first_fragment_label">First Fragment</string>
<string name="second_fragment_label">Second Fragment</string>
<string name="next">Next</string>
<string name="previous">Previous</string>

<string name="hello_first_fragment">Hello first fragment</string>
<string name="hello_second_fragment">Hello second fragment. Arg: %1$s</string>
</resources>
8 changes: 8 additions & 0 deletions example/src/main/res/values/themes.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<resources>
<style name="NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
<style name="Theme.Library.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar"/>
<style name="Theme.Library.PopupOverlay" parent="ThemeOverlay.AppCompat.Light"/>
</resources>

0 comments on commit d98ac37

Please sign in to comment.