ExpandableListView 中的 smoothScrollToPosition
我正在尝试使用 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
ExpandableListView.post()
等待列表完成加载项目。ExpandableListView.getCount()
用于获取项目(组和子项)的计数提供持续时间,以便列表不会快速滚动(方法需要 API 11)
smoothScrollToPositionFromTop(int 位置、int 偏移量、int 持续时间)
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)
您计算出位置:
您可以使用它来滚动
但是,如果您在 Activity 类的
onCreate()
方法中调用smoothScrollToPosition()
方法,则应该调用使用 Handler 在屏幕上显示 UI 后的 smoothScrollToPosition()
方法:You are calculated the position:
You can use it to scroll
But, If you call
smoothScrollToPosition()
method inonCreate()
method of Activity class, you should callsmoothScrollToPosition()
method after UI is shown on screen by using Handler:使用的位置取决于每个组是否扩展。如果该组已展开,则将其所有子项添加到位置计数中。否则,不要!
使用此位置应该会将您带到列表视图的底部。
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!
Using this position should bring you to the bottom of the list view.