Java tutorial
//package com.java2s; /* * Copyright 2014 - 2016 Michael Rapp * * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except * in compliance with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software distributed under the License * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express * or implied. See the License for the specific language governing permissions and limitations under * the License. */ import android.os.Build; import android.support.annotation.NonNull; import android.widget.ExpandableListView; import android.widget.GridView; import android.widget.ListView; public class Main { /** * Scrolls a specific expandable list view to the item at a specific index. * * @param expandableListView * The expandable list view, which should be scrolled, as an instance of the class * {@link ExpandableListView}. The expandable list view may not be null * @param index * The index of the item, the expandable list view should be scrolled to, as an {@link * Integer} value * @param offset * The index of the item, the expandable list view should be scrolled to, as an {@link * Integer} value */ private static void scrollTo(@NonNull final ExpandableListView expandableListView, final int index, final int offset) { expandableListView.setSelectionFromTop(index, offset); } /** * Scrolls a specific list view to the item at a specific index. * * @param listView * The list view, which should be scrolled, as an instance of the class {@link * ListView}. The list view may not be null * @param index * The index of the item, the list view should be scrolled to, as an {@link Integer} * value * @param offset * The offset of the item, the list view should be scrolled to, as an {@link Integer} * value */ private static void scrollTo(@NonNull final ListView listView, final int index, final int offset) { listView.setSelectionFromTop(index, offset); } /** * Scrolls a specific grid view to the item at a specific index. * * @param gridView * 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); } } }