As we all know, the Android ListView and ScrollView have their own sliding effect, if nested with each other will be problematic, the ListView is not full, not sliding, and so on, Google is also recommended not to appear this wonderful design, but there are always some wonderful products, There will be some of this perverted design pattern, of course, it may be the problem of the development of the ape itself, the extension of the program has a problem, can only be helpless to use this design mode, I was also a time ago encountered two ListView at the same time, the data source is different, because it is the company's products, can not be posted code Of course, I began to design a problem, so from the evil can not live ah, good nonsense don't say, we focus on how to solve the problem.
Ways to troubleshoot nested issues
1. Dynamically set the height of the listview
经测试发现,固定listview高度是可以做到的,但是实际中,listview的数据是会发生变化的,所以更合理的应该是动态设置高度。
Public Static void Setlistviewheightbasedonchildren(ListView ListView) {ListAdapter listadapter = Listview.getadapter ();if(ListAdapter = =NULL) {//Pre-condition return; }intTotalheight =0; for(inti =0; I < Listadapter.getcount (); i++) {View ListItem = Listadapter.getview (i,NULL, ListView); Listitem.measure (0,0); Totalheight + = Listitem.getmeasuredheight (); } viewgroup.layoutparamsparams= Listview.getlayoutparams ();params. Height = totalheight + (listview.getdividerheight () * (Listadapter.getcount ()-1)); Listview.setlayoutparams (params); } }
This method is called after setting the adapter, but this method also needs a note that the ListView item must be Linerlayout layout, because only Linerlayout has measure method, if the other layout will throw an exception. Source code can see Linerlayout have measure method, everyone is interested in, you can analyze it yourself.
2. Customize the ListView that adapts perfectly to ScrollView
Public class listviewforscrollview extends ListView { Public Listviewforscrollview(Context context) {Super(context); } Public Listviewforscrollview(context context, AttributeSet attrs) {Super(context, attrs); } Public Listviewforscrollview(context context, AttributeSet attrs,intDefstyle) {Super(Context, attrs, Defstyle); }@Override /** * Rewrite This method to achieve the effect of adapting the ListView to the ScrollView */ protected void onmeasure(intWidthmeasurespec,intHEIGHTMEASURESPEC) {intExpandspec = Measurespec.makemeasurespec (Integer.max_value >>2, measurespec.at_most);Super. Onmeasure (Widthmeasurespec, Expandspec); }}
The code for this method is more concise, just inherit the ListView override Onmeasure method.
3. Use a ListView method to resolve
This solution, is to deal with the data source, if it is to achieve different UI effects, you can also in the GetView method, to judge, highly recommend this approach, may be logical processing aspects, will be somewhat complex, but the scalability and efficiency are very high.
# # Advantages and Disadvantages Summary # #
In fact, I think 1 and 22 ways, very much the same, but the first one there is a limit, in the layout need to use Linerlayout, in the layout of the processing needs more things, the second is more lazy practice, if you are lazy, recommend the second kind, but I really feel that this efficiency is very low, If you encounter more data, more complex, you need to constantly calculate the height of the ListView, resulting in very low efficiency, prone to problems such as ANR. Here I still strongly recommend to use a ListView implementation, because the scalability is better, more efficient than the repetition of the judgment height, much higher.
Today is New Year's Eve, I hope you all have the best of luck, the goat is smug.
How to implement Android exotic ScrollView nested listview