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

后端 未结 11 1553
臣服心动
臣服心动 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 00:49

    I ended up by doing this:

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Log.i("",position+" - "+lastposition);
    
        if (position >= lastposition)
            animation = new TranslateAnimation(Animation.RELATIVE_TO_SELF,
                    0.0f, Animation.RELATIVE_TO_SELF, 0.0f,
                    Animation.RELATIVE_TO_SELF, 1.0f,
                    Animation.RELATIVE_TO_SELF, 0.0f);
        else
            animation = new TranslateAnimation(Animation.RELATIVE_TO_SELF,
                    0.0f, Animation.RELATIVE_TO_SELF, 0.0f,
                    Animation.RELATIVE_TO_SELF, -1.0f,
                    Animation.RELATIVE_TO_SELF, 0.0f);
    
        animation.setDuration(600);
        set.addAnimation(animation);
    
        row.startAnimation(set);
    
        lastposition = position;
    
    }
    
    0 讨论(0)
  • 2020-12-01 00:57

    Try this . I hope it helps you . Logic From @Gal Rom Answer .

    lv.setOnScrollListener(new OnScrollListener() {
            private int mLastFirstVisibleItem;
    
            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {
    
            }
    
            @Override
            public void onScroll(AbsListView view, int firstVisibleItem,
                    int visibleItemCount, int totalItemCount) {
    
                if(mLastFirstVisibleItem<firstVisibleItem)
                {
                    Log.i("SCROLLING DOWN","TRUE");
                }
                if(mLastFirstVisibleItem>firstVisibleItem)
                {
                    Log.i("SCROLLING UP","TRUE");
                }
                mLastFirstVisibleItem=firstVisibleItem;
    
            }
        });
    
    0 讨论(0)
  • 2020-12-01 01:00
    list.setOnScrollListener(new OnScrollListener() {
            int last_item;
            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {
    
            }
    
            @Override
            public void onScroll(AbsListView view, int firstVisibleItem,
                    int visibleItemCount, int totalItemCount) {
                if(last_item<firstVisibleItem+visibleItemCount-1){
                    System.out.println("List is scrolling upwards");
                }
                else if(last_item>firstVisibleItem+visibleItemCount-1){
                    System.out.println("List is scrolling downwards");
                }
                 last_item = firstVisibleItem+visibleItemCount-1;
            }
        });
    

    Based on the position of the last visible item i decide whether Listview is going up or down.

    0 讨论(0)
  • 2020-12-01 01:05

    Here's my approach: It gets you more immediate feedback on how much you've scrolled: OnScroll, you can just get the Top position of the first item in your list. It's a pretty reliable to get actual scroll position information immediately.

    listView.getChildAt(0).getTop()
    
    0 讨论(0)
  • 2020-12-01 01:05

    General solution that doesn't rely on positions of views/etc. Just check the vertical scroll offset and compare it to the previous scroll offset. If the new value is greater than the old the user is scrolling down, and vice-versa.

    // [START check vertical scroll direction]
    int oldScrollOffset = 0;
    
    listView.setOnScrollChangeListener(new View.OnScrollChangeListener() {
            @Override
            public void onScrollChange(View view, int i, int i1, int i2, int i3) {
    
                Boolean scrollDirectionDown;
    
                int newScrollOffset = listView.computeVerticalScrollOffset();
    
                if (newScrollOffset > oldScrollOffset) {
                    scrollDirectionDown = true;
                } else {
                    scrollDirectionDown = false;
                }
    
                oldScrollOffset = newScrollOffset;
    
                if (scrollDirectionDown) {
                    // Update accordingly for scrolling down
                    Log.d(TAG, "scrolling down");
                } else {
                    // Update accordingly for scrolling up
                    Log.d(TAG, "scrolling up");
                }
    
        });
    // [END check vertical scroll direction]
    
    0 讨论(0)
  • 2020-12-01 01:07

    I've used this much simpler solution:

    public class ScrollDetectingListView extends ListView
    

    ...

    setOnScrollListener( new OnScrollListener() 
    {
    
        private int mInitialScroll = 0;
    
        @Override
        public void onScroll(AbsListView view, int firstVisibleItem,
                int visibleItemCount, int totalItemCount) 
        {
            int scrolledOffset = computeVerticalScrollOffset();
    
            boolean scrollUp = scrolledOffset > mInitialScroll;
            mInitialScroll = scrolledOffset;
        }
    
    
        @Override
        public void onScrollStateChanged(AbsListView view, int scrollState) {
    
    
        }
    
    }
    
    0 讨论(0)
提交回复
热议问题