Android: fix list scroll y inaccurate

This commit is contained in:
王劲鹏 2023-06-21 20:29:54 +08:00 committed by osborn
parent fffd5a9a3f
commit 22c47e379a

View File

@ -41,7 +41,9 @@ import com.github.pengfeizhou.jscore.JSValue;
import org.json.JSONArray;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.Callable;
@ -185,6 +187,42 @@ public class ListNode extends SuperNode<RecyclerView> implements IDoricScrollabl
return super.canScrollVertically();
}
private final Map<Integer, Integer> heightMap = new HashMap<>();
@Override
public void onLayoutCompleted(RecyclerView.State state) {
super.onLayoutCompleted(state);
int count = getChildCount();
for (int i = 0; i < count; i++) {
View view = getChildAt(i);
if (view != null) {
heightMap.put(recyclerView.getChildAdapterPosition(view), view.getHeight());
}
}
}
@Override
public int computeVerticalScrollOffset(RecyclerView.State state) {
if (getChildCount() == 0) {
return 0;
}
try {
int firstVisiblePosition = findFirstVisibleItemPosition();
View firstVisibleView = findViewByPosition(firstVisiblePosition);
int offsetY = 0;
if (firstVisibleView != null) {
offsetY = -(int) (firstVisibleView.getY());
}
for (int i = 0; i < firstVisiblePosition; i++) {
Integer height = heightMap.get(i);
offsetY += height == null ? 0 : height;
}
return offsetY;
} catch (Exception e) {
return 0;
}
}
@Override
public void smoothScrollToPosition(RecyclerView recyclerView, RecyclerView.State state, int position) {
if (scrollable) {