android:implement IDoricScrollable for Scroller,List and Flowlayout

This commit is contained in:
pengfei.zhou 2020-02-13 17:58:08 +08:00 committed by osborn
parent 00a5020847
commit 5b0e4959e1
5 changed files with 123 additions and 5 deletions

View File

@ -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);
}

View File

@ -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);
}

View File

@ -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<HVScrollView> {
public class ScrollerNode extends SuperNode<HVScrollView> 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<HVScrollView> {
@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<HVScrollView> {
}
}
@Override
public void setScrollChangeListener(DoricScrollChangeListener listener) {
this.doricScrollChangeListener = listener;
}
}

View File

@ -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<RecyclerView> {
public class FlowLayoutNode extends SuperNode<RecyclerView> implements IDoricScrollable {
private final FlowAdapter flowAdapter;
private final StaggeredGridLayoutManager staggeredGridLayoutManager = new StaggeredGridLayoutManager(
2,
@ -73,6 +76,7 @@ public class FlowLayoutNode extends SuperNode<RecyclerView> {
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> {
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;
}
}

View File

@ -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<RecyclerView> {
public class ListNode extends SuperNode<RecyclerView> implements IDoricScrollable {
private final ListAdapter listAdapter;
private String renderItemFuncId;
String onLoadMoreFuncId;
@ -44,6 +48,7 @@ public class ListNode extends SuperNode<RecyclerView> {
SparseArray<String> 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 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<RecyclerView> {
}
return null;
}
@Override
public void setScrollChangeListener(DoricScrollChangeListener listener) {
this.doricScrollChangeListener = listener;
}
}