use*_*520 51 android listview selection smooth-scrolling android-arrayadapter
在将一个元素添加到与listview关联的arrayadapter之后,我试图平滑地滚动到列表的最后一个元素.问题是它只是滚动到一个随机位置
arrayadapter.add(item);
//DOES NOT WORK CORRECTLY:
listview.smoothScrollToPosition(arrayadapter.getCount()-1);
//WORKS JUST FINE:
listview.setSelection(arrayadapter.getCount()-1);
Run Code Online (Sandbox Code Playgroud)
Mar*_*ini 58
您可能希望告诉ListView在UI线程可以处理它时发布滚动(这就是为什么它不能正确滚动).SmoothScroll需要做很多工作,而不是只是去一个忽略速度/时间/等的位置.("动画"所需).
因此你应该做的事情如下:
getListView().post(new Runnable() {
@Override
public void run() {
getListView().smoothScrollToPosition(pos);
}
});
Run Code Online (Sandbox Code Playgroud)
Lar*_*erg 13
(从我的回答中复制:smoothScrollToPositionFromTop()并不总是像它应该的那样工作)
这是一个已知的错误.请参阅https://code.google.com/p/android/issues/detail?id=36062
但是,我实现了这个解决方案,处理可能发生的所有边缘情况:
第一次调用smothScrollToPositionFromTop(position)
然后,当滚动完成时,调用setSelection(position)
.后一个调用通过直接跳到所需位置来纠正不完整的滚动.这样做,用户仍然会将其动画滚动到此位置.
我在两个辅助方法中实现了这个解决方法:
smoothScrollToPosition()
public static void smoothScrollToPosition(final AbsListView view, final int position) {
View child = getChildAtPosition(view, position);
// There's no need to scroll if child is already at top or view is already scrolled to its end
if ((child != null) && ((child.getTop() == 0) || ((child.getTop() > 0) && !view.canScrollVertically(1)))) {
return;
}
view.setOnScrollListener(new AbsListView.OnScrollListener() {
@Override
public void onScrollStateChanged(final AbsListView view, final int scrollState) {
if (scrollState == SCROLL_STATE_IDLE) {
view.setOnScrollListener(null);
// Fix for scrolling bug
new Handler().post(new Runnable() {
@Override
public void run() {
view.setSelection(position);
}
});
}
}
@Override
public void onScroll(final AbsListView view, final int firstVisibleItem, final int visibleItemCount,
final int totalItemCount) { }
});
// Perform scrolling to position
new Handler().post(new Runnable() {
@Override
public void run() {
view.smoothScrollToPositionFromTop(position, 0);
}
});
}
Run Code Online (Sandbox Code Playgroud)
getChildAtPosition()
public static View getChildAtPosition(final AdapterView view, final int position) {
final int index = position - view.getFirstVisiblePosition();
if ((index >= 0) && (index < view.getChildCount())) {
return view.getChildAt(index);
} else {
return null;
}
}
Run Code Online (Sandbox Code Playgroud)
您应该使用setSelection()方法.
使用 LayoutManager 平滑滚动
layoutManager.smoothScrollToPosition(recyclerView, new RecyclerView.State(), position);
Run Code Online (Sandbox Code Playgroud)
打完电话之后还arrayadapter.notifyDataSetChanged()
打吗arrayadapter.add()
?另外可以肯定的是,smoothScrollToPosition
是否setSelection
有可用的方法,而ListView
不是arrayadapter
您上面提到的那样。
无论如何,看看这是否有帮助: smoothScrollToPosition after notificationDataSetChanged not work in android
归档时间: |
|
查看次数: |
53829 次 |
最近记录: |