Skip to content

Commit

Permalink
fix scroll conflict
Browse files Browse the repository at this point in the history
  • Loading branch information
lsjwzh committed Jul 6, 2018
1 parent 17f81f0 commit c57ede7
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 19 deletions.
2 changes: 1 addition & 1 deletion widget.multirvcontainer/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ apply plugin: 'com.android.library'

android {
compileSdkVersion 25
buildToolsVersion "25.0.3"
buildToolsVersion '27.0.3'

defaultConfig {
minSdkVersion 14
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,17 @@ private void scrollToPositionInternal(int position, boolean isSmooth) {
&& getChildCount() > 0) {
ViewHolder childViewHolder = getChildViewHolder(getChildAt(getChildCount() - 1));
if (childViewHolder != null) {
int coordinatedTop = multiRVScrollView.getCoordinatedTop(this);
float coordinatedTop = multiRVScrollView.getCoordinatedTop(this);
if (childViewHolder.getAdapterPosition() < position) {
multiRVScrollView.scrollTo(0, coordinatedTop);
multiRVScrollView.scrollTo(0, (int) coordinatedTop);
rvScrollToPosition(position, isSmooth);
} else {
ViewHolder targetViewHolder = findViewHolderForAdapterPosition(position);
if (targetViewHolder != null &&
targetViewHolder.itemView.getBottom()
+ coordinatedTop
- multiRVScrollView.getScrollY() > multiRVScrollView.getHeight()) {
multiRVScrollView.scrollTo(0, coordinatedTop);
multiRVScrollView.scrollTo(0, (int) coordinatedTop);
}
rvScrollToPosition(position, isSmooth);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,15 @@ void init(Context context, AttributeSet attrs, int defStyleAttr) {
setOnScrollChangeListener(mNestScrollListener);
}

protected boolean canHandleByHostScrollView(int dy) {
for (NestRecyclerViewHelper next : mNestRecyclerViewHelpers) {
if (next.canHandleByHostScrollView(dy)) {
return true;
}
}
return false;
}

public boolean isCoordinatedWith(@NonNull RecyclerView recyclerView) {
for (NestRecyclerViewHelper next : mNestRecyclerViewHelpers) {
if (next.mNestedRecyclerView == recyclerView) {
Expand Down Expand Up @@ -177,6 +186,15 @@ public boolean startNestedScroll(int axes) {
return super.startNestedScroll(axes);
}

@Override
public void onNestedPreScroll(View target, int dx, int dy, int[] consumed) {
Log.d(TAG, "onNestedPreScroll dy:" + dy + " consumed:" + consumed[1]);
for (NestRecyclerViewHelper helper : mNestRecyclerViewHelpers) {
helper.onNestedPreScroll(target, dx, dy, consumed);
}
super.onNestedPreScroll(target, dx, dy, consumed);
}

@Override
public void onNestedScroll(View target, int dxConsumed, int dyConsumed, int dxUnconsumed,
int dyUnconsumed) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,24 @@ void startNestedScroll(int axes) {
mHostScrollView.smoothScrollBy(0, 0);
}


void onNestedPreScroll(View target, int dx, int dy, int[] consumed) {
if (isRecyclerViewNestedScrollingEnabled(target)) {
if (canHandleByHostScrollView(dy)) {
int oldScrollY = mHostScrollView.getScrollY();
Log.d(TAG, "want scrollBy " + dy);
mHostScrollView.scrollBy(0, dy);
consumed[1] = mHostScrollView.getScrollY() - oldScrollY;
Log.d(TAG, "real scrollBy " + consumed[1]);
}
}
}

boolean canHandleByHostScrollView(int dy) {
return !shouldHandleByRecyclerView()
|| (mNestedRecyclerView.computeVerticalScrollOffset() == 0 && dy < 0);
}

boolean onNestedScroll(View target, int dxConsumed, int dyConsumed, int dxUnconsumed,
int dyUnconsumed) {
if (isRecyclerViewNestedScrollingEnabled(target)) {
Expand Down Expand Up @@ -162,23 +180,18 @@ int getRecyclerViewTopOffset() {
}

int getRecyclerViewPartTop() {
return mChildContainsRecyclerView == null
? mNestedRecyclerView.getTop()
: mChildContainsRecyclerView.getTop() + mNestedRecyclerView.getTop();
}

private int getRecyclerViewPartBottom() {
return mChildContainsRecyclerView == null
? mNestedRecyclerView.getBottom()
: mNestedRecyclerView.getBottom() + mChildContainsRecyclerView.getTop();
return (int) (mChildContainsRecyclerView == null
? mNestedRecyclerView.getTop() + mNestedRecyclerView.getTranslationY()
: mChildContainsRecyclerView.getTop() + mChildContainsRecyclerView.getTranslationY()
+ mNestedRecyclerView.getTop() + mNestedRecyclerView.getTranslationY());
}

private View findDirectChildContainsRecyclerView() {
View parent = (View) mNestedRecyclerView.getParent();
if (parent == mHostScrollView.getChildAt(0)) {
if (parent == mHostScrollView.getScrollableCoreChild()) {
return null;
}
while (parent.getParent() != mHostScrollView.getChildAt(0)) {
while (parent.getParent() != mHostScrollView.getScrollableCoreChild()) {
parent = (View) parent.getParent();
}
return parent;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,13 @@
import java.util.List;

import static android.view.MotionEvent.ACTION_CANCEL;
import static android.view.MotionEvent.ACTION_OUTSIDE;
import static android.view.MotionEvent.ACTION_UP;

public class PullToRefreshHostScrollView extends MultiRVScrollView {
private static final String TAG = PullToRefreshHostScrollView.class.getSimpleName();
private int mTouchSlop;
private int mLastEventAction;
private int mLastEventAction = ACTION_OUTSIDE;
private List<RefreshListener> mRefreshListeners = new ArrayList<>();
private boolean mMoveBeforeTouchRelease;

Expand Down Expand Up @@ -129,8 +130,9 @@ public void run() {
public void stopNestedScroll() {
super.stopNestedScroll();
Log.d(TAG, "stopNestedScroll:");
if (mLastEventAction == ACTION_UP) {
if (mLastEventAction == ACTION_UP || mLastEventAction == ACTION_CANCEL) {
adjustRefreshViewState();
mLastEventAction = ACTION_OUTSIDE;
}
}

Expand All @@ -151,13 +153,20 @@ public void onNestedPreScroll(View target, int dx, int dy, int[] consumed) {
if (dy < -mTouchSlop && getScrollY() == 0 && consumed[1] == 0) {
consumed[1] = mTouchSlop + 1 + dy;
}
int dyUnconsumed = dy - consumed[1];
Log.d(TAG, " onNestedPreScroll dyConsumed:" + consumed[1] + " dyUnconsumed:" + dyUnconsumed);
if (dyUnconsumed != 0 && (getScrollY() == 0 || mMoveBeforeTouchRelease)
&& canHandleByHostScrollView(dyUnconsumed)) {
tryConsume(dyUnconsumed);
}
}

@Override
public void onNestedScroll(View target, int dxConsumed, int dyConsumed, int dxUnconsumed, int
dyUnconsumed) {
Log.d(TAG, "dyConsumed:" + dyConsumed + " dyUnconsumed:" + dyUnconsumed);
if (dyUnconsumed != 0 && (getScrollY() == 0 || mMoveBeforeTouchRelease)) {
Log.d(TAG, "onNestedScroll dyConsumed:" + dyConsumed + " dyUnconsumed:" + dyUnconsumed);
if (dyUnconsumed != 0 && (getScrollY() == 0 || mMoveBeforeTouchRelease) &&
canHandleByHostScrollView(dyUnconsumed)) {
if (tryConsume(dyUnconsumed)) {
return;
}
Expand Down

0 comments on commit c57ede7

Please sign in to comment.