当前位置: 代码迷 >> 综合 >> listview 利用handler自动滚动到底部在从头开始循环滚动
  详细解决方案

listview 利用handler自动滚动到底部在从头开始循环滚动

热度:94   发布时间:2023-10-19 07:57:20.0

listview基本的获取数据:

在获取数据后:

添加:listview 的滑动监听

realTimeListView.setOnScrollListener(new AbsListView.OnScrollListener() {@Overridepublic void onScrollStateChanged(AbsListView view, int scrollState) {}@Overridepublic void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {if ((firstVisibleItem+visibleItemCount) == totalItemCount){//判断滑动到底部了,但没有下面的的代码,发现一个问题,就是看不到最后一条数据就开始重新调了View lastVisibleItemView = realTimeListView.getChildAt(realTimeListView.getChildCount()-1);if (lastVisibleItemView != null && lastVisibleItemView.getBottom() == realTimeListView.getHeight()){realTimeListView.smoothScrollToPosition(0);//这句话就是调到指定的位置。}}}});
 /*** 向上滚动*/public void listScrollUp() {listScrollOff();handler.postDelayed(run_scroll_up, 0);}/*** 停止滚动*/public void listScrollOff() {handler.removeCallbacks(run_scroll_up);}Handler handler1 = new Handler(){@Overridepublic void handleMessage(Message msg) {handler1.removeCallbacks(run_scroll_up);}};Runnable run_scroll_up = new Runnable() {@Overridepublic void run() {realTimeListView.smoothScrollBy(50,10);//50是滑动的距离handler1.postDelayed(run_scroll_up,1000);}};
调用向上的滚动:

下面是向下滚动

 /*** 向下滚动*/public void listScrollDown() {listScrollOff();handler.postDelayed(run_scroll_down, 0);}
 Runnable run_scroll_down = new Runnable() {@Overridepublic void run() {lvMusics.smoothScrollBy(-1, 10);//-1是滑动的距离handler.postDelayed(run_scroll_down, 10);}};


GitHub地址: 点击打开链接

  相关解决方案