Android: fix inaccurate scroll when slider in gallery style

This commit is contained in:
王劲鹏 2023-06-14 11:08:22 +08:00 committed by osborn
parent 1b105d70e4
commit 722d0873d4
2 changed files with 21 additions and 2 deletions

View File

@ -60,6 +60,8 @@ public class SliderNode extends SuperNode<RecyclerView> {
private int slidePosition; private int slidePosition;
private boolean needSlideToPosition; private boolean needSlideToPosition;
private PagerSnapHelper snapHelper;
public SliderNode(DoricContext doricContext) { public SliderNode(DoricContext doricContext) {
super(doricContext); super(doricContext);
this.slideAdapter = new SlideAdapter(this); this.slideAdapter = new SlideAdapter(this);
@ -69,7 +71,6 @@ public class SliderNode extends SuperNode<RecyclerView> {
protected RecyclerView build() { protected RecyclerView build() {
final RecyclerView recyclerView = new RecyclerView(getContext()); final RecyclerView recyclerView = new RecyclerView(getContext());
final SliderLayoutManager layoutManager = new SliderLayoutManager(getContext(), LinearLayoutManager.HORIZONTAL, false) { final SliderLayoutManager layoutManager = new SliderLayoutManager(getContext(), LinearLayoutManager.HORIZONTAL, false) {
@Override @Override
public boolean canScrollHorizontally() { public boolean canScrollHorizontally() {
@ -92,7 +93,7 @@ public class SliderNode extends SuperNode<RecyclerView> {
}; };
recyclerView.setLayoutManager(layoutManager); recyclerView.setLayoutManager(layoutManager);
final PagerSnapHelper snapHelper = new PagerSnapHelper(); snapHelper = new PagerSnapHelper();
snapHelper.attachToRecyclerView(recyclerView); snapHelper.attachToRecyclerView(recyclerView);
recyclerView.addItemDecoration(new RecyclerView.ItemDecoration() { recyclerView.addItemDecoration(new RecyclerView.ItemDecoration() {
@ -332,6 +333,20 @@ public class SliderNode extends SuperNode<RecyclerView> {
} else { } else {
mView.scrollToPosition(page); mView.scrollToPosition(page);
} }
if (slideStyle.equals("gallery")) {
mView.post(new Runnable() {
@Override
public void run() {
View snapView = snapHelper.findSnapView(mView.getLayoutManager());
if (snapView != null) {
int[] distance = snapHelper.calculateDistanceToFinalSnap(mView.getLayoutManager(), snapView);
mView.smoothScrollBy(distance[0], distance[1]);
}
}
});
}
} }
if (!TextUtils.isEmpty(onPageSlidedFuncId)) { if (!TextUtils.isEmpty(onPageSlidedFuncId)) {
callJSResponse(onPageSlidedFuncId, page); callJSResponse(onPageSlidedFuncId, page);

View File

@ -33,5 +33,9 @@ class SliderStyleDemo extends Panel {
heightSpec: LayoutSpec.MOST, heightSpec: LayoutSpec.MOST,
} }
})) }))
setTimeout(() => {
pager.slidePage(this.context, 2, false);
}, 200)
} }
} }