Android shares a BaseAdapter, getView () multiple loading method, and baseadaptergetview

Source: Internet
Author: User

Android shares a BaseAdapter, getView () multiple loading method, and baseadaptergetview

I. Introduction to BaseAdapter

BaseAdapter is a list of listview, gridview, and other data adapters. It is mainly used to transmit a set of data to UI display components such as ListView, Spinner, Gallery, and GridView, if there are too many data items in the listView list, such as 1000 items, if we put all these 1000 Items in the interface, the software directly overflows the memory. BaseAdapter can help us solve this problem just now, the working principle of BaseAdapter is as follows:

We can see that if we have 1000 data items, only 7 items are actually displayed, and other items are cached in Recycler. Recycler is similar to a message pair column and is used to save data items not displayed on the ui, if the top (or bottom) slides out from the listView interface through scrolling, the Recycler adds the data item to the Message pair column. For example, the first item has slipped out from the interface, when the first item slides in again, android will determine whether the first item has been loaded. If so, re-set the data source of the first item and display it. When a data item is loaded for the first time, slide up, or slide down, the getView () method is called. However, in many cases, the getView () method is called multiple times, multiple calls will refresh the interface multiple times, and the performance will be reduced, for example, the interface will be stuck.

/** @ Params position: the subscript of the data to be displayed. Starting from 0, @ params view displays the View @ ViewGroup View group */public view getView (int position, view View, viewGroup viewGroup)

Ii. Solution

1: Common online methods

<ListView        android:id="@+id/lv_messages"        android:layout_width="fill_parent"        android:layout_height="fill_parent"  ></ListView>

The reason why a VIew is executed multiple times on the internet is that, when a view is displayed, the VIew height is measured and the measure method is executed. As a result, the getView is executed multiple times, but the listView is nested in the ScrollView, the getView () method of BaseAdapter is called multiple times.

2: override the onMeasure and onLayout methods of ListView.

I override the onMeasure and onLayout methods of ListView, define a variable boolean isOnMeasure = false, and then determine whether to load the interface for the first time in the getView method of BastAdapter, this method is used to solve this problem.

  @Override    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {        isOnMeasure = true;        int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,                MeasureSpec.AT_MOST);        super.onMeasure(widthMeasureSpec, expandSpec);    }    @Override    protected void onLayout(boolean changed, int l, int t, int r, int b) {        isOnMeasure = false;        super.onLayout(changed, l, t, r, b);    }

Iii. complete code

1: override listView

public class ScrollListView extends ListView {    private boolean isOnMeasure;    public boolean isMeasure() {        return isOnMeasure;    }    public ScrollListView(Context context) {        super(context);    }    public ScrollListView(Context context, AttributeSet attrs) {        super(context, attrs);    }    public ScrollListView(Context context, AttributeSet attrs, int defStyle) {        super(context, attrs, defStyle);    }    @Override    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {        isOnMeasure = true;        int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,                MeasureSpec.AT_MOST);        super.onMeasure(widthMeasureSpec, expandSpec);//        super.onMeasure(widthMeasureSpec, heightMeasureSpec);    }    @Override    protected void onLayout(boolean changed, int l, int t, int r, int b) {        isOnMeasure = false;        super.onLayout(changed, l, t, r, b);    }}

2: xml of activity

 <view.ScrollListView                android: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 initData (View view, Object viewHolder, int position) {if (! (BaseGridView) viewGroup). isMeasure () {// The first time loading, processing Interface ui} else {// not the first loading, not processing anything }}

 

 

If you have a better method, please share your method. Thank you!

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.