How to get all children (visible and invisible) from a ListView?

女生的网名这么多〃 提交于 2020-01-01 09:55:58

问题


My problem is similar to ListView getChildAt returning null for visible children, but despite searching I cannot find a solution.

I have a ListView with a Scroll. The ListView has 10 items, 7 of which are visible and 3 are hidden by scroll. I also have an external method (out of adapter) that must get all of the children from this ListView (e.g. using getChildAt()).

I need all 10 of the items, but the last 3 are null objects. I've tried code like the following:

getListView().smoothScrollToPosition();

But this doesn't work.

I think that I don't need to post the rest of my code, as the description says everything?


回答1:


As you have already seen you can't get all the child row views from a ListView simply because a ListView holds only the views for the visible rows(plus some recycled rows but you can't reach those). The correct way to do what you want is to store whatever data in the adapter's data and retrieve it from there.

But the ListView doesn't keep the current values from RadioGroup in running time.

I've seen that you have some problems with this so I've adapted some old code to build a basic example, code that you can find here.




回答2:


I don't think so you need to add scroll view for a listView. Scroll automatically works on ListView. Try your application without adding scroll view and I'm sure it'll work as you needed.




回答3:


The non-visible children of a listView don't actually exist. When they become visible, one of the redundant views is recycled or a new view is generated. So you can't actually access all the views. Why do you want to? Whatever changes you want to make should be made to the data that populates the views rather than the views themselves.




回答4:


The reason those children are null it's because they really do not exist and they will never exist, if only 7 children are on the screen at one time, the system will only create 7 and re-use by passing the convertView back to the adapter getView() method.

If you want to grab information regarding your whole dataset you should search on the dataset itself, instead of the views on the screen. E.g. if it's an ArrayAdapter, loop the array; if it's a CursorAdapter, loop the cursor; etc.




回答5:


There are a few point that you need to take care of: 1. List view provides inbuilt scroll functionality, So don't use Scroll view. It will only mess up things. 2. List view doesn't contain ALL the children. When you scroll it, it creates only visible items on run time. 3. If you want to get all the children altogether, Better keep an ArrayList of the child objects that your list has. You can add or remove children to this ArrayList as per requirement.



来源:https://stackoverflow.com/questions/13950776/how-to-get-all-children-visible-and-invisible-from-a-listview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!