Mar*_*vre 26
我已经找到了一种方法来做这个只是听滚动并通过实现ListView.OnScrollListener滚动结束时更改位置
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
switch (scrollState) {
case OnScrollListener.SCROLL_STATE_IDLE:
if (scrolling){
// get first visible item
View itemView = view.getChildAt(0);
int top = Math.abs(itemView.getTop()); // top is a negative value
int bottom = Math.abs(itemView.getBottom());
if (top >= bottom){
((ListView)view).setSelectionFromTop(view.getFirstVisiblePosition()+1, 0);
} else {
((ListView)view).setSelectionFromTop(view.getFirstVisiblePosition(), 0);
}
}
scrolling = false;
break;
case OnScrollListener.SCROLL_STATE_TOUCH_SCROLL:
case OnScrollListener.SCROLL_STATE_FLING:
Log.i("TEST", "SCROLLING");
scrolling = true;
break;
}
}
Run Code Online (Sandbox Code Playgroud)
变化不是那么顺利但是有效.
小智 5
利用来自@nininho解决方案的几个想法,我得到了我的listview以顺利滚动而不是突然转向该项目.需要注意的是,我只在带有文本的基本ListView中在Moto X上测试了这个解决方案,但它在设备上运行良好.尽管如此,我对此解决方案充满信心,并鼓励您提供反馈.
listview.setOnScrollListener(new OnScrollListener() {
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
// TODO Auto-generated method stub
if (scrollState == SCROLL_STATE_IDLE) {
View itemView = view.getChildAt(0);
int top = Math.abs(itemView.getTop());
int bottom = Math.abs(itemView.getBottom());
int scrollBy = top >= bottom ? bottom : -top;
if (scrollBy == 0) {
return;
}
smoothScrollDeferred(scrollBy, (ListView)view);
}
}
private void smoothScrollDeferred(final int scrollByF,
final ListView viewF) {
final Handler h = new Handler();
h.post(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
viewF.smoothScrollBy(scrollByF, 200);
}
});
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
// TODO Auto-generated method stub
}
});
Run Code Online (Sandbox Code Playgroud)
我推迟平滑滚动的原因是因为在我的测试中,直接调用状态改变回调的smoothScrollBy方法实际上有滚动问题.此外,我没有预见到一个经过充分测试的强大解决方案,并且在下面的解决方案中,我根本没有任何状态.此解决方案尚未在Google Play商店中使用,但应作为一个很好的起点.
| 归档时间: |
|
| 查看次数: |
12299 次 |
| 最近记录: |