diff --git a/doric-android/doric/src/main/java/pub/doric/DoricScrollChangeListener.java b/doric-android/doric/src/main/java/pub/doric/DoricScrollChangeListener.java new file mode 100644 index 00000000..63791518 --- /dev/null +++ b/doric-android/doric/src/main/java/pub/doric/DoricScrollChangeListener.java @@ -0,0 +1,37 @@ +/* + * Copyright [2019] [Doric.Pub] + * + * 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 pub.doric; + +import android.view.View; + +/** + * @Description: pub.doric + * @Author: pengfei.zhou + * @CreateDate: 2020-02-13 + */ +public interface DoricScrollChangeListener { + /** + * Called when the scroll position of a view changes. + * + * @param v The view whose scroll position has changed. + * @param scrollX Current horizontal scroll origin. + * @param scrollY Current vertical scroll origin. + * @param oldScrollX Previous horizontal scroll origin. + * @param oldScrollY Previous vertical scroll origin. + */ + void onScrollChange(View v, int scrollX, int scrollY, + int oldScrollX, int oldScrollY); +} diff --git a/doric-android/doric/src/main/java/pub/doric/IDoricScrollable.java b/doric-android/doric/src/main/java/pub/doric/IDoricScrollable.java new file mode 100644 index 00000000..0de28bac --- /dev/null +++ b/doric-android/doric/src/main/java/pub/doric/IDoricScrollable.java @@ -0,0 +1,25 @@ +/* + * Copyright [2019] [Doric.Pub] + * + * 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 pub.doric; + +/** + * @Description: pub.doric + * @Author: pengfei.zhou + * @CreateDate: 2020-02-13 + */ +public interface IDoricScrollable { + void setScrollChangeListener(DoricScrollChangeListener listener); +} diff --git a/doric-android/doric/src/main/java/pub/doric/shader/ScrollerNode.java b/doric-android/doric/src/main/java/pub/doric/shader/ScrollerNode.java index 18ccf135..57c0a8e9 100644 --- a/doric-android/doric/src/main/java/pub/doric/shader/ScrollerNode.java +++ b/doric-android/doric/src/main/java/pub/doric/shader/ScrollerNode.java @@ -15,11 +15,12 @@ */ package pub.doric.shader; - import com.github.pengfeizhou.jscore.JSObject; import com.github.pengfeizhou.jscore.JSValue; import pub.doric.DoricContext; +import pub.doric.DoricScrollChangeListener; +import pub.doric.IDoricScrollable; import pub.doric.extension.bridge.DoricPlugin; import pub.doric.widget.HVScrollView; @@ -29,9 +30,10 @@ import pub.doric.widget.HVScrollView; * @CreateDate: 2019-11-18 */ @DoricPlugin(name = "Scroller") -public class ScrollerNode extends SuperNode { +public class ScrollerNode extends SuperNode implements IDoricScrollable { private String mChildViewId; private ViewNode mChildNode; + private DoricScrollChangeListener doricScrollChangeListener; public ScrollerNode(DoricContext doricContext) { super(doricContext); @@ -51,7 +53,16 @@ public class ScrollerNode extends SuperNode { @Override protected HVScrollView build() { - return new HVScrollView(getContext()); + HVScrollView hvScrollView = new HVScrollView(getContext()); + hvScrollView.setOnScrollChangeListener(new HVScrollView.OnScrollChangeListener() { + @Override + public void onScrollChange(HVScrollView v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) { + if (doricScrollChangeListener != null) { + doricScrollChangeListener.onScrollChange(v, scrollX, scrollY, oldScrollX, oldScrollY); + } + } + }); + return hvScrollView; } @Override @@ -98,4 +109,8 @@ public class ScrollerNode extends SuperNode { } } + @Override + public void setScrollChangeListener(DoricScrollChangeListener listener) { + this.doricScrollChangeListener = listener; + } } diff --git a/doric-android/doric/src/main/java/pub/doric/shader/flowlayout/FlowLayoutNode.java b/doric-android/doric/src/main/java/pub/doric/shader/flowlayout/FlowLayoutNode.java index c01c543c..d06808c7 100644 --- a/doric-android/doric/src/main/java/pub/doric/shader/flowlayout/FlowLayoutNode.java +++ b/doric-android/doric/src/main/java/pub/doric/shader/flowlayout/FlowLayoutNode.java @@ -16,6 +16,7 @@ package pub.doric.shader.flowlayout; import android.graphics.Rect; +import android.util.Log; import android.view.View; import androidx.annotation.NonNull; @@ -26,6 +27,8 @@ import com.github.pengfeizhou.jscore.JSObject; import com.github.pengfeizhou.jscore.JSValue; import pub.doric.DoricContext; +import pub.doric.DoricScrollChangeListener; +import pub.doric.IDoricScrollable; import pub.doric.extension.bridge.DoricPlugin; import pub.doric.shader.SuperNode; import pub.doric.shader.ViewNode; @@ -37,7 +40,7 @@ import pub.doric.utils.DoricUtils; * @CreateDate: 2019-11-28 */ @DoricPlugin(name = "FlowLayout") -public class FlowLayoutNode extends SuperNode { +public class FlowLayoutNode extends SuperNode implements IDoricScrollable { private final FlowAdapter flowAdapter; private final StaggeredGridLayoutManager staggeredGridLayoutManager = new StaggeredGridLayoutManager( 2, @@ -73,6 +76,7 @@ public class FlowLayoutNode extends SuperNode { String onLoadMoreFuncId; boolean loadMore = false; String loadMoreViewId; + private DoricScrollChangeListener doricScrollChangeListener; public FlowLayoutNode(DoricContext doricContext) { super(doricContext); @@ -193,6 +197,22 @@ public class FlowLayoutNode extends SuperNode { recyclerView.setLayoutManager(staggeredGridLayoutManager); recyclerView.setAdapter(flowAdapter); recyclerView.addItemDecoration(spacingItemDecoration); + recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() { + @Override + public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) { + super.onScrolled(recyclerView, dx, dy); + if (doricScrollChangeListener != null) { + int offsetX = recyclerView.computeHorizontalScrollOffset(); + int offsetY = recyclerView.computeVerticalScrollOffset(); + doricScrollChangeListener.onScrollChange(recyclerView, offsetX, offsetY, offsetX - dx, offsetY - dy); + } + } + }); return recyclerView; } + + @Override + public void setScrollChangeListener(DoricScrollChangeListener listener) { + this.doricScrollChangeListener = listener; + } } diff --git a/doric-android/doric/src/main/java/pub/doric/shader/list/ListNode.java b/doric-android/doric/src/main/java/pub/doric/shader/list/ListNode.java index 4c2316c2..d37408b7 100644 --- a/doric-android/doric/src/main/java/pub/doric/shader/list/ListNode.java +++ b/doric-android/doric/src/main/java/pub/doric/shader/list/ListNode.java @@ -15,9 +15,11 @@ */ package pub.doric.shader.list; +import android.util.Log; import android.util.SparseArray; import android.view.View; +import androidx.annotation.NonNull; import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.RecyclerView; @@ -25,6 +27,8 @@ import com.github.pengfeizhou.jscore.JSObject; import com.github.pengfeizhou.jscore.JSValue; import pub.doric.DoricContext; +import pub.doric.DoricScrollChangeListener; +import pub.doric.IDoricScrollable; import pub.doric.extension.bridge.DoricPlugin; import pub.doric.shader.SuperNode; import pub.doric.shader.ViewNode; @@ -35,7 +39,7 @@ import pub.doric.shader.ViewNode; * @CreateDate: 2019-11-12 */ @DoricPlugin(name = "List") -public class ListNode extends SuperNode { +public class ListNode extends SuperNode implements IDoricScrollable { private final ListAdapter listAdapter; private String renderItemFuncId; String onLoadMoreFuncId; @@ -44,6 +48,7 @@ public class ListNode extends SuperNode { SparseArray itemValues = new SparseArray<>(); boolean loadMore = false; String loadMoreViewId; + private DoricScrollChangeListener doricScrollChangeListener; public ListNode(DoricContext doricContext) { super(doricContext); @@ -70,6 +75,17 @@ public class ListNode extends SuperNode { RecyclerView recyclerView = new RecyclerView(getContext()); recyclerView.setLayoutManager(new LinearLayoutManager(getContext())); recyclerView.setAdapter(this.listAdapter); + recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() { + @Override + public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) { + super.onScrolled(recyclerView, dx, dy); + if (doricScrollChangeListener != null) { + int offsetX = recyclerView.computeHorizontalScrollOffset(); + int offsetY = recyclerView.computeVerticalScrollOffset(); + doricScrollChangeListener.onScrollChange(recyclerView, offsetX, offsetY, offsetX - dx, offsetY - dy); + } + } + }); return recyclerView; } @@ -144,4 +160,9 @@ public class ListNode extends SuperNode { } return null; } + + @Override + public void setScrollChangeListener(DoricScrollChangeListener listener) { + this.doricScrollChangeListener = listener; + } }