ExpandableListView 中的 smoothScrollToPosition

发布于 2024-12-22 14:49:56 字数 692 浏览 2 评论 0原文

我正在尝试使用 smoothScrollToPosition(intposition) 方法但失败了,因为我不知道如何计算位置属性。

adapter.getGroupCount()

没有

adapter.getChildrenCount(getGroupCount()-1);

public int getLastIndex() {
    int count = 0;
    for (int g = 0; g < getGroupCount(); g++)
        count += getChildrenCount(g) + 1;
    return count;
}

这个伎俩

没有。 TLDR:如何(平滑)滚动到 API 级别 8+ 上的 ExpandableListView 底部?

I'm trying to scroll to the bottom of an ExpandableListView with the smoothScrollToPosition(int position) method but fail because i have no idea how to calculate the position attribute.

Neither

adapter.getGroupCount()

nor

adapter.getChildrenCount(getGroupCount()-1);

nor

public int getLastIndex() {
    int count = 0;
    for (int g = 0; g < getGroupCount(); g++)
        count += getChildrenCount(g) + 1;
    return count;
}

did the trick.

TLDR: How can I (smooth-)scroll to the bottom of a ExpandableListView on API level 8+?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

通知家属抬走 2024-12-29 14:49:56
expandableListView.setOnGroupClickListener(new OnGroupClickListener() {

    @Override
    public boolean onGroupClick(ExpandableListView parent, View v,
            int groupPosition, long id) {
        // TODO Auto-generated method stub
        parent.smoothScrollToPosition(groupPosition);
        return true;
    }
});
expandableListView.setOnGroupClickListener(new OnGroupClickListener() {

    @Override
    public boolean onGroupClick(ExpandableListView parent, View v,
            int groupPosition, long id) {
        // TODO Auto-generated method stub
        parent.smoothScrollToPosition(groupPosition);
        return true;
    }
});
白鸥掠海 2024-12-29 14:49:56
listView.post(new Runnable() {
    @Override
    public void run() {
        listView.smoothScrollToPositionFromTop(listView.getCount()*2, 0, 5000);
    }
});

ExpandableListView.post() 等待列表完成加载项目。
ExpandableListView.getCount() 用于获取项目(组和子项)的计数

提供持续时间,以便列表不会快速滚动(方法需要 API 11)
smoothScrollToPositionFromTop(int 位置、int 偏移量、int 持续时间)

listView.post(new Runnable() {
    @Override
    public void run() {
        listView.smoothScrollToPositionFromTop(listView.getCount()*2, 0, 5000);
    }
});

ExpandableListView.post() to wait for the list to finish loading the items.
ExpandableListView.getCount() to get the count of the items (groups and children)

Provide a duration so the list doesn't scroll to quickly (method requires API 11)
smoothScrollToPositionFromTop(int position, int offset, int duration)

忆伤 2024-12-29 14:49:56

您计算出位置:

public int getLastIndex() {
    int count = 0;
    for (int g = 0; g < getGroupCount(); g++)
        count += getChildrenCount(g) + 1;
    return count;
}

您可以使用它来滚动

expandableListView.smoothScrollToPosition(getLastIndex());

但是,如果您在 Activity 类的 onCreate() 方法中调用 smoothScrollToPosition() 方法,则应该调用 使用 Handler 在屏幕上显示 UI 后的 smoothScrollToPosition() 方法:

  @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    .....
    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            expandableListView.smoothScrollToPosition(getLastIndex());
        }
    },1000);
}

You are calculated the position:

public int getLastIndex() {
    int count = 0;
    for (int g = 0; g < getGroupCount(); g++)
        count += getChildrenCount(g) + 1;
    return count;
}

You can use it to scroll

expandableListView.smoothScrollToPosition(getLastIndex());

But, If you call smoothScrollToPosition() method in onCreate() method of Activity class, you should call smoothScrollToPosition() method after UI is shown on screen by using Handler:

  @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    .....
    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            expandableListView.smoothScrollToPosition(getLastIndex());
        }
    },1000);
}
你的笑 2024-12-29 14:49:56

使用的位置取决于每个组是否扩展。如果该组已展开,则将其所有子项添加到位置计数中。否则,不要!

int getPosition(ExpandableListView listView, ExpandableListViewAdapter adapter) {
    int position = 0;
    for (int group = 0; group < adapter.getGroupCount(); ++group) {
        position += 1;
        if (listView.isGroupExpanded(group)) {
            position += adapter.getChildrenCount(group);
        }
    }
}

使用此位置应该会将您带到列表视图的底部。

The position to use depends on whether each group is expanded or not. If the group is expanded, add all of its children to the position count. Otherwise, don't!

int getPosition(ExpandableListView listView, ExpandableListViewAdapter adapter) {
    int position = 0;
    for (int group = 0; group < adapter.getGroupCount(); ++group) {
        position += 1;
        if (listView.isGroupExpanded(group)) {
            position += adapter.getChildrenCount(group);
        }
    }
}

Using this position should bring you to the bottom of the list view.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文