本文整理汇总了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;
}