Java Code Examples for android.widget.ListView#smoothScrollToPositionFromTop()

The following examples show how to use android.widget.ListView#smoothScrollToPositionFromTop() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may want to check out the right sidebar which shows the related API usage.
Example 1
Source Project: COCOQuery   File: AbstractViewQuery.java    License: Apache License 2.0 6 votes vote down vote up
public T smoothScrollTo(final int position) {
    if (view instanceof ListView) {
        final ListView listView = (ListView) view;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            listView.smoothScrollToPositionFromTop(position, 0);
            listView.postDelayed(new Runnable() {
                @Override
                public void run() {
                    // Mock touchEvent to stop listView Scrolling.
                    listView.onTouchEvent(MotionEvent.obtain(System.currentTimeMillis(),
                            System.currentTimeMillis(), MotionEvent.ACTION_DOWN, 0, 0, 0));
                }
            }, 150 - 20);

            listView.postDelayed(new Runnable() {
                @Override
                public void run() {
                    listView.setSelectionFromTop(position, 0);
                }
            }, 150);
        } else {
            listView.setSelectionFromTop(position, 0);
        }
    }
    return self();
}
 
Example 2
/**
 * listview滚动到顶部
 *
 * @param animate 是否动画
 * @return
 */
public UDBaseListOrRecyclerView scrollToTop( int offset,  boolean animate) {
     ListView lv = getListView();
    if (lv != null) {
        if (animate) {
            if (lv.getFirstVisiblePosition() > 7) {//hack fast scroll
                lv.setSelection(7);
            }
            lv.smoothScrollToPositionFromTop(0, offset);
        } else {
            lv.setSelectionFromTop(0, offset);
        }
    }
    return this;
}
 
Example 3
/**
 * listview滚动到某个位置
 *
 * @param section
 * @param rowInSection
 * @param offset
 * @return
 */
public UDBaseListOrRecyclerView scrollToItem( int section,  int rowInSection,  int offset,  boolean animate) {
     ListView lv = getListView();
    if (lv != null) {
        if (animate) {
            lv.smoothScrollToPositionFromTop(getPositionBySectionAndRow(section, rowInSection), offset);
        } else {
            lv.setSelectionFromTop(getPositionBySectionAndRow(section, rowInSection), offset);
        }
    }
    return this;
}
 
Example 4
Source Project: Qshp   File: ListViewUtils.java    License: MIT License 5 votes vote down vote up
/**
 * 滚动列表到position
 *
 * @param listView
 * @param position
 */
public static void smoothScrollListView(ListView listView, int position) {
    if (Build.VERSION.SDK_INT > 7) {
        listView.smoothScrollToPositionFromTop(0, 0);
    } else {
        listView.setSelection(position);
    }
}
 
Example 5
Source Project: Android-Next   File: AutoScrollListView.java    License: Apache License 2.0 5 votes vote down vote up
public static void smoothScrollToPositionCompat(ListView listView,
                                                int position, int offset) {
    if (android.os.Build.VERSION.SDK_INT >= 11) {
        listView.smoothScrollToPositionFromTop(position, offset);
    }
    {
        int firstVisible = listView.getFirstVisiblePosition();
        int lastVisible = listView.getLastVisiblePosition();
        if (position < firstVisible)
            listView.smoothScrollToPosition(position);
        else
            listView.smoothScrollToPosition(position + lastVisible
                    - firstVisible - 2);
    }
}