One: Baseadapter Introduction
Baseadapter is a list of Listview,gridview, which uses the data adapter, its main purpose is to send a set of data to the ListView, Spinner, Gallery and the GridView UI display components, If the ListView list has too many data items, such as 1000 items, if we put all of the 1000 items in the interface, the software directly memory overflow, Baseadapter just can help us solve this problem, baseadapter working principle diagram is as follows:
It can be seen that if we have 1000 data items that actually show only 7 items, and the other caches are in recycler, the recycler is similar to a message pair column that holds data items that are not displayed on the UI, if the top (or bottom) is scrolled out of the ListView interface, Recycler adds the data item to the message pair column, as in, the first item has slipped out of the interface, and when the first item is re-slid, Android will determine if the first item is loaded, and if so, reset the data source for the first item and then display it. The GetView () method is called when the data item is loaded for the first time, either up or down, but many times the GetView () method system is called multiple times, and the interface is refreshed multiple times by the call, and performance degrades, such as the interface will stutter.
/** @params position need to display the data subscript, starting from 0 @params view display @viewgroup View Group * /public view GetView (int
II: Solutions
1: Methods commonly used on the internet
< ListView Android:id = "@+id/lv_messages" android:layout_width= "Fill_parent" android:layout_height= "Fill_parent " ></ListView>
Online said the execution of the reason is because each view, it is to measure the height of the view, the execution of the measure method, causing GetView to execute multiple times, but the ListView nested in ScrollView, Baseadapter GetView () method is called multiple times.
2: Rewrite the onmeasure and OnLayout methods of the ListView.
I rewrite the onmeasure and OnLayout methods of the ListView, define whether the first load of the variable, Boolean isonmeasure=false, and then in the Bastadapter GetView method, determine whether the interface is loaded for the first time, This method is used to deal with this problem.
@Overrideprotected voidOnmeasure (intWidthmeasurespec,intHeightmeasurespec) {Isonmeasure=true; intExpandspec = Measurespec.makemeasurespec (integer.max_value >> 2, Measurespec.at_most); Super. Onmeasure (Widthmeasurespec, Expandspec); } @Overrideprotected voidOnLayout (BooleanChangedintLintTintRintb) {isonmeasure=false; Super. OnLayout (Changed, L, T, R, b); }
Three: Complete code
1: Rewrite listview
Public classScrolllistviewextendsListView {Private Booleanisonmeasure; Public Booleanismeasure () {returnisonmeasure; } PublicScrolllistview (Context context) {Super(context); } PublicScrolllistview (Context context, AttributeSet attrs) {Super(context, attrs); } PublicScrolllistview (context context, AttributeSet attrs,intDefstyle) { Super(context, attrs, Defstyle); } @Overrideprotected voidOnmeasure (intWidthmeasurespec,intHeightmeasurespec) {Isonmeasure=true; intExpandspec = Measurespec.makemeasurespec (integer.max_value >> 2, Measurespec.at_most); Super. Onmeasure (Widthmeasurespec, expandspec);//super.onmeasure (Widthmeasurespec, heightmeasurespec);} @Overrideprotected voidOnLayout (BooleanChangedintLintTintRintb) {isonmeasure=false; Super. OnLayout (Changed, L, T, R, b); }}
XML for 2:activity
<view. ScrolllistviewAndroid:id= "@+id/ordergoodslist"style= "@style/list_normal"Android:layout_width= "Fill_parent"Android:layout_height= "Fill_parent"Android:layout_below= "@id/line3"Android:background= "@color/gray_f2"Android:divider= "@color/gray_f2"Android:dividerheight= "1dip" ></view. Scrolllistview>
3: Adapter Code
protected viewgroup viewgroup; @Override Public void int position) { if (! ((Basegridview) viewgroup). Ismeasure ()) { // first load, processing interface UI }Else { // is not the first time to load, do not handle anything } }
If you have a better method, please also leave to share your method, thank you!
Android shares a way to handle baseadapter,getview () multiple loads