Example usage for android.widget GridView setSelection

List of usage examples for android.widget GridView setSelection

Introduction

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

Prototype

@Override
public void setSelection(int position) 

Source Link

Document

Sets the currently selected item

Usage

From source file:Main.java

/**
 * Scrolls a specific grid view to the item at a specific index.
 *
 * @param gridView/* w  w w.jav a  2  s  . c om*/
 *         The grid view, which should be scrolled, as an instance of the class {@link
 *         GridView}. The grid view may not be null
 * @param index
 *         The index of the item, the grid view should be scrolled to, as an {@link Integer}
 *         value
 * @param offset
 *         The offset of the item, the grid view should be scrolled to, as an {@link Integer}
 *         value
 */
private static void scrollTo(@NonNull final GridView gridView, final int index, final int offset) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        gridView.setSelectionFromTop(index, offset);
    } else {
        gridView.setSelection(index);
    }
}

From source file:org.xbmc.android.remotesandbox.ui.phone.DashboardFragment.java

private void setupDashboardItems(GridView menuGrid) {

    final ArrayList<HomeItem> homeItems = new ArrayList<HomeItem>();
    final SharedPreferences prefs = PreferenceManager
            .getDefaultSharedPreferences(getActivity().getApplicationContext());
    if (prefs.getBoolean("setting_show_home_music", true))
        homeItems.add(new HomeItem(HOME_ACTION_MUSIC, R.drawable.home_ic_music, "Music", "Listen to"));

    menuGrid.setAdapter(new HomeAdapter(getActivity(), homeItems));
    menuGrid.setOnItemClickListener(getHomeMenuOnClickListener());
    menuGrid.setSelected(true);//w ww.ja  va2s  .  co  m
    menuGrid.setSelection(0);
}

From source file:com.github.jvanhie.discogsscrobbler.ReleaseListFragment.java

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    //call listener to determine correct width when available or changed
    getView().getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override//from  ww w  .  j  a  v  a2s.co m
        public void onGlobalLayout() {
            int fragmentWidth = getView().getWidth();
            if (fragmentWidth > 0) {
                if (mGrid) {
                    GridView grid = (GridView) mList;
                    int col = grid.getNumColumns();
                    int newCol = Math.round((fragmentWidth / logicalDensity) / (float) 150);
                    if (col != newCol) {
                        int pos = grid.getFirstVisiblePosition();
                        //if an item is selected, focus on this instead
                        if (mActivatedPosition != ListView.INVALID_POSITION)
                            pos = mActivatedPosition;
                        grid.setNumColumns(newCol);
                        grid.setSelection(pos);
                    }
                }
            }
        }
    });
}