Skip to content

Commit

Permalink
Android: customize snap helper for handle scrollTriggeredManually
Browse files Browse the repository at this point in the history
  • Loading branch information
jingpeng authored and penfeizhou committed Dec 22, 2023
1 parent 97d7cdb commit 78b782d
Showing 1 changed file with 21 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.LinearSmoothScroller;
import androidx.recyclerview.widget.PagerSnapHelper;
Expand All @@ -35,7 +36,6 @@
import java.util.concurrent.Callable;

import pub.doric.DoricContext;
import pub.doric.DoricScrollChangeListener;
import pub.doric.async.AsyncResult;
import pub.doric.extension.bridge.DoricMethod;
import pub.doric.extension.bridge.DoricPlugin;
Expand Down Expand Up @@ -74,6 +74,8 @@ public class SliderNode extends SuperNode<RecyclerView> {
private String onScrollFuncId;
private final DoricJSDispatcher jsDispatcher = new DoricJSDispatcher();

private boolean scrollTriggeredManually;

public SliderNode(DoricContext doricContext) {
super(doricContext);
this.slideAdapter = new SlideAdapter(this);
Expand Down Expand Up @@ -105,7 +107,23 @@ public void smoothScrollToPosition(RecyclerView recyclerView, RecyclerView.State
};

recyclerView.setLayoutManager(layoutManager);
snapHelper = new PagerSnapHelper();
snapHelper = new PagerSnapHelper() {
@Nullable
@Override
public View findSnapView(RecyclerView.LayoutManager layoutManager) {
if (scrollTriggeredManually) {
mView.post(new Runnable() {
@Override
public void run() {
scrollTriggeredManually = false;
}
});
return null;
} else {
return super.findSnapView(layoutManager);
}
}
};
snapHelper.attachToRecyclerView(recyclerView);

recyclerView.addItemDecoration(new RecyclerView.ItemDecoration() {
Expand Down Expand Up @@ -437,6 +455,7 @@ public void scrollBy(JSObject params) {
}
float dx = params.getProperty("dx").asNumber().toFloat();
if (animated) {
scrollTriggeredManually = true;
mView.smoothScrollBy(DoricUtils.dp2px(dx), 0);
} else {
mView.scrollBy(DoricUtils.dp2px(dx), 0);
Expand Down

0 comments on commit 78b782d

Please sign in to comment.