feature:CoordinateLayout support multi set

This commit is contained in:
pengfei.zhou 2020-03-10 17:23:55 +08:00 committed by osborn
parent eceb7b9567
commit 01d736c5a6
12 changed files with 106 additions and 28 deletions

View File

@ -21,5 +21,7 @@ package pub.doric;
* @CreateDate: 2020-02-13 * @CreateDate: 2020-02-13
*/ */
public interface IDoricScrollable { public interface IDoricScrollable {
void setScrollChangeListener(DoricScrollChangeListener listener); void addScrollChangeListener(DoricScrollChangeListener listener);
void removeScrollChangeListener(DoricScrollChangeListener listener);
} }

View File

@ -119,8 +119,7 @@ public class CoordinatorPlugin extends DoricJavaPlugin {
final boolean finalIsNavBar = isNavBar; final boolean finalIsNavBar = isNavBar;
if (finalScrollNode instanceof IDoricScrollable) { if (finalScrollNode instanceof IDoricScrollable) {
((IDoricScrollable) finalScrollNode).addScrollChangeListener(new DoricScrollChangeListener() {
((IDoricScrollable) finalScrollNode).setScrollChangeListener(new DoricScrollChangeListener() {
@Override @Override
public void onScrollChange(View v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) { public void onScrollChange(View v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
if (scrollY <= startAnchor) { if (scrollY <= startAnchor) {

View File

@ -22,6 +22,9 @@ import com.github.pengfeizhou.jscore.JSONBuilder;
import com.github.pengfeizhou.jscore.JSObject; import com.github.pengfeizhou.jscore.JSObject;
import com.github.pengfeizhou.jscore.JSValue; import com.github.pengfeizhou.jscore.JSValue;
import java.util.HashSet;
import java.util.Set;
import pub.doric.DoricContext; import pub.doric.DoricContext;
import pub.doric.DoricScrollChangeListener; import pub.doric.DoricScrollChangeListener;
import pub.doric.IDoricScrollable; import pub.doric.IDoricScrollable;
@ -39,7 +42,7 @@ import pub.doric.widget.HVScrollView;
public class ScrollerNode extends SuperNode<HVScrollView> implements IDoricScrollable { public class ScrollerNode extends SuperNode<HVScrollView> implements IDoricScrollable {
private String mChildViewId; private String mChildViewId;
private ViewNode mChildNode; private ViewNode mChildNode;
private DoricScrollChangeListener doricScrollChangeListener; private Set<DoricScrollChangeListener> listeners = new HashSet<>();
private String onScrollFuncId; private String onScrollFuncId;
private String onScrollEndFuncId; private String onScrollEndFuncId;
@ -67,8 +70,8 @@ public class ScrollerNode extends SuperNode<HVScrollView> implements IDoricScrol
@Override @Override
public void onScrollChange(HVScrollView v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) { public void onScrollChange(HVScrollView v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
if (doricScrollChangeListener != null) { for (DoricScrollChangeListener listener : listeners) {
doricScrollChangeListener.onScrollChange(v, scrollX, scrollY, oldScrollX, oldScrollY); listener.onScrollChange(v, scrollX, scrollY, oldScrollX, oldScrollY);
} }
if (!TextUtils.isEmpty(onScrollFuncId)) { if (!TextUtils.isEmpty(onScrollFuncId)) {
callJSResponse(onScrollFuncId, new JSONBuilder() callJSResponse(onScrollFuncId, new JSONBuilder()
@ -147,8 +150,13 @@ public class ScrollerNode extends SuperNode<HVScrollView> implements IDoricScrol
} }
@Override @Override
public void setScrollChangeListener(DoricScrollChangeListener listener) { public void addScrollChangeListener(DoricScrollChangeListener listener) {
this.doricScrollChangeListener = listener; listeners.add(listener);
}
@Override
public void removeScrollChangeListener(DoricScrollChangeListener listener) {
listeners.remove(listener);
} }
@DoricMethod @DoricMethod

View File

@ -26,6 +26,9 @@ import androidx.recyclerview.widget.StaggeredGridLayoutManager;
import com.github.pengfeizhou.jscore.JSObject; import com.github.pengfeizhou.jscore.JSObject;
import com.github.pengfeizhou.jscore.JSValue; import com.github.pengfeizhou.jscore.JSValue;
import java.util.HashSet;
import java.util.Set;
import pub.doric.DoricContext; import pub.doric.DoricContext;
import pub.doric.DoricScrollChangeListener; import pub.doric.DoricScrollChangeListener;
import pub.doric.IDoricScrollable; import pub.doric.IDoricScrollable;
@ -76,7 +79,7 @@ public class FlowLayoutNode extends SuperNode<RecyclerView> implements IDoricScr
String onLoadMoreFuncId; String onLoadMoreFuncId;
boolean loadMore = false; boolean loadMore = false;
String loadMoreViewId; String loadMoreViewId;
private DoricScrollChangeListener doricScrollChangeListener; private Set<DoricScrollChangeListener> listeners = new HashSet<>();
public FlowLayoutNode(DoricContext doricContext) { public FlowLayoutNode(DoricContext doricContext) {
super(doricContext); super(doricContext);
@ -201,18 +204,24 @@ public class FlowLayoutNode extends SuperNode<RecyclerView> implements IDoricScr
@Override @Override
public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) { public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy); super.onScrolled(recyclerView, dx, dy);
if (doricScrollChangeListener != null) { for (DoricScrollChangeListener listener : listeners) {
int offsetX = recyclerView.computeHorizontalScrollOffset(); int offsetX = recyclerView.computeHorizontalScrollOffset();
int offsetY = recyclerView.computeVerticalScrollOffset(); int offsetY = recyclerView.computeVerticalScrollOffset();
doricScrollChangeListener.onScrollChange(recyclerView, offsetX, offsetY, offsetX - dx, offsetY - dy); listener.onScrollChange(recyclerView, offsetX, offsetY, offsetX - dx, offsetY - dy);
} }
} }
}); });
return recyclerView; return recyclerView;
} }
@Override @Override
public void setScrollChangeListener(DoricScrollChangeListener listener) { public void addScrollChangeListener(DoricScrollChangeListener listener) {
this.doricScrollChangeListener = listener; listeners.add(listener);
}
@Override
public void removeScrollChangeListener(DoricScrollChangeListener listener) {
listeners.remove(listener);
} }
} }

View File

@ -26,6 +26,9 @@ import androidx.recyclerview.widget.RecyclerView;
import com.github.pengfeizhou.jscore.JSObject; import com.github.pengfeizhou.jscore.JSObject;
import com.github.pengfeizhou.jscore.JSValue; import com.github.pengfeizhou.jscore.JSValue;
import java.util.HashSet;
import java.util.Set;
import pub.doric.DoricContext; import pub.doric.DoricContext;
import pub.doric.DoricScrollChangeListener; import pub.doric.DoricScrollChangeListener;
import pub.doric.IDoricScrollable; import pub.doric.IDoricScrollable;
@ -48,7 +51,7 @@ public class ListNode extends SuperNode<RecyclerView> implements IDoricScrollabl
SparseArray<String> itemValues = new SparseArray<>(); SparseArray<String> itemValues = new SparseArray<>();
boolean loadMore = false; boolean loadMore = false;
String loadMoreViewId; String loadMoreViewId;
private DoricScrollChangeListener doricScrollChangeListener; private Set<DoricScrollChangeListener> listeners = new HashSet<>();
public ListNode(DoricContext doricContext) { public ListNode(DoricContext doricContext) {
super(doricContext); super(doricContext);
@ -79,10 +82,10 @@ public class ListNode extends SuperNode<RecyclerView> implements IDoricScrollabl
@Override @Override
public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) { public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy); super.onScrolled(recyclerView, dx, dy);
if (doricScrollChangeListener != null) { for (DoricScrollChangeListener listener : listeners) {
int offsetX = recyclerView.computeHorizontalScrollOffset(); int offsetX = recyclerView.computeHorizontalScrollOffset();
int offsetY = recyclerView.computeVerticalScrollOffset(); int offsetY = recyclerView.computeVerticalScrollOffset();
doricScrollChangeListener.onScrollChange(recyclerView, offsetX, offsetY, offsetX - dx, offsetY - dy); listener.onScrollChange(recyclerView, offsetX, offsetY, offsetX - dx, offsetY - dy);
} }
} }
}); });
@ -162,7 +165,12 @@ public class ListNode extends SuperNode<RecyclerView> implements IDoricScrollabl
} }
@Override @Override
public void setScrollChangeListener(DoricScrollChangeListener listener) { public void addScrollChangeListener(DoricScrollChangeListener listener) {
this.doricScrollChangeListener = listener; listeners.add(listener);
}
@Override
public void removeScrollChangeListener(DoricScrollChangeListener listener) {
listeners.remove(listener);
} }
} }

