Detailed explanation of listview instances nested in Android adapter

Detailed explanation of listview instances nested in Android adapter

preface:

Can be nested ~ but the display needs special treatment ~ the following is the treatment method

A few days ago, due to the needs of the project, another listview was placed in one listview, that is, another listview was placed in each listitem of a listview. But at the beginning, you will find that the small listview you put in will not be displayed completely, and its height is always a problem. After checking the Internet, I found that others have encountered such problems, and most people do not recommend such a design, because Android prohibits putting another Scrollview in Scrollview by default, and its height cannot be calculated.

After searching again, it is found that the cow on stackoverflow has solved this problem. Through experiments, it is found that the problem can be solved. Its idea is to recalculate the height of the listview according to the sub items of the listview after setting the listview adapter, and then set the height to the listview as layoutparams, so that its height is correct. The following is the source code:

If you call this static method after setting up ListView's Adapter, you can make ListView display correctly in its parent ListView's ListItem. However, it should be noted that each item of the child listview must be a LinearLayout, not others. Because other layouts (such as relativelayout) do not override onmeasure(), an exception will be thrown during onmeasure().

Another problem with nesting listviews (or scrollviews) in a Scrollview is that the child Scrollview cannot slide (if it is not fully displayed), because the sliding event will be eaten by the parent Scrollview. If you want the child Scrollview to slide, you can only forcibly intercept the sliding event. Some people have sent code in the forum and said yes. Although I haven't tried it myself, it is estimated to be feasible.

Although the technical difficulties of displaying Scrollview in Scrollview can be overcome, such a design is a very poor user experience, because it is not easy for users to see and operate the content in the sub Scrollview. For example, a good design is that each item of the parent listview only displays a general description, and then clicking its item will enter another page to describe and display in detail and the operation of this item.

The above is a detailed explanation of the examples of Android adapter nested listview. There are many articles on Android Development on this site. I hope you can search and consult. Thank you for reading. I hope it can help you. Thank you for your support to this site!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>