Detecting the scrolling direction in the adapter (up/down)

后端 未结 11 1604
臣服心动
臣服心动 2020-12-01 00:38

I am trying to mimic the Google Plus application in my project, as it seems to be the reference now.

The listview effect when scrolling is really nice and I would li

11条回答
  •  佛祖请我去吃肉
    2020-12-01 01:14

    More complex solution (working with long items height in listview)

    1. Create custom listview

      public class ScrollDetectingListView extends ListView {
          public ScrollDetectingListView(Context context) {
              super(context);
          }
      
          public ScrollDetectingListView(Context context, AttributeSet attrs) {
              super(context,attrs);
          } 
      
          public ScrollDetectingListView(Context context, AttributeSet attrs, int defStyle) {
              super(context, attrs, defStyle);
          }
      
          //we need this protected method for scroll detection
          public int getVerticalScrollOffset() {
              return computeVerticalScrollOffset();
          }
      }
      
    2. Override onScroll

          listView.setOnScrollListener(new AbsListView.OnScrollListener() {
      
          private int mInitialScroll = 0;
      
          @Override
          public void onScrollStateChanged(AbsListView view, int scrollState) {
      
          }
      
          @Override
          public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
              int scrolledOffset = listView.getVerticalScrollOffset();
              if (scrolledOffset!=mInitialScroll) {
                  //if scroll position changed
                  boolean scrollUp = (scrolledOffset - mInitialScroll) < 0;
                  mInitialScroll = scrolledOffset;
              }
          }
          });
      

提交回复
热议问题