Example usage for android.widget GridView smoothScrollToPosition

List of usage examples for android.widget GridView smoothScrollToPosition

Introduction

In this page you can find the example usage for android.widget GridView smoothScrollToPosition.

Prototype

@android.view.RemotableViewMethod
public void smoothScrollToPosition(int position) 

Source Link

Document

Smoothly scroll to the specified adapter position.

Usage

From source file:Main.java

/**
 * smoothScroll a specific item to the center of the list :D
 *
 * @param position/* w  w w. j  ava  2 s  .c  o  m*/
 */

public static void smoothScrollToCenterPosition(GridView gridView, int position) {
    int pos = position;
    if (position <= gridView.getFirstVisiblePosition()) {
        pos = position - (gridView.getChildCount() / 2);
        if (pos < 0) {
            pos = 0;
        }
        gridView.smoothScrollToPosition(pos);
    } else {
        pos = position + (gridView.getChildCount() / 2);
        if (pos >= gridView.getCount()) {
            pos = gridView.getCount() - 1;
            if (pos < 0) {
                pos = 0;
            }
        }
    }
    gridView.smoothScrollToPosition(pos);
}