如何显示在列表视图中的最后一个项目,当滚动动态获取新项目?视图、新项目、项目、动态

2023-09-07 02:15:52 作者:?二才叫范、

我在我的应用程序的列表视图,显示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个项目的第一个项目。但在这里发生的事情是,显示屏将到ListView真正的第一个项目,但同样它会自动滚动到新添加的10个项目的第一个项目。因此,对UX会坏在这里....我希望它改变,因此,它不对子级去列表视图的顶部。希望我解释清楚,请大家帮帮忙,三江源

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);