Skip to content

Commit

Permalink
viewpager:support scrollEventThrottle
Browse files Browse the repository at this point in the history
  • Loading branch information
arvinwli committed Oct 30, 2023
1 parent f2ba451 commit 5875169
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ export default class PagerExample extends React.Component {
onPageSelected={this.onPageSelected}
onPageScrollStateChanged={this.onPageScrollStateChanged}
onPageScroll={this.onPageScroll}
scrollEventThrottle={1000}
>
{
[
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ public void run() {
private final int[] mScrollOffsetPair = new int[2];
private int mNestedScrollOffset = 0;

protected int mScrollEventThrottle = 400; // 400ms最多回调一次


private void init(Context context, boolean isVertical) {
setCallPageChangedOnFirstLayout(true);
setEnableReLayoutOnAttachToWindow(false);
Expand Down Expand Up @@ -328,6 +331,10 @@ public void setOverflow(String overflow) {
invalidate();
}

public void setScrollEventThrottle(int scrollEventThrottle) {
mScrollEventThrottle = scrollEventThrottle;
}

@Override
public boolean onStartNestedScroll(@NonNull View child, @NonNull View target, int axes) {
if (!isScrollEnabled()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,10 @@ public void setOverflow(HippyViewPager pager, String overflow) {
pager.setOverflow(overflow);
}

@HippyControllerProps(name = "scrollEventThrottle", defaultType = HippyControllerProps.NUMBER, defaultNumber = 30.0D)
public void setScrollEventThrottle(HippyViewPager pager,int scrollEventThrottle) {
pager.setScrollEventThrottle(scrollEventThrottle);
}
private void resolveInvalidParams(@Nullable Promise promise) {
if (promise != null) {
String msg = "Invalid parameter!";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

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

import android.os.SystemClock;
import android.view.View;

import androidx.annotation.NonNull;
Expand All @@ -34,14 +35,30 @@ public class ViewPagerPageChangeListener implements ViewPager.OnPageChangeListen
private int mLastPageIndex = 0;
private int mCurrPageIndex = 0;
private final HippyViewPager mPager;
private long mLastScrollEventTimeStamp = -1;

public ViewPagerPageChangeListener(@NonNull HippyViewPager pager) {
mPager = pager;
}

/**
* 检查是否需要发送事件
* @return
*/
protected boolean checkSendOnScrollEvent() {
if (mPager.isScrollEnabled()) {
long currTime = SystemClock.elapsedRealtime();
if (currTime - mLastScrollEventTimeStamp >= mPager.mScrollEventThrottle) {
mLastScrollEventTimeStamp = currTime;
return true;
}
}
return false;
}

@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
if (mPager != null) {
if (mPager != null && checkSendOnScrollEvent()) {
Map<String, Object> params = new HashMap<>();
params.put(PAGE_ITEM_POSITION, position);
params.put(PAGE_ITEM_OFFSET, positionOffset);
Expand Down Expand Up @@ -91,6 +108,7 @@ private void onScrollStateChangeToIdle() {
params.put(PAGE_ITEM_POSITION, mLastPageIndex);
EventUtils.sendComponentEvent(lastView, EventUtils.EVENT_PAGE_ITEM_DID_DISAPPEAR, params);
mLastPageIndex = mCurrPageIndex;
mLastScrollEventTimeStamp = -1;
}

@Override
Expand Down

0 comments on commit 5875169

Please sign in to comment.