本文整理匯總了Java中android.widget.AbsListView.setSelection方法的典型用法代碼示例。如果您正苦於以下問題:Java AbsListView.setSelection方法的具體用法?Java AbsListView.setSelection怎麽用?Java AbsListView.setSelection使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類android.widget.AbsListView
的用法示例。
在下文中一共展示了AbsListView.setSelection方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: scrollToTop
import android.widget.AbsListView; //導入方法依賴的package包/類
public void scrollToTop() {
if (child instanceof AbsListView) {
AbsListView absListView = (AbsListView) child;
absListView.setSelection(0);
} else if (child instanceof RecyclerView) {
RecyclerView recyclerView = (RecyclerView) child;
RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
layoutManager.scrollToPosition(0);
}
}
示例2: restoreBestPositionWithScroll
import android.widget.AbsListView; //導入方法依賴的package包/類
/**
* Restore the best position possible in the list.
*
* @return the new last position
*/
public static int restoreBestPositionWithScroll(final AbsListView listView, int selectedPosition,
int firstVisiblePosition, final int scroll) {
if(listView.getFirstVisiblePosition() <= selectedPosition && listView.getLastVisiblePosition()>=selectedPosition)
return selectedPosition;
final int newPositionToSelect;
newPositionToSelect = selectedPosition>=0?selectedPosition:0;
if (newPositionToSelect < listView.getCount()) {
//we select next enabled view
for(int i = newPositionToSelect; i<listView.getCount(); i++){
if(i<listView.getAdapter().getCount()&&listView.getAdapter().isEnabled(i)){
//we don't want to loose scroll
final int pos = i;
if(listView instanceof GridView){
listView.setSelection(pos);
listView.smoothScrollToPositionFromTop(pos, scroll);
}
else if(listView instanceof ListView)
((ListView)listView).setSelectionFromTop(pos, scroll);
break;
}
}
// Make sure the album list/grid has still the focus after changing the
// view mode
listView.postDelayed(new Runnable() {
@Override
public void run() {
listView.requestFocus();
}
}, 200);
}
return newPositionToSelect;
}