Solution: Nested listview in scrollview makes the listview slide up and down stuck

Source: Internet
Author: User

That's what it was above! Implementation Method: The preceding figure shows an infinite loop between left and right implemented by viewpager with automatic carousel banner. Below the banner diagram is a listview pull-up function. However, during my implementation, I encountered a problem that the display effect of the entire page has been achieved, however, the banner chart and the listview below appear choppy when sliding up or down. Of course, I also encountered a problem before, that is, my banner chart and listview are placed in the scrollview of the parent control together, yes, the listview cannot be fully displayed. solve these two problems.

Solution 1: <The listview cannot be fully displayed in scrollview>

After you call the following method in listview. setadapter (); or adapter. yydatasetchanged ();, the listview that cannot be fully displayed can be correctly displayed in its parent control scrollview. However, it should be noted that for the fill layout of each item in the listview, the root of the layout must be the linearlayout control; otherwise, the listview cannot pass the onmeasure () calculate and display the height of each item correctly, and an exception is thrown! The method to call is as follows: An exception is thrown.

/** * 动态测量listview-Item的高度 * @param listView */public static void setListViewHeightBasedOnChildren(ListView listView) {          ListAdapter listAdapter = listView.getAdapter();           if (listAdapter == null) {              // pre-condition              return;          }            int totalHeight = 0;          for (int i = 0; i < listAdapter.getCount(); i++) {              View listItem = listAdapter.getView(i, null, listView);              listItem.measure(0, 0);              totalHeight += listItem.getMeasuredHeight();          }            ViewGroup.LayoutParams params = listView.getLayoutParams();          params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));          listView.setLayoutParams(params);      }  

Solution 2: <listview slide up and down>

As long as the listview slides up and down, the main reason is that when the listview fills the adapter. The class baseadapter method getview () that is rewritten is called by an infinite loop. This consumes a large amount of memory and causes a lag when the listview slides up and down. The reason is that in the measure process, listview usually loads multiple items based on the screen, and also displays these loaded items at the same time. The parent element of these items is the listview.

According to Google's explanation, view is divided into two phases during draw: Measure and layout. In the measure stage, two parameters are calculated: height and width. Note that this is a recursive process. From top to bottom, decorview calls the measure of its sub-elements in turn. After the two parameters are calculated, layout is started and draw is called. For listview, of course, each item will be called the measure method. In this process, getview and getcount will be called, and there may be many calls depending on your needs. The problem is that in layout, the listview or its parent element's height and width attributes are defined. Based on my experience, it is best to set both height and width to fill_parent to avoid the crazy call of listview to getview. The solution is to avoid self-adaptation as much as possible. Unless it is a last resort, the fixed size or filling effect will be better. Note that if the above dynamic measurement is used to set the display height of the listview, and fill_parent is used to set the attribute of the listview, The getview () in the adapter cannot be stopped () crazy call of methods; or the display of listview content is still incomplete; based on my own practical experience, you can do this: place the listview in linearlayout, <only one listview exists in linearlayout>. I infer that when you load data through dynamic measurement, TA needs an independent space.

In this case, the problem is basically solved. When we put it in the scrollview, The listview slides and gets stuck, and the display is incomplete.


 

Solution: Nested listview in scrollview makes the listview slide up and down stuck

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.