Android-list view summary of various wonderful problems

Source: Internet
Author: User
Tags call back repetition

For list view, everyone must love and hate, based on the list view layout, as long as creative can write a variety of good performance, the interface cool animation of the list view. Therefore, some problems will be encountered. So here's a summary of the problem.

First, ScrollView embedded list view, this layout is very strange, we usually develop when the list view more than a screen when the list view automatically will scroll, because he brought the scroll bar, do not believe that can go to view the source code. Oh, may be the habit of thinking it, ScrollView into a ListView, when the list view will display a row. Why is this? The ListView space is nested in the ScrollView, and the size of the ListView cannot be computed correctly, so the size of the list can be calculated from the code, based on the current ListView list item. Of course, there are various solutions available online. I think the two methods are better.
(1) Rewrite the list view ononmeasure (), the effect is also possible, but sometimes in my computer test failed.

 Public  class mainlistview extends ListView{      Public Mainlistview(Context context) {//TODO auto-generated method stub            Super(context); } Public Mainlistview(context context, AttributeSet attrs) {//TODO auto-generated method stub            Super(context, attrs); } Public Mainlistview(context context, AttributeSet attrs,intDefstyle) {//TODO auto-generated method stub            Super(Context, attrs, Defstyle); }@Override          protected void onmeasure(intWidthmeasurespec,intHEIGHTMEASURESPEC) {//TODO auto-generated method stub            intExpandspec = Measurespec.makemeasurespec (Integer.max_value >>2, measurespec.at_most);Super. Onmeasure (Widthmeasurespec, Expandspec); }      }

(2) Recalculate the height of item of List view, and add up is the total width of the list view.

 Public Static void Setlistviewheightbasedonchildren(ListView ListView) {//Get ListView corresponding AdapterListAdapter listadapter = Listview.getadapter ();if(ListAdapter = =NULL) {return; }intTotalheight =0; for(inti =0, Len = Listadapter.getcount (); i < Len; i++) {//Listadapter.getcount () returns the number of data itemsView ListItem = Listadapter.getview (i,NULL, ListView); Listitem.measure (0,0);//Calculate the width height of the child viewTotalheight + = Listitem.getmeasuredheight ();//Statistics total height of all children} viewgroup.layoutparamsparams= Listview.getlayoutparams ();params. Height = totalheight + (listview.getdividerheight () * (Listadapter.getcount ()-1));//Listview.getdividerheight () Gets the height of the delimiter between children        //Params.height finally get the entire ListView full display required heightListview.setlayoutparams (params);

These two methods are actually re-measuring the width and height of the list view.

Second, Android ListView checkbox status disorder

Two solutions given by many people
1: Come up and say that because the Convertview object is common, you cannot use the Convetview, but each time you GetView (), the new object's view comes out. This is probably the way to get out of the butt.
2: That is, then I'll make a collection of my own. Save checkbox State, then confusion, kill you. Now that there is a list in the adapter to save the checkbox state, why do you want to save the state of the checkbox again? Isn't it superfluous? Why does the
cause this behavior?
1: First analyze the next ViewHolder.checkBox.setOnCheckedChangeListener (New Oncheckedchangelistener () ...);
The phrase is to add a listener to the checkbox, and if the checkbox state changes, the system will automatically call back the Oncheckedchange () method inside.
and the Oncheckedchange () method in this article is the code that records the checkbox state after this change.
2: Then analyze the next if (List.get (position). Type = = a.type_checked) {...}, this part of the code is based on the object property in the list collection to initialize the View checkbox should be the selection state.
3: When I swipe up and down the ListView, the checkbox is in a state of confusion, and according to the 2nd analysis, the status of the checkbox is not going to go awry, unless the properties of the object in the list collection have been changed, where did it change?
4: There is only one place in the text that changes the properties of the CheckBox object in the list collection, which is the Oncheckedchangelistener () method mentioned in the 1th. It was carried out?
What's going on, it's impossible, hit a breakpoint, run and then you know when you swipe up and down the ListView does stop.
5: This is the function of Convertview, because no matter how many data are displayed in the ListView, only a few objects are shared, and our code re-assigns the resulting object every time.
Ultimate Solution: Simply add the listener method to the code that initializes the checkbox state in view.

Three: Android listview Asynchronous loading Picture dislocation, repetition, flicker analysis
For example, there are 100 item on the ListView, one screen only shows 10 item, we know that GetView () Convertview is used to reuse the view object, because one item corresponds to a View object, The ImageView control is the view object obtained through Findviewbyid (), and when we reuse the View object, the ImageView object is also reused. For example, the 11th Item view re-uses the 1th Item view object, then the ImageView is reused at the same time, so when the picture is not downloaded, this imageview (11th item) shows the data is the reuse (1th item) data.

1:item Picture Display Repeat
This display repetition means that the current line item shows a picture of the previous line item.
For example, the ListView slides to the 2nd row to load a picture asynchronously, but the load is slow, the ListView has slid to line 14th during the loading process, and the picture is loaded at the end of the slide. Line 2nd is no longer in the screen, according to the caching principle described above, the 2nd row of the View object may be reused in line 14th, so we see that the 14th line shows the image that should belong to the 2nd line, causing the display to repeat.
2. Item image shows confusion
This display disorder refers to a line item that shows a picture that does not belong to the line item.
The same reason as above.
3. Item image Display flashes
The other situation described above, if the 14th line of pictures and quickly loaded the end, so we see the 14th line first shows the re-use of the 2nd line of the picture, and immediately display their own images to cover the flicker confusion.

Solution:
Through the above analysis we know that the cause of the disorder is the asynchronous loading and the object is reused, if each time GetView can give an identity to the object, when the asynchronous loading is done to compare the identity with the current row of the identity of the item is consistent, the same is displayed, otherwise do not do processing.
Principle: First give ImageView set a tag, this tag is set in the URL of the image, and then in the loading time to get this URL and to load that position in the URL comparison, if not the same load, the same is the same as the former will not load.

The above part draws on others ' blog, oneself met and then summarizes. Give yourself a refresher.
  
  

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Android-list view summary of various wonderful problems

Related Article

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.