Skip to content

Commit

Permalink
feat(android): add nestedScrollPriority for ListView and ScrollView
Browse files Browse the repository at this point in the history
* feat(android): squash branch scroll_conflict (Tencent#6)

* fix(android): nested scroll feature

  - edit priority api
  - scrollview prevent nested scroll when enabled paging
  - fix viewpager vertical issue
  - viewpager nested scroll
  - translate comments

* fix(android): Eltonqin/netscroll bugfix (#3)

* fix(android):HorizonScrollView may throw exception when handle onInterceptTouchEvent

* fix(android):post task still run after remove

* fix(android):post task still run after remove,modify format

Co-authored-by: eltonqin <[email protected]>

* fix(android): nested scroll feature

  - viewpager nested scroll issue

* feat(docs): update docs

Co-authored-by: HOHOHOTangoDown <[email protected]>
Co-authored-by: eltonqin <[email protected]>

* fix(android): nested scroll conflicts with pull refresh

* feat(docs, demo): update docs and demos

Co-authored-by: HOHOHOTangoDown <[email protected]>
Co-authored-by: eltonqin <[email protected]>
  • Loading branch information
3 people authored and pba-cra committed Feb 1, 2023
1 parent 3cde9fa commit 6ecf50a
Show file tree
Hide file tree
Showing 19 changed files with 931 additions and 76 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
import com.tencent.mtt.hippy.utils.LogUtils;
import com.tencent.mtt.hippy.utils.PixelUtil;
import com.tencent.mtt.hippy.views.common.CommonBorder;
import com.tencent.mtt.hippy.views.common.HippyNestedScrollComponent;
import com.tencent.mtt.hippy.views.common.HippyNestedScrollHelper;
import com.tencent.mtt.hippy.views.hippylist.HippyRecyclerViewWrapper;
import com.tencent.mtt.hippy.views.list.HippyListView;
import com.tencent.mtt.hippy.views.scroll.HippyScrollView;
Expand Down Expand Up @@ -640,6 +642,52 @@ public void setRenderToHardwareTexture(T view, boolean useHWTexture) {
view.setLayerType(useHWTexture ? View.LAYER_TYPE_HARDWARE : View.LAYER_TYPE_NONE, null);
}

@HippyControllerProps(name = HippyNestedScrollComponent.PROP_PRIORITY, defaultType =
HippyControllerProps.STRING, defaultString = HippyNestedScrollComponent.PRIORITY_SELF)
public void setNestedScrollPriority(T view, String priorityName) {
if (view instanceof HippyNestedScrollComponent) {
HippyNestedScrollComponent sc = (HippyNestedScrollComponent) view;
HippyNestedScrollComponent.Priority priority = HippyNestedScrollHelper.priorityOf(priorityName);
sc.setNestedScrollPriority(HippyNestedScrollComponent.DIRECTION_ALL, priority);
}
}

@HippyControllerProps(name = HippyNestedScrollComponent.PROP_LEFT_PRIORITY, defaultType =
HippyControllerProps.STRING, defaultString = HippyNestedScrollComponent.PRIORITY_SELF)
public void setNestedScrollLeftPriority(T view, String priorityName) {
if (view instanceof HippyNestedScrollComponent) {
HippyNestedScrollComponent.Priority priority = HippyNestedScrollHelper.priorityOf(priorityName);
((HippyNestedScrollComponent) view).setNestedScrollPriority(HippyNestedScrollComponent.DIRECTION_LEFT, priority);
}
}

@HippyControllerProps(name = HippyNestedScrollComponent.PROP_TOP_PRIORITY, defaultType =
HippyControllerProps.STRING, defaultString = HippyNestedScrollComponent.PRIORITY_SELF)
public void setNestedScrollTopPriority(T view, String priorityName) {
if (view instanceof HippyNestedScrollComponent) {
HippyNestedScrollComponent.Priority priority = HippyNestedScrollHelper.priorityOf(priorityName);
((HippyNestedScrollComponent) view).setNestedScrollPriority(HippyNestedScrollComponent.DIRECTION_TOP, priority);
}
}

@HippyControllerProps(name = HippyNestedScrollComponent.PROP_RIGHT_PRIORITY, defaultType =
HippyControllerProps.STRING, defaultString = HippyNestedScrollComponent.PRIORITY_SELF)
public void setNestedScrollRightPriority(T view, String priorityName) {
if (view instanceof HippyNestedScrollComponent) {
HippyNestedScrollComponent.Priority priority = HippyNestedScrollHelper.priorityOf(priorityName);
((HippyNestedScrollComponent) view).setNestedScrollPriority(HippyNestedScrollComponent.DIRECTION_RIGHT, priority);
}
}

@HippyControllerProps(name = HippyNestedScrollComponent.PROP_BOTTOM_PRIORITY, defaultType =
HippyControllerProps.STRING, defaultString = HippyNestedScrollComponent.PRIORITY_SELF)
public void setNestedScrollBottomPriority(T view, String priorityName) {
if (view instanceof HippyNestedScrollComponent) {
HippyNestedScrollComponent.Priority priority = HippyNestedScrollHelper.priorityOf(priorityName);
((HippyNestedScrollComponent) view).setNestedScrollPriority(HippyNestedScrollComponent.DIRECTION_BOTTOM, priority);
}
}

@SuppressWarnings("EmptyMethod")
@HippyControllerProps(name = NodeProps.CUSTOM_PROP)
public void setCustomProp(T view, String methodName, Object props) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/* Tencent is pleased to support the open source community by making Hippy available.
* Copyright (C) 2018 THL A29 Limited, a Tencent company. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.tencent.mtt.hippy.views.common;

import androidx.core.view.NestedScrollingChild;
import androidx.core.view.NestedScrollingChild2;
import androidx.core.view.NestedScrollingParent;
import androidx.core.view.NestedScrollingParent2;

public interface HippyNestedScrollComponent {

String PROP_PRIORITY = "nestedScrollPriority";
String PROP_LEFT_PRIORITY = "nestedScrollLeftPriority";
String PROP_TOP_PRIORITY = "nestedScrollTopPriority";
String PROP_RIGHT_PRIORITY = "nestedScrollRightPriority";
String PROP_BOTTOM_PRIORITY = "nestedScrollBottomPriority";

String PRIORITY_PARENT = "parent";
String PRIORITY_SELF = "self";
String PRIORITY_NONE = "none";

int DIRECTION_INVALID = -1;
int DIRECTION_ALL = 0;
int DIRECTION_LEFT = 1;
int DIRECTION_TOP = 2;
int DIRECTION_RIGHT = 3;
int DIRECTION_BOTTOM = 4;

void setNestedScrollPriority(int direction, Priority priority);

Priority getNestedScrollPriority(int direction);

enum Priority {
NOT_SET,
PARENT,
SELF,
NONE,
}

/**
* Nested scroll interface declaration
*/
interface HippyNestedScrollTarget extends HippyNestedScrollComponent, NestedScrollingParent,
NestedScrollingChild {

}

/**
* Added non-touch scrolling type than {@link HippyNestedScrollTarget}
*/
interface HippyNestedScrollTarget2 extends HippyNestedScrollTarget, NestedScrollingParent2,
NestedScrollingChild2 {

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/* Tencent is pleased to support the open source community by making Hippy available.
* Copyright (C) 2018 THL A29 Limited, a Tencent company. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.tencent.mtt.hippy.views.common;

import static com.tencent.mtt.hippy.views.common.HippyNestedScrollComponent.DIRECTION_BOTTOM;
import static com.tencent.mtt.hippy.views.common.HippyNestedScrollComponent.DIRECTION_LEFT;
import static com.tencent.mtt.hippy.views.common.HippyNestedScrollComponent.DIRECTION_RIGHT;
import static com.tencent.mtt.hippy.views.common.HippyNestedScrollComponent.DIRECTION_TOP;
import static com.tencent.mtt.hippy.views.common.HippyNestedScrollComponent.PRIORITY_NONE;
import static com.tencent.mtt.hippy.views.common.HippyNestedScrollComponent.PRIORITY_PARENT;
import static com.tencent.mtt.hippy.views.common.HippyNestedScrollComponent.PRIORITY_SELF;
import static com.tencent.mtt.hippy.views.common.HippyNestedScrollComponent.Priority;

import android.view.View;
import com.tencent.mtt.hippy.views.common.HippyNestedScrollComponent.HippyNestedScrollTarget;

public class HippyNestedScrollHelper {

public static Priority priorityOf(String name) {
switch (name) {
case PRIORITY_SELF:
return Priority.SELF;
case PRIORITY_PARENT:
return Priority.PARENT;
case PRIORITY_NONE:
return Priority.NONE;
default:
throw new RuntimeException("Invalid priority: " + name);
}
}

public static Priority priorityOfX(View target, int dx) {
// not scrolling, priority is NONE
if (dx == 0) {
return Priority.NONE;
}
// non-HippyNestedScrollTarget View, priority is SELF
if (!(target instanceof HippyNestedScrollTarget)) {
return Priority.SELF;
}
// get priority from target by direction
return ((HippyNestedScrollTarget) target).getNestedScrollPriority(
dx > 0 ? DIRECTION_LEFT : DIRECTION_RIGHT);
}

public static Priority priorityOfY(View target, int dy) {
// not scrolling, priority is NONE
if (dy == 0) {
return Priority.NONE;
}
// non-HippyNestedScrollTarget View, priority is SELF
if (!(target instanceof HippyNestedScrollTarget)) {
return Priority.SELF;
}
// get priority from target by direction
return ((HippyNestedScrollTarget) target).getNestedScrollPriority(
dy > 0 ? DIRECTION_TOP : DIRECTION_BOTTOM);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,18 @@
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;

import android.widget.FrameLayout;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.view.NestedScrollingChild2;
import androidx.core.view.NestedScrollingParent2;
import androidx.core.view.ViewCompat;
import androidx.recyclerview.widget.HippyRecyclerViewBase;
import androidx.recyclerview.widget.IHippyViewAboundListener;
import androidx.recyclerview.widget.LinearLayoutManager;
import com.tencent.mtt.hippy.HippyEngineContext;
import com.tencent.mtt.hippy.utils.LogUtils;
import com.tencent.mtt.hippy.utils.PixelUtil;
import com.tencent.mtt.hippy.views.common.HippyNestedScrollComponent.HippyNestedScrollTarget2;
import com.tencent.mtt.hippy.views.common.HippyNestedScrollHelper;
import com.tencent.mtt.hippy.views.hippylist.recyclerview.helper.skikcy.IHeaderAttachListener;
import com.tencent.mtt.hippy.views.hippylist.recyclerview.helper.skikcy.IHeaderHost;
import com.tencent.mtt.hippy.views.hippylist.recyclerview.helper.skikcy.StickyHeaderHelper;
Expand All @@ -46,8 +45,7 @@
* Created on 2020/12/22. Description
*/
public class HippyRecyclerView<ADP extends HippyRecyclerListAdapter> extends HippyRecyclerViewBase
implements IHeaderAttachListener, IHippyViewAboundListener,
NestedScrollingChild2, NestedScrollingParent2 {
implements IHeaderAttachListener, IHippyViewAboundListener, HippyNestedScrollTarget2 {

private static int DEFAULT_ITEM_VIEW_CACHE_SIZE = 8;
protected HippyEngineContext hippyEngineContext;
Expand All @@ -65,6 +63,8 @@ public class HippyRecyclerView<ADP extends HippyRecyclerListAdapter> extends Hip
private boolean isTvPlatform = false;
private HippyRecycleViewFocusHelper mFocusHelper = null;
private final int[] mScrollConsumedPair = new int[2];
private final Priority[] mNestedScrollPriority = {Priority.SELF, Priority.NOT_SET,
Priority.NOT_SET, Priority.NOT_SET, Priority.NOT_SET};
private int mNestedScrollAxesTouch;
private int mNestedScrollAxesNonTouch;

Expand Down Expand Up @@ -676,6 +676,20 @@ protected boolean isPullRefreshShowing() {
&& listAdapter.footerRefreshHelper.getVisibleSize() > 0;
}

@Override
public void setNestedScrollPriority(int direction, Priority priority) {
mNestedScrollPriority[direction] = priority;
}

@Override
public Priority getNestedScrollPriority(int direction) {
Priority result = mNestedScrollPriority[direction];
if (result == Priority.NOT_SET) {
result = mNestedScrollPriority[DIRECTION_ALL];
}
return result;
}

private int computeHorizontallyScrollDistance(int dx) {
if (dx < 0) {
return Math.max(dx, -computeHorizontalScrollOffset());
Expand Down Expand Up @@ -764,33 +778,36 @@ public void onStopNestedScroll(@NonNull View target, int type) {

@Override
public void onNestedScroll(@NonNull View target, int dxConsumed, int dyConsumed,
int dxUnconsumed,
int dyUnconsumed) {
int dxUnconsumed, int dyUnconsumed) {
onNestedScroll(target, dxConsumed, dyConsumed, dxUnconsumed, dyUnconsumed,
ViewCompat.TYPE_TOUCH);
}

@Override
public void onNestedScroll(@NonNull View target, int dxConsumed, int dyConsumed,
int dxUnconsumed,
int dyUnconsumed, int type) {
int dxUnconsumed, int dyUnconsumed, int type) {
// Step 1: process pull refresh
if (type == ViewCompat.TYPE_TOUCH) {
mScrollConsumedPair[0] = 0;
mScrollConsumedPair[1] = 0;
if (handlePullRefresh(dxUnconsumed, dyUnconsumed, mScrollConsumedPair)) {
dxConsumed += mScrollConsumedPair[0];
dyConsumed += mScrollConsumedPair[1];
dxUnconsumed -= mScrollConsumedPair[0];
dyUnconsumed -= mScrollConsumedPair[1];
if (HippyNestedScrollHelper.priorityOfX(target, dxUnconsumed) == Priority.SELF
|| HippyNestedScrollHelper.priorityOfY(target, dyUnconsumed) == Priority.SELF) {
mScrollConsumedPair[0] = 0;
mScrollConsumedPair[1] = 0;
if (handlePullRefresh(dxUnconsumed, dyUnconsumed, mScrollConsumedPair)) {
dxConsumed += mScrollConsumedPair[0];
dyConsumed += mScrollConsumedPair[1];
dxUnconsumed -= mScrollConsumedPair[0];
dyUnconsumed -= mScrollConsumedPair[1];
}
}
} else if (isPullRefreshShowing()) {
// don't respond non-touch scroll, prevent header/footer scroll to wrong position
return;
}
// Step 2: process the current View
int myDx = dxUnconsumed != 0 ? computeHorizontallyScrollDistance(dxUnconsumed) : 0;
int myDy = dyUnconsumed != 0 ? computeVerticallyScrollDistance(dyUnconsumed) : 0;
int myDx = HippyNestedScrollHelper.priorityOfX(target, dxUnconsumed) == Priority.SELF
? computeHorizontallyScrollDistance(dxUnconsumed) : 0;
int myDy = HippyNestedScrollHelper.priorityOfY(target, dyUnconsumed) == Priority.SELF
? computeVerticallyScrollDistance(dyUnconsumed) : 0;
if (myDx != 0 || myDy != 0) {
scrollBy(myDx, myDy);
dxConsumed += myDx;
Expand All @@ -799,8 +816,10 @@ public void onNestedScroll(@NonNull View target, int dxConsumed, int dyConsumed,
dyUnconsumed -= myDy;
}
// Step 3: dispatch to the parent for processing
int parentDx = dxUnconsumed;
int parentDy = dyUnconsumed;
int parentDx = HippyNestedScrollHelper.priorityOfX(this, dxUnconsumed) == Priority.NONE ? 0
: dxUnconsumed;
int parentDy = HippyNestedScrollHelper.priorityOfY(this, dyUnconsumed) == Priority.NONE ? 0
: dyUnconsumed;
if (parentDx != 0 || parentDy != 0) {
dispatchNestedScroll(dxConsumed, dyConsumed, parentDx, parentDy, null, type);
}
Expand All @@ -814,19 +833,47 @@ public void onNestedPreScroll(@NonNull View target, int dx, int dy, @NonNull int
@Override
public void onNestedPreScroll(@NonNull View target, int dx, int dy, @NonNull int[] consumed,
int type) {
// Dispatch to the parent for processing first
int parentDx = dx;
int parentDy = dy;
// Step 1: Dispatch to the parent for processing
int parentDx = HippyNestedScrollHelper.priorityOfX(this, dx) == Priority.NONE ? 0 : dx;
int parentDy = HippyNestedScrollHelper.priorityOfY(this, dy) == Priority.NONE ? 0 : dy;
if (parentDx != 0 || parentDy != 0) {
// Temporarily store `consumed` to reuse the Array
int consumedX = consumed[0];
int consumedY = consumed[1];
consumed[0] = 0;
consumed[1] = 0;
mScrollConsumedPair[0] = 0;
mScrollConsumedPair[1] = 0;
// must use super to prevent duplicate handlePullRefresh
super.dispatchNestedPreScroll(parentDx, parentDy, consumed, null, type);
consumed[0] += consumedX;
consumed[1] += consumedY;
super.dispatchNestedPreScroll(parentDx, parentDy, mScrollConsumedPair, null, type);
consumed[0] += mScrollConsumedPair[0];
consumed[1] += mScrollConsumedPair[1];
dx -= mScrollConsumedPair[0];
dy -= mScrollConsumedPair[1];
}
// Step 2: process pull refresh
if (HippyNestedScrollHelper.priorityOfX(target, dx) == Priority.PARENT
|| HippyNestedScrollHelper.priorityOfY(target, dy) == Priority.PARENT) {
if (type == ViewCompat.TYPE_TOUCH) {
mScrollConsumedPair[0] = 0;
mScrollConsumedPair[1] = 0;
if (handlePullRefresh(dx, dy, mScrollConsumedPair)) {
consumed[0] += mScrollConsumedPair[0];
consumed[1] += mScrollConsumedPair[1];
dx -= mScrollConsumedPair[0];
dy -= mScrollConsumedPair[1];
}
} else if (isPullRefreshShowing()) {
// don't respond non-touch scroll, prevent header/footer scroll to wrong position
consumed[0] += dx;
consumed[1] += dy;
return;
}
}
// Step 3: process the current View
int myDx = HippyNestedScrollHelper.priorityOfX(target, dx) == Priority.PARENT
? computeHorizontallyScrollDistance(dx) : 0;
int myDy = HippyNestedScrollHelper.priorityOfY(target, dy) == Priority.PARENT
? computeVerticallyScrollDistance(dy) : 0;
if (myDx != 0 || myDy != 0) {
consumed[0] += myDx;
consumed[1] += myDy;
scrollBy(myDx, myDy);
}
}

Expand Down
Loading

0 comments on commit 6ecf50a

Please sign in to comment.