How to resize a ListView when new rows are added?

你。 提交于 2019-12-08 07:51:00

问题


So I have this:

...<!--Stuff above-->
<ListView 
     android:id="@+id/reminders_list"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"></ListView>
...<!--More stuff below-->

My problem is when I try to add more rows it does not work because the adapter doesn't add more views if there is no more space. At least in this instance. This means I need to resize it dynamically. Question is how to do this? If something besides resizing is possible I would rather do that. Thanks in advance.


回答1:


Problem is with your android:layout_height="wrap_content"

Setting height as wrap_content will shows only first three rows rest will be ignored. Try to make it as android:layout_height="fill_parent"

As Romain Guy (Google Engineer workd on UI toolkit) Said in his post

By setting the width to wrap_contentyou are telling ListView to be as wide as the widest of its children. ListView must therefore measure its items and to get the items it has to call getView() on the Adapter. This may happen several times depending on the number of layout passes, the behavior of the parent layout, etc.

So if you set the layout width or layout height of your ListView to wrap_content the ListView will try to measure every single view that is attached to it - which is definitely not what you want.

Keep in mind: avoid setting wrap_content for ListViews or GridViews at all times, for more details see this Google I/O video talking about the world of listview

Dynamically Adding rows to List View Link_1

Dynamically Adding rows to List View Link_2

Dynamically Adding rows to List View Link_3

Dynamically Adding rows to List View Link_4




回答2:


When adding elements to the List Adapter, you need to call its notifyDataSetChanged() member function to which notifies any attached observers (such as a ListView), that the data has changed, and will cause it to refresh.



来源:https://stackoverflow.com/questions/11406776/how-to-resize-a-listview-when-new-rows-are-added

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