Java Code Examples for android.widget.ListView.setSelectionFromTop()

The following are Jave code examples for showing how to use setSelectionFromTop() of the android.widget.ListView class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: boohee_v5.6   File: ListViewCompatDonut.java   View Source Code Vote up 7 votes
static void scrollListBy(ListView listView, int y) {
    int firstPosition = listView.getFirstVisiblePosition();
    if (firstPosition != -1) {
        View firstView = listView.getChildAt(0);
        if (firstView != null) {
            listView.setSelectionFromTop(firstPosition, firstView.getTop() - y);
        }
    }
}
 
Example 2
Project: Android-DFU-App   File: UARTLogFragment.java   View Source Code Vote up 6 votes
@Override
public void onLoadFinished(final Loader<Cursor> loader, final Cursor data) {
	// Here we have to restore the old saved scroll position, or scroll to the bottom if before adding new events it was scrolled to the bottom.  
	final ListView list = getListView();
	final int position = mLogScrollPosition;
	final boolean scrolledToBottom = position == LOG_SCROLLED_TO_BOTTOM || (list.getCount() > 0 && list.getLastVisiblePosition() == list.getCount() - 1);

	mLogAdapter.swapCursor(data);

	if (position > LOG_SCROLL_NULL) {
		list.setSelectionFromTop(position, 0);
	} else {
		if (scrolledToBottom)
			list.setSelection(list.getCount() - 1);
	}
	mLogScrollPosition = LOG_SCROLL_NULL;
}
 
Example 3
Project: GitHub   File: RefreshContentWrapper.java   View Source Code Vote up 5 votes
@Override
    public AnimatorUpdateListener onLoadingFinish(final RefreshKernel kernel, final int footerHeight, int startDelay, final int duration) {
        if (mScrollableView != null && kernel.getRefreshLayout().isEnableScrollContentWhenLoaded()) {
            if (!canScrollDown(mScrollableView)) {
                return null;
            }
            if (mScrollableView instanceof AbsListView && !(mScrollableView instanceof ListView) && Build.VERSION.SDK_INT < 19) {
                if (startDelay > 0) {
                    kernel.getRefreshLayout().getLayout().postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            ((AbsListView) mScrollableView).smoothScrollBy(footerHeight, duration);
                        }
                    }, startDelay);
                } else {
                    ((AbsListView) mScrollableView).smoothScrollBy(footerHeight, duration);
                }
                return null;
            }
            return new AnimatorUpdateListener() {
                int lastValue = kernel.getSpinner();
                @Override
                public void onAnimationUpdate(ValueAnimator animation) {
                    int value = (int) animation.getAnimatedValue();
                    try {
                        if (mScrollableView instanceof ListView) {
                            if (Build.VERSION.SDK_INT >= 19) {
                                ((ListView) RefreshContentWrapper.this.mScrollableView).scrollListBy(value - lastValue);
                            } else {
//                                ListViewCompat.scrollListBy((ListView) mScrollableView, value - lastValue);
                                ListView listView = (ListView) RefreshContentWrapper.this.mScrollableView;
                                final int firstPosition = listView.getFirstVisiblePosition();
                                if (firstPosition == ListView.INVALID_POSITION) {
                                    return;
                                }
                                final View firstView = listView.getChildAt(0);
                                if (firstView == null) {
                                    return;
                                }
                                final int newTop = firstView.getTop() - (value - lastValue);
                                listView.setSelectionFromTop(firstPosition, newTop);
                            }
                        } else {
                            mScrollableView.scrollBy(0, value - lastValue);
                        }
                    } catch (Throwable ignored) {
                        //根据用户反馈,此处可能会有BUG
                    }
                    lastValue = value;
                }
            };
        }
        return null;
    }
 
Example 4
Project: Rxjava2.0Demo   File: RefreshContentWrapper.java   View Source Code Vote up 5 votes
@Override
    public AnimatorUpdateListener onLoadingFinish(final RefreshKernel kernel, final int footerHeight, int startDelay, final int duration) {
        if (mScrollableView != null && kernel.getRefreshLayout().isEnableScrollContentWhenLoaded()) {
            if (!canScrollDown(mScrollableView)) {
                return null;
            }
            if (mScrollableView instanceof AbsListView && !(mScrollableView instanceof ListView) && Build.VERSION.SDK_INT < 19) {
                if (startDelay > 0) {
                    kernel.getRefreshLayout().getLayout().postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            ((AbsListView) mScrollableView).smoothScrollBy(footerHeight, duration);
                        }
                    }, startDelay);
                } else {
                    ((AbsListView) mScrollableView).smoothScrollBy(footerHeight, duration);
                }
                return null;
            }
            return new AnimatorUpdateListener() {
                int lastValue = kernel.getSpinner();
                @Override
                public void onAnimationUpdate(ValueAnimator animation) {
                    int value = (int) animation.getAnimatedValue();
                    try {
                        if (mScrollableView instanceof ListView) {
                            if (Build.VERSION.SDK_INT >= 19) {
                                ((ListView) RefreshContentWrapper.this.mScrollableView).scrollListBy(value - lastValue);
                            } else {
//                                ListViewCompat.scrollListBy((ListView) mScrollableView, value - lastValue);
                                ListView listView = (ListView) RefreshContentWrapper.this.mScrollableView;
                                final int firstPosition = listView.getFirstVisiblePosition();
                                if (firstPosition == ListView.INVALID_POSITION) {
                                    return;
                                }
                                final View firstView = listView.getChildAt(0);
                                if (firstView == null) {
                                    return;
                                }
                                final int newTop = firstView.getTop() - (value - lastValue);
                                listView.setSelectionFromTop(firstPosition, newTop);
                            }
                        } else {
                            mScrollableView.scrollBy(0, value - lastValue);
                        }
                    } catch (Throwable ignored) {
                        //根据用户反馈,此处可能会有BUG
                    }
                    lastValue = value;
                }
            };
        }
        return null;
    }
 
Example 5
Project: LuaViewPlayground   File: UDBaseListView.java   View Source Code Vote up 5 votes
/**
 * listview滚动到顶部
 *
 * @param animate 是否动画
 * @return
 */
public UDBaseListOrRecyclerView scrollToTop(final int offset, final boolean animate) {
    final 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 6
Project: LuaViewPlayground   File: UDBaseListView.java   View Source Code Vote up 5 votes
/**
 * listview滚动到某个位置
 *
 * @param section
 * @param rowInSection
 * @param offset
 * @return
 */
public UDBaseListOrRecyclerView scrollToItem(final int section, final int rowInSection, final int offset, final boolean animate) {
    final ListView lv = getListView();
    if (lv != null) {
        if (animate) {
            lv.smoothScrollToPositionFromTop(getPositionBySectionAndRow(section, rowInSection), offset);
        } else {
            lv.setSelectionFromTop(getPositionBySectionAndRow(section, rowInSection), offset);
        }
    }
    return this;
}