首页 > 解决方案 > 如何在不滚动的情况下直接降落在特定位置?

问题描述

如何在列表视图中打开特定位置?让我告诉我我的要求和我尝试了什么!这是目录,当我们在第 100 页时,当我们单击目录时,它通过突出显示显示第 100 页。这是要求。但在我的情况下,它一直在滚动,用户需要等到滚动在第 100 页停止。我使用了 setSelection 但它不起作用。因此,如果我使用 smoothScrollingToPosition ,它会继续滚动以停止在特定位置。所以我的要求是不直接滚动以打开特定页面。下面是逻辑。我在类适配器的构造函数中应用了列表视图。

在适配器中,

@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.
            }
        }

标签: androidlistviewandroid-adapterbaseadapter

解决方案


对于直接滚动,请使用以下代码:

getListView().setSelection(21);// anylistview.setSelection(21);

为了平滑滚动,请使用:

getListView().smoothScrollToPosition(21);//anylistview.smoothScrollToPosition(21);

推荐阅读