【问题标题】:How to directly land on specific position without scrolling?如何在不滚动的情况下直接降落在特定位置?
【发布时间】:2019-01-07 10:02:04
【问题描述】:

如何在列表视图中打开特定位置?让我告诉我我的要求和我尝试了什么! 这是目录,当我们在第 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.
            }
        }

【问题讨论】:

    标签: android listview android-adapter baseadapter


    【解决方案1】:

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

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

    要平滑滚动,请使用:

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

    【讨论】:

    • 直接滚动不起作用。那就是问题所在。我上面已经提到了。
    • getListView().smoothScrollToPositionFromTop(position,offset,duration);你也可以用这个
    • 什么是偏移量和持续时间?
    • position -> 要滚动到的位置offset ----> 滚动完成时到视图顶部的所需位置距离(以像素为单位)duration-> 毫秒数用于滚动
    【解决方案2】:

    你可以这样做 -

    yourListView.smoothScrollToPositionFromTop(position,offset,duration);
    

    position 是您要滚动的位置,duration 是滚动所需的毫秒数,offset 是距离(以像素为单位)

    【讨论】:

    • @star 你试过这个吗?
    猜你喜欢
    • 2020-05-06
    • 2018-05-27
    • 1970-01-01
    • 2023-03-15
    • 2011-01-18
    • 1970-01-01
    • 2021-10-04
    • 1970-01-01
    • 2016-06-23
    相关资源
    最近更新 更多