如何通过动画自动停止滚动屏幕前面的项目。目前它可以使用以下代码,但滚动速度非常快。
所以我的要求是通过动画执行此操作,以便用户可以看到listview的滑动。
以下是我的代码,但我没有得到预期的结果。
listview.setOnScrollListener(new AbsListView.OnScrollListener() {
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
if (scrollState == SCROLL_STATE_IDLE) {
View child = layout.getChildAt(0); // first visible child
Rect r = new Rect(0, 0, child.getWidth(), child.getHeight()); // set this initially, as required by the docs
double height = child.getHeight() * 1.0;
layout.getChildVisibleRect(child, r, null);
if (Math.abs(r.height()) != height)
{
//only smooth scroll when not scroll to correct position
if (Math.abs(r.height()) < height / 2.0)
{
listview.postDelayed(new Runnable() {
@Override
public void run() {
listview.smoothScrollBy(listview.getFirstVisiblePosition(), 10000);
}
}, 500);
} else if (Math.abs(r.height()) > height / 2.0)
{
listview.postDelayed(new Runnable() {
@Override
public void run() {
listview.smoothScrollBy(listview.getFirstVisiblePosition(), 10000);
}
}, 500);
} else
{
listview.postDelayed(new Runnable() {
@Override
public void run() {
listview.smoothScrollBy(listview.getFirstVisiblePosition(),10000);
}
},500);
}
}
}
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
}
});
答案 0 :(得分:0)
老问题,但仅供参考,这对我有用:
if (condition) {
// add stuff to begining of ListView and scroll up
arrayList.add(0, stuff);
listView.post(new Runnable() {
@Override
public void run() {
listView.smoothScrollToPosition(0);
}
});
} else {
// add stuff to end of ListView and scroll down
arrayList.add(index, stuff);
listView.post(new Runnable() {
@Override
public void run() {
listView.smoothScrollToPosition(adapter.getCount() - 1);
}
});
}
基本上只是smoothScrollToPosition
而不是smoothScrollBy
。