滚动以动态获取新项目时,如何显示列表视图中的最后一个项目? [英] How to show the last item in the List View when scrolled to get new items dynamically?

查看:19
本文介绍了滚动以动态获取新项目时,如何显示列表视图中的最后一个项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序中有一个列表视图,最初显示 10 个项目,当我滚动到底部时,它每次动态添加 10 个项目.我的问题是当我滚动到底部时,显示会转到列表中的第一个项目(不是新添加的 10 个项目),所以我需要一直向下滚动.

I have a list view in my app which shows 10 items initially, and when i scroll to the bottom, it dynamically adds 10 items every time. My problem is when i scroll to the bottom, the display goes to the 1st item in the list(not the newly added 10 items), so i need to scroll all the way down.

我用过

ListView.setSelection(totalItemCount - 9);

显示新添加的 10 个项目中的第 1 个项目.但是这里发生的事情是显示实际上转到了列表视图的第一个项目,但它再次自动滚动到新添加的 10 个项目的第一个项目.所以,用户体验在这里会很糟糕......我希望它改变,这样它就不会出现在列表视图的顶部.希望我解释清楚,请帮忙,谢谢

to display the 1st item of the newly added 10 items. But here what happens is the display goes to the 1st item of the listview actually, but again it automatically scrolls to the 1st item of the newly added 10 items. so, the ux will be bad here....i want it to change so that it whould not go to the top of the listview. hope i explained it clearly, please help, thankyou

推荐答案

当您向下滚动并开始获取新数据时,您应该使用此方法将上次可见位置存储在某些变量中:

When you scroll down and start getting new data you should store your Last Visible Position in some variables using this method :

int firstVisiblePosition = listView.getFirstVisiblePosition();

编辑试试这个:

View view = listView.getChildAt(0);
int distFromTop = (view == null) ? 0 : view.getTop();

然后在您完成加载数据后,您应该使用:

Then after you completed loading data you should set this position using :

listView.setSelectionFromTop(firstVisiblePosition, distFromTop);

这篇关于滚动以动态获取新项目时,如何显示列表视图中的最后一个项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