如何在列表视图中打开特定位置?让我告诉我我的要求和尝试的内容! 它是目录,当我们在第100页时,当单击目录时,通过高亮显示第100页。这是要求。 但是在我的情况下,它一直在滚动,用户需要等到滚动停止在第100页。我使用了setSelection,但是它不起作用。 因此,如果我使用smoothScrollingToPosition,它将继续滚动以停止在特定位置。因此,我的要求是无需直接滚动即可打开特定页面。 以下是逻辑。我在类Adapter的构造函数中应用了listview。
在适配器中,
@Override
public final View getView(final int position, View convertView, final ViewGroup parent) {
int type = getItemViewType(position);
if ((convertView == null) || (((Integer) convertView.getTag()) != type)) {
convertView = createView(position);
convertView.setTag(type);
}
final BaseTableOfContents node = getItem(position);
for(int i=0; i<mNodes.size();i++){
BaseTableOfContents nodes = mNodes.get(i);
if(okayToHighlightAnchorNode(nodes,i)==true){ //only when true, it lands to specific position.
Log.v("nodes","nodes"+okayToHighlightAnchorNode(nodes,i) + nodes.getText() + i);
// returnValue.getListView().setSelection(i); //but this is not working.
returnValue.getListView().smoothScrollToPosition(i,i); //this keeps on scrolling to specific page instead of directly landing to the specific page.
}
}
答案 0 :(得分:0)
要直接滚动,请使用以下代码:
getListView().setSelection(21);// anylistview.setSelection(21);
要平滑滚动,请使用以下
:getListView().smoothScrollToPosition(21);//anylistview.smoothScrollToPosition(21);
答案 1 :(得分:0)
您可以执行此操作-
yourListView.smoothScrollToPositionFromTop(position,offset,duration);
其中position
是您要滚动的位置,duration
是滚动所花费的毫秒数,而offset
是像素的距离