diff --git a/doric-android/doric/src/main/java/pub/doric/shader/slider/SliderNode.java b/doric-android/doric/src/main/java/pub/doric/shader/slider/SliderNode.java index 9e303789..8f01dbe5 100644 --- a/doric-android/doric/src/main/java/pub/doric/shader/slider/SliderNode.java +++ b/doric-android/doric/src/main/java/pub/doric/shader/slider/SliderNode.java @@ -60,6 +60,8 @@ public class SliderNode extends SuperNode { private int slidePosition; private boolean needSlideToPosition; + private PagerSnapHelper snapHelper; + public SliderNode(DoricContext doricContext) { super(doricContext); this.slideAdapter = new SlideAdapter(this); @@ -69,7 +71,6 @@ public class SliderNode extends SuperNode { protected RecyclerView build() { final RecyclerView recyclerView = new RecyclerView(getContext()); - final SliderLayoutManager layoutManager = new SliderLayoutManager(getContext(), LinearLayoutManager.HORIZONTAL, false) { @Override public boolean canScrollHorizontally() { @@ -92,7 +93,7 @@ public class SliderNode extends SuperNode { }; recyclerView.setLayoutManager(layoutManager); - final PagerSnapHelper snapHelper = new PagerSnapHelper(); + snapHelper = new PagerSnapHelper(); snapHelper.attachToRecyclerView(recyclerView); recyclerView.addItemDecoration(new RecyclerView.ItemDecoration() { @@ -332,6 +333,20 @@ public class SliderNode extends SuperNode { } else { 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)) { callJSResponse(onPageSlidedFuncId, page); diff --git a/doric-demo/src/SliderStyleDemo.ts b/doric-demo/src/SliderStyleDemo.ts index 8bed4d50..b600be8a 100644 --- a/doric-demo/src/SliderStyleDemo.ts +++ b/doric-demo/src/SliderStyleDemo.ts @@ -33,5 +33,9 @@ class SliderStyleDemo extends Panel { heightSpec: LayoutSpec.MOST, } })) + + setTimeout(() => { + pager.slidePage(this.context, 2, false); + }, 200) } } \ No newline at end of file