Problem with smoothScrollToPositionFromTop android

1

I currently have a grid nxn which moves through some buttons which I press can be from 1 to 10 (it's an example) every 1 represents a position in the grid when he changes from 1 to 2 or to 3 he will have to smoothScrollToPositionFromTop medium go in 3 milliseconds and in animation is why I do not use another function I require the duration it has.

The problem with the smoothScrollToPositionFromTop method is that when going to the position of the new element on some occasions or it is short or it is passed of the screen and the user has to look for the point in the grid if someone can help me serious help.

gridview.smoothScrollToPositionFromTop(position ,0,3);

The variable position is the one assigned to the button.

    
asked by diego alejandro franco osorio 03.08.2016 в 00:32
source

1 answer

0

There is a known problem with the smoothScrollToPositionFromTop method and apparently also with smothScrollToPosition where the positioning fails in the cases that one has n elements in the view and tries to slide to the n + 2 element.

A simple way to remedy the problem is how setSelection(int position) does not show this behavior, call setSelection from Handler at the end of the slip.

// crear un Handler en la lista
Handler mHandler;

// en onCreate:
mHandler = new Handler();

// luego un override para smoothScrollToPositionFromTop
@Override
public void smoothScrollToPositionFromTop(int position, int offset, int duration){

    final int pos = position;

    super.smoothScrollToPositionFromTop(position, offset, duration);

    // Aquí se puede experimentar con duration - n para compensar el tiempo de construir y lanzar el Runnable
    mHandler.postDelayed(new Runnable(){
        @Override
        public void run(){ setSelection(pos); }
    , duration );
}

There are more a little more complex possibilities to remedy this error, for example by ending the slip in OnScrollListener using onScroll (after finishing the slip) or onScrollStateChanged , which is always called before the view is update (that may happen several times during a slide).

    
answered by 16.02.2017 в 16:15