Skip to content

Commit

Permalink
fix bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
1993hzw committed Mar 8, 2019
1 parent 9471fb4 commit c6cdb79
Show file tree
Hide file tree
Showing 8 changed files with 162 additions and 23 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,4 +96,5 @@ overScrollView.addOffsetChangeListener(new IOffsetChangeListener() {
...
}
});
```
```
(The default `IOverScrollCallback` is [SimpleOverScrollCallback](https://github.com/1993hzw/OverScroll/blob/master/overscroll/src/main/java/cn/forward/overscroll/SimpleOverScrollCallback.java))
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,14 @@ public void onTabSelected(TabLayout.Tab tab) {
getSupportFragmentManager().beginTransaction()
.replace(R.id.fragment_container, new HorizontalRecyclerViewFragment())
.commitAllowingStateLoss();
} else {
} else if(tab.getPosition() == 2){
getSupportFragmentManager().beginTransaction()
.replace(R.id.fragment_container, new NestedScrollFragment())
.commitAllowingStateLoss();
}else{
getSupportFragmentManager().beginTransaction()
.replace(R.id.fragment_container, new TestFragment())
.commitAllowingStateLoss();
}

}
Expand Down
21 changes: 21 additions & 0 deletions app/src/main/java/cn/forward/overscrolldemo/TestFragment.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package cn.forward.overscrolldemo;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

/**
* @author ziwei huang
*/
public class TestFragment extends Fragment {

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.layout_test, container, false);
return view;
}
}
8 changes: 8 additions & 0 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,14 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="嵌套滚动" />

<!--
<android.support.design.widget.TabItem
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="test" />
-->

</android.support.design.widget.TabLayout>

</LinearLayout>
110 changes: 110 additions & 0 deletions app/src/main/res/layout/layout_test.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">

<FrameLayout
android:layout_width="match_parent"
android:layout_height="200dp"
android:minHeight="50dp"
android:background="#fff"
app:layout_scrollFlags="scroll|exitUntilCollapsed">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="1"
android:textColor="#000"
android:textSize="36dp" />
</FrameLayout>
</android.support.design.widget.AppBarLayout>

<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">

<FrameLayout
android:layout_width="match_parent"
android:layout_height="200dp"
android:background="#f00">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="1"
android:textColor="#000"
android:textSize="36dp" />
</FrameLayout>

<FrameLayout
android:layout_width="match_parent"
android:layout_height="200dp"
android:background="#00f">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="3"
android:textColor="#000"
android:textSize="36dp" />
</FrameLayout>

<FrameLayout
android:layout_width="match_parent"
android:layout_height="200dp"
android:background="#0ff">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="4"
android:textColor="#000"
android:textSize="36dp" />
</FrameLayout>

<FrameLayout
android:layout_width="match_parent"
android:layout_height="200dp"
android:background="#ff0">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="5"
android:textColor="#000"
android:textSize="36dp" />
</FrameLayout>

<FrameLayout
android:layout_width="match_parent"
android:layout_height="200dp"
android:background="#f0f">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="6"
android:textColor="#000"
android:textSize="36dp" />
</FrameLayout>
</LinearLayout>
</android.support.v4.widget.NestedScrollView>

</android.support.design.widget.CoordinatorLayout>
4 changes: 2 additions & 2 deletions overscroll/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ android {
defaultConfig {
minSdkVersion 14
targetSdkVersion 26
versionCode 1
versionName "1.0"
versionCode 2
versionName "1.1"

testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import android.support.v4.math.MathUtils;
import android.support.v4.view.ViewCompat;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.animation.DecelerateInterpolator;
import android.view.animation.Interpolator;
Expand Down Expand Up @@ -136,10 +135,10 @@ protected void onNestedScrollInner(CoordinatorLayout coordinatorLayout, View chi
if (type == ViewCompat.TYPE_TOUCH) {
scroll(child, distanceUnconsumed, 0, getMaxOffset(child));
} else { // fling
if ((mOverScroller != null && mOverScroller.computeScrollOffset()
&& Math.abs(mOverScroller.getCurrVelocity()) < Math.abs(overscrollListener.getMinFlingVelocity(this, child, mDirectionToEnd))) // too slow
||
getOffset(child) >= overscrollListener.getMaxFlingOffset(this, child, mDirectionToEnd)) { // reach edge
if (mOverScroller == null
|| !mOverScroller.computeScrollOffset()
|| Math.abs(mOverScroller.getCurrVelocity()) < Math.abs(overscrollListener.getMinFlingVelocity(this, child, mDirectionToEnd)) // too slow
|| getOffset(child) >= overscrollListener.getMaxFlingOffset(this, child, mDirectionToEnd)) { // reach edge
ViewCompat.stopNestedScroll(target, ViewCompat.TYPE_NON_TOUCH);
} else {
scroll(child, distanceUnconsumed,
Expand All @@ -155,8 +154,9 @@ protected void onNestedScrollInner(CoordinatorLayout coordinatorLayout, View chi
if (type == ViewCompat.TYPE_TOUCH) {
scroll(child, distanceUnconsumed, getMinOffset(child), 0);
} else { // fling
if ((mOverScroller != null && mOverScroller.computeScrollOffset()
&& Math.abs(mOverScroller.getCurrVelocity()) < overscrollListener.getMinFlingVelocity(this, child, mDirectionToStart)) // too slow
if (mOverScroller == null
|| !mOverScroller.computeScrollOffset()
|| Math.abs(mOverScroller.getCurrVelocity()) < Math.abs(overscrollListener.getMinFlingVelocity(this, child, mDirectionToStart)) // too slow
|| getOffset(child) <= overscrollListener.getMaxFlingOffset(this, child, mDirectionToStart)) { // reach edge
ViewCompat.stopNestedScroll(target, ViewCompat.TYPE_NON_TOUCH);
} else {
Expand All @@ -175,12 +175,6 @@ protected boolean onNestedPreFlingInner(CoordinatorLayout coordinatorLayout, Vie
return false;
}

if (getOffset(child) != 0) { // 越界后不能产生惯性滑动,否则造成越界过程中child内部同时也发生滑动
// No fling can occur after crossing the boundary, otherwise the fling of the child will also occur during the crossing process.
ViewCompat.stopNestedScroll(target, ViewCompat.TYPE_NON_TOUCH);
return true; // must true
}

if (mOverScroller == null) {
mOverScroller = new OverScroller(coordinatorLayout.getContext());
}
Expand All @@ -202,8 +196,9 @@ public void onStopNestedScroll(CoordinatorLayout coordinatorLayout, View child,

if (type == ViewCompat.TYPE_TOUCH) { // touching
if (getOffset(child) != 0) { // and out of bound
ViewCompat.stopNestedScroll(child, ViewCompat.TYPE_NON_TOUCH);
springBack(child);
if( mOverScroller == null || !mOverScroller.computeScrollOffset()) { // no fling
springBack(child);
}
}
} else {
if (getOffset(child) != 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ public interface IOverScrollCallback {
public @interface ScrollDirection {
}

public static final int DIRECTION_UP = 1 << 0;
public static final int DIRECTION_DOWN = 1 << 1;
public static final int DIRECTION_LEFT = 1 << 2;
public static final int DIRECTION_RIGHT = 1 << 3;
public static final int DIRECTION_UP = 1 << 0; // Direction to start
public static final int DIRECTION_DOWN = 1 << 1; // Direction to end
public static final int DIRECTION_LEFT = 1 << 2; // Direction to start
public static final int DIRECTION_RIGHT = 1 << 3; // Direction to end

/**
* @param overScroll
Expand Down

0 comments on commit c6cdb79

Please sign in to comment.