Android: fix list scroll y inaccurate
This commit is contained in:
parent
fffd5a9a3f
commit
22c47e379a
@ -41,7 +41,9 @@ import com.github.pengfeizhou.jscore.JSValue;
|
|||||||
|
|
||||||
import org.json.JSONArray;
|
import org.json.JSONArray;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
|
import java.util.Map;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
import java.util.concurrent.Callable;
|
import java.util.concurrent.Callable;
|
||||||
|
|
||||||
@ -185,6 +187,42 @@ public class ListNode extends SuperNode<RecyclerView> implements IDoricScrollabl
|
|||||||
return super.canScrollVertically();
|
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
|
@Override
|
||||||
public void smoothScrollToPosition(RecyclerView recyclerView, RecyclerView.State state, int position) {
|
public void smoothScrollToPosition(RecyclerView recyclerView, RecyclerView.State state, int position) {
|
||||||
if (scrollable) {
|
if (scrollable) {
|
||||||
|
Reference in New Issue
Block a user