List view snap to item

后端 未结 6 595
不思量自难忘°
不思量自难忘° 2020-12-02 17:45

I\'m creating a list of pictures using a ListView and the photos are of a size that would fit 2 to 3 photos on the screen.

The problem that I\'m having is that I wou

6条回答
  •  无人及你
    2020-12-02 18:26

    Using @nininho 's solution,

    In the onScrollStateChanged when the state changes to SCROLL_STATE_IDLE, remember the position to snap and raise a flag:

    snapTo = view.getFirstVisiblePosition();
    shouldSnap = true;
    

    Then, override the computeScroll() method:

    @Override
    public void computeScroll() {
        super.computeScroll();
        if(shouldSnap){
            this.smoothScrollToPositionFromTop(snapTo, 0);
            shouldSnap = false;
        }
    }
    

提交回复
热议问题