feat:Fix Flowlayout call onLoadMore multi times

This commit is contained in:
pengfeizhou 2021-02-02 19:52:39 +08:00 committed by osborn
parent 112e45af06
commit 2de59a307f
2 changed files with 21 additions and 4 deletions

View File

@ -45,6 +45,7 @@ class FlowAdapter extends RecyclerView.Adapter<FlowAdapter.DoricViewHolder> {
int itemCount = 0;
int batchCount = 15;
SparseArray<String> itemValues = new SparseArray<>();
private int loadAnchor = 0;
FlowAdapter(FlowLayoutNode flowLayoutNode) {
this.flowLayoutNode = flowLayoutNode;
@ -66,9 +67,8 @@ class FlowAdapter extends RecyclerView.Adapter<FlowAdapter.DoricViewHolder> {
holder.flowLayoutItemNode.setId(jsObject.getProperty("id").asString().value());
holder.flowLayoutItemNode.blend(jsObject.getProperty("props").asObject());
}
if (position >= this.itemCount) {
this.flowLayoutNode.callJSResponse(this.flowLayoutNode.onLoadMoreFuncId);
if (position >= this.itemCount && !TextUtils.isEmpty(this.flowLayoutNode.onLoadMoreFuncId)) {
callLoadMore();
StaggeredGridLayoutManager.LayoutParams layoutParams = new StaggeredGridLayoutManager.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
holder.itemView.getLayoutParams().height
@ -143,6 +143,14 @@ class FlowAdapter extends RecyclerView.Adapter<FlowAdapter.DoricViewHolder> {
}
}
private void callLoadMore() {
if (loadAnchor != itemCount) {
loadAnchor = itemCount;
this.flowLayoutNode.callJSResponse(this.flowLayoutNode.onLoadMoreFuncId);
}
}
static class DoricViewHolder extends RecyclerView.ViewHolder {
FlowLayoutItemNode flowLayoutItemNode;

View File

@ -158,6 +158,7 @@ @interface DoricFlowLayoutNode () <UICollectionViewDataSource, UICollectionViewD
@property(nonatomic, copy) NSString *onScrollFuncId;
@property(nonatomic, copy) NSString *onScrollEndFuncId;
@property(nonatomic, strong) DoricJSDispatcher *jsDispatcher;
@property(nonatomic, assign) NSUInteger loadAnchor;
@end
@implementation DoricFlowLayoutNode
@ -298,6 +299,14 @@ - (void)callItem:(NSUInteger)position size:(CGSize)size {
[self.view.collectionViewLayout invalidateLayout];
}
- (void)callLoadMore {
if (self.itemCount != self.loadAnchor) {
self.loadAnchor = self.itemCount;
[self callJSResponse:self.onLoadMoreFuncId, nil];
}
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return self.itemCount + (self.loadMore ? 1 : 0);
}
@ -320,7 +329,7 @@ - (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collection
[node blend:props];
node.view.width = (collectionView.width - (self.columnCount - 1) * self.columnSpace) / self.columnCount;
if (position > 0 && position >= self.itemCount && self.onLoadMoreFuncId) {
[self callJSResponse:self.onLoadMoreFuncId, nil];
[self callLoadMore];
}
[node.view.doricLayout apply];
[node requestLayout];