list add findVisibleItems api

This commit is contained in:
pengfei.zhou
2021-10-11 18:02:56 +08:00
committed by osborn
parent 0e77c62e13
commit 8fd2477c81
4 changed files with 132 additions and 0 deletions

View File

@@ -20,6 +20,7 @@ import android.text.TextUtils;
import android.view.View;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;
@@ -28,6 +29,9 @@ import com.github.pengfeizhou.jscore.JSONBuilder;
import com.github.pengfeizhou.jscore.JSObject;
import com.github.pengfeizhou.jscore.JSValue;
import org.json.JSONArray;
import org.json.JSONObject;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.Callable;
@@ -36,6 +40,7 @@ import pub.doric.DoricContext;
import pub.doric.DoricScrollChangeListener;
import pub.doric.IDoricScrollable;
import pub.doric.async.AsyncResult;
import pub.doric.extension.bridge.DoricMethod;
import pub.doric.extension.bridge.DoricPlugin;
import pub.doric.shader.SuperNode;
import pub.doric.shader.ViewNode;
@@ -326,6 +331,53 @@ public class FlowLayoutNode extends SuperNode<RecyclerView> implements IDoricScr
listeners.remove(listener);
}
private int calibratePosition(int position) {
if (hasHeader() && position == 0) {
return -11;
}
if (hasFooter() && position == this.itemCount
+ (this.loadMore ? 1 : 0)
+ (this.hasHeader() ? 1 : 0)
+ (this.hasFooter() ? 1 : 0) - 1) {
return -12;
}
if (position >= this.itemCount + (this.hasHeader() ? 1 : 0)) {
return -10;
}
if (this.hasHeader()) {
return position - 1;
}
return position;
}
@DoricMethod
public JSONArray findVisibleItems() {
int[] startPos = staggeredGridLayoutManager.findFirstVisibleItemPositions(null);
int[] endPos = staggeredGridLayoutManager.findLastVisibleItemPositions(null);
JSONArray jsonArray = new JSONArray();
for (int i = 0; i < staggeredGridLayoutManager.getSpanCount(); i++) {
jsonArray.put(new JSONBuilder()
.put("first", calibratePosition(startPos[i]))
.put("last", calibratePosition(endPos[i]))
.toJSONObject());
}
return jsonArray;
}
@DoricMethod
public JSONArray findCompletelyVisibleItems() {
int[] startPos = staggeredGridLayoutManager.findFirstCompletelyVisibleItemPositions(null);
int[] endPos = staggeredGridLayoutManager.findFirstCompletelyVisibleItemPositions(null);
JSONArray jsonArray = new JSONArray();
for (int i = 0; i < staggeredGridLayoutManager.getSpanCount(); i++) {
jsonArray.put(new JSONBuilder()
.put("first", calibratePosition(startPos[i]))
.put("last", calibratePosition(endPos[i]))
.toJSONObject());
}
return jsonArray;
}
boolean hasHeader() {
return !TextUtils.isEmpty(this.headerViewId);
}

View File

@@ -26,12 +26,15 @@ import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.github.pengfeizhou.jscore.JSArray;
import com.github.pengfeizhou.jscore.JSDecoder;
import com.github.pengfeizhou.jscore.JSNumber;
import com.github.pengfeizhou.jscore.JSONBuilder;
import com.github.pengfeizhou.jscore.JSObject;
import com.github.pengfeizhou.jscore.JSValue;
import org.json.JSONObject;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.Callable;
@@ -300,6 +303,47 @@ public class ListNode extends SuperNode<RecyclerView> implements IDoricScrollabl
moveToPosition(pos.toInt(), animated);
}
private int calibratePosition(int position) {
if (hasHeader() && position == 0) {
return -11;
}
if (hasFooter() && position == this.itemCount
+ (this.loadMore ? 1 : 0)
+ (this.hasHeader() ? 1 : 0)
+ (this.hasFooter() ? 1 : 0) - 1) {
return -12;
}
if (position >= this.itemCount + (this.hasHeader() ? 1 : 0)) {
return -10;
}
if (this.hasHeader()) {
return position - 1;
}
return position;
}
@DoricMethod
public JSONObject findVisibleItems() {
LinearLayoutManager linearLayoutManager = (LinearLayoutManager) this.mView.getLayoutManager();
assert linearLayoutManager != null;
int startPos = linearLayoutManager.findFirstVisibleItemPosition();
int endPos = linearLayoutManager.findLastVisibleItemPosition();
return new JSONBuilder()
.put("first", calibratePosition(startPos))
.put("last", calibratePosition(endPos)).toJSONObject();
}
@DoricMethod
public JSONObject findCompletelyVisibleItems() {
LinearLayoutManager linearLayoutManager = (LinearLayoutManager) this.mView.getLayoutManager();
assert linearLayoutManager != null;
int startPos = linearLayoutManager.findFirstCompletelyVisibleItemPosition();
int endPos = linearLayoutManager.findLastCompletelyVisibleItemPosition();
return new JSONBuilder()
.put("first", calibratePosition(startPos))
.put("last", calibratePosition(endPos)).toJSONObject();
}
private void moveToPosition(int pos, boolean smooth) {
if (mView.getLayoutManager() == null) {
return;