android ListView can't stop at exact pixel position

不想你离开。 提交于 2019-12-24 10:57:52

问题


What i'm trying to achieve is to prevent showing some listView beyond a certain precise position. Let's say i want to stop when reaching pixel 150 relatively to the top of the list (all rows have the same height, 240px at the moment), what i'm doing now is :

public abstract class EndlessScrollListener implements AbsListView.OnScrollListener {
 @Override
 public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount){

   int firstVisibleItemTop = -1;

   View v = view.getChildAt(0);

   if(v != null) {
     firstVisibleItemTop = v.getTop();
   }

   if(firstVisibleItemTop >= 150){
      //to prevent the list to scroll  
      view.smoothScrollBy(0,0);
   }
 }

 @Override
 public void onScrollStateChanged(AbsListView view, int scrollState) {
  //same logic here
 }

}

(I also have implemented the same logic having custom OnTouchListener on the listView for the case when the finger is still on the screen aka case MotionEvent.ACTION_MOVE)

The problem is that my OnScrollListener do not get called back often enough so the position is exceeded and I end up showing part of the listView that shouldnt be visible. The fastest the scroll is, the more the position will be exceeded. I've tried also to increase the friction of the list which helps reduce the gap but doesn't solve the issue.

I guess the listView Object was not meant for that use (getting called back for each pixel doesnt look reasonable) but i can't find any other way to achieve this unless maybe i customize ListView class but that look painfull to achieve.

Maybe can you recommand a solution keeping ListView or pointing me to an alternative to ListView that might do the trick ?

Thank you.

来源:https://stackoverflow.com/questions/45289193/android-listview-cant-stop-at-exact-pixel-position

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!