View File

@ -5,6 +5,11 @@
#import <Foundation/Foundation.h> #import <Foundation/Foundation.h>
#import "DoricScrollableProtocol.h" #import "DoricScrollableProtocol.h"
typedef void (^DoricDidScrollBlock)(UIScrollView *__nonnull scrollView);
@protocol DoricScrollableProtocol <NSObject> @protocol DoricScrollableProtocol <NSObject>
@property(nonatomic, strong, nullable) void (^didScrollListener)(UIScrollView *__nonnull scrollView);
- (void)addDidScrollBlock:(__nonnull DoricDidScrollBlock)didScrollListener;
- (void)removeDidScrollBlock:(__nonnull DoricDidScrollBlock)didScrollListener;
@end @end

View File

@ -22,5 +22,4 @@
#import "DoricScrollableProtocol.h" #import "DoricScrollableProtocol.h"
@interface DoricFlowLayoutNode : DoricSuperNode<UICollectionView *> <DoricScrollableProtocol> @interface DoricFlowLayoutNode : DoricSuperNode<UICollectionView *> <DoricScrollableProtocol>
@property(nonatomic, strong, nullable) void (^didScrollListener)(UIScrollView *__nonnull scrollView);
@end @end

View File

@ -171,6 +171,7 @@ @interface DoricFlowLayoutNode () <UICollectionViewDataSource, UICollectionViewD
@property(nonatomic, copy) NSString *onLoadMoreFuncId; @property(nonatomic, copy) NSString *onLoadMoreFuncId;
@property(nonatomic, copy) NSString *loadMoreViewId; @property(nonatomic, copy) NSString *loadMoreViewId;
@property(nonatomic, assign) BOOL loadMore; @property(nonatomic, assign) BOOL loadMore;
@property(nonatomic, strong) NSMutableSet <DoricDidScrollBlock> *didScrollBlocks;
@end @end
@implementation DoricFlowLayoutNode @implementation DoricFlowLayoutNode
@ -370,8 +371,23 @@ - (NSInteger)doricFlowLayoutColumnCount {
} }
- (void)scrollViewDidScroll:(UIScrollView *)scrollView { - (void)scrollViewDidScroll:(UIScrollView *)scrollView {
if (self.didScrollListener) { for (DoricDidScrollBlock block in self.didScrollBlocks) {
self.didScrollListener(scrollView); block(scrollView);
} }
} }
- (NSMutableSet<DoricDidScrollBlock> *)didScrollBlocks {
if (!_didScrollBlocks) {
_didScrollBlocks = [NSMutableSet new];
}
return _didScrollBlocks;
}
- (void)addDidScrollBlock:(__nonnull DoricDidScrollBlock)didScrollListener {
[self.didScrollBlocks addObject:didScrollListener];
}
- (void)removeDidScrollBlock:(__nonnull DoricDidScrollBlock)didScrollListener {
[self.didScrollBlocks removeObject:didScrollListener];
}
@end @end

View File

@ -22,5 +22,4 @@
#import "DoricScrollableProtocol.h" #import "DoricScrollableProtocol.h"
@interface DoricListNode : DoricSuperNode<UITableView *> <DoricScrollableProtocol> @interface DoricListNode : DoricSuperNode<UITableView *> <DoricScrollableProtocol>
@property(nonatomic, strong, nullable) void (^didScrollListener)(UIScrollView *__nonnull scrollView);
@end @end

View File

@ -66,6 +66,7 @@ @interface DoricListNode () <UITableViewDataSource, UITableViewDelegate>
@property(nonatomic, copy) NSString *renderItemFuncId; @property(nonatomic, copy) NSString *renderItemFuncId;
@property(nonatomic, copy) NSString *loadMoreViewId; @property(nonatomic, copy) NSString *loadMoreViewId;
@property(nonatomic, assign) BOOL loadMore; @property(nonatomic, assign) BOOL loadMore;
@property(nonatomic, strong) NSMutableSet <DoricDidScrollBlock> *didScrollBlocks;
@end @end
@implementation DoricListNode @implementation DoricListNode
@ -250,8 +251,24 @@ - (DoricViewNode *)subNodeWithViewId:(NSString *)viewId {
} }
- (void)scrollViewDidScroll:(UIScrollView *)scrollView { - (void)scrollViewDidScroll:(UIScrollView *)scrollView {
if (self.didScrollListener) { for (DoricDidScrollBlock block in self.didScrollBlocks) {
self.didScrollListener(scrollView); block(scrollView);
} }
} }
- (NSMutableSet<DoricDidScrollBlock> *)didScrollBlocks {
if (!_didScrollBlocks) {
_didScrollBlocks = [NSMutableSet new];
}
return _didScrollBlocks;
}
- (void)addDidScrollBlock:(__nonnull DoricDidScrollBlock)didScrollListener {
[self.didScrollBlocks addObject:didScrollListener];
}
- (void)removeDidScrollBlock:(__nonnull DoricDidScrollBlock)didScrollListener {
[self.didScrollBlocks removeObject:didScrollListener];
}
@end @end

View File

@ -28,5 +28,4 @@
@end @end
@interface DoricScrollerNode : DoricSuperNode<DoricScrollView *> <DoricScrollableProtocol> @interface DoricScrollerNode : DoricSuperNode<DoricScrollView *> <DoricScrollableProtocol>
@property(nonatomic, strong, nullable) void (^didScrollListener)(UIScrollView *__nonnull scrollView);
@end @end

View File

@ -53,6 +53,7 @@ @interface DoricScrollerNode () <UIScrollViewDelegate>
@property(nonatomic, copy) NSString *childViewId; @property(nonatomic, copy) NSString *childViewId;
@property(nonatomic, copy) NSString *onScrollFuncId; @property(nonatomic, copy) NSString *onScrollFuncId;
@property(nonatomic, copy) NSString *onScrollEndFuncId; @property(nonatomic, copy) NSString *onScrollEndFuncId;
@property(nonatomic, strong) NSMutableSet <DoricDidScrollBlock> *didScrollBlocks;
@end @end
@implementation DoricScrollerNode @implementation DoricScrollerNode
@ -139,8 +140,8 @@ - (DoricViewNode *)subNodeWithViewId:(NSString *)viewId {
} }
- (void)scrollViewDidScroll:(UIScrollView *)scrollView { - (void)scrollViewDidScroll:(UIScrollView *)scrollView {
if (self.didScrollListener) { for (DoricDidScrollBlock block in self.didScrollBlocks) {
self.didScrollListener(scrollView); block(scrollView);
} }
if (self.onScrollFuncId) { if (self.onScrollFuncId) {
[self callJSResponse:self.onScrollFuncId, [self callJSResponse:self.onScrollFuncId,
@ -192,4 +193,20 @@ - (void)scrollBy:(NSDictionary *)params {
MIN(self.view.contentSize.height - self.view.height, MAX(0, offset.y + self.view.contentOffset.y))) MIN(self.view.contentSize.height - self.view.height, MAX(0, offset.y + self.view.contentOffset.y)))
animated:animated]; animated:animated];
} }
- (NSMutableSet<DoricDidScrollBlock> *)didScrollBlocks {
if (!_didScrollBlocks) {
_didScrollBlocks = [NSMutableSet new];
}
return _didScrollBlocks;
}
- (void)addDidScrollBlock:(__nonnull DoricDidScrollBlock)didScrollListener {
[self.didScrollBlocks addObject:didScrollListener];
}
- (void)removeDidScrollBlock:(__nonnull DoricDidScrollBlock)didScrollListener {
[self.didScrollBlocks removeObject:didScrollListener];
}
@end @end