gpt4 book ai didi

java - Recyclerview 布局管理器在 Android 中滚动后获取位置 View

转载 作者:行者123 更新时间:2023-12-04 09:39:28 24 4
gpt4 key购买 nike

Recyclerview正在滚动它的 LinearLayoutManager像这样lm.scrollToPositionWithOffset(position, offset) .如何在滚动前获取滚动位置的 View ?滚动后将滚动返回 null 的 View ,因为仍未创建。我试过 Runnable , ObserveronLayoutCompleted但仍然为空。如何获得 View ?

lm.scrollToPositionWithOffset(position, offset);

recyclerView.post(new Runnable(){
@Override
public void run(){
View v1 = recyclerView.getChildAt(position); // returning null.
View v2 = recyclerView.getLayoutManager().getChildAt(position); // returning null.
}
});

recyclerView.getViewTreeObserver()
.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
View v1 = recyclerView.getChildAt(position); // returning null.
View v2 = recyclerView.getLayoutManager().getChildAt(position); // returning null.
recyclerView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
}});

@Override // in LinearLayoutManager
public void onLayoutCompleted(RecyclerView.State state) {
super.onLayoutCompleted(state);
View v1 = recyclerView.getChildAt(position); // returning null.
View v2 = this.getChildAt(position); // returning null.
}

最佳答案

View v = lm.getChildAt(position);
lm.scrollToPositionWithOffset(position, offset);
v.post(new Runnable() {
@Override
public void run() {
// View is ready here.
});

关于java - Recyclerview 布局管理器在 Android 中滚动后获取位置 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62398571/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com