1. How does listView improve its efficiency?

In fact, when converView is empty, use the setTag() method to bind each View to a ViewHolder object that holds the control. When convertView is not empty and reuses an already created view, the getTag() method is used to retrieve the bound ViewHolder, which avoids layer upon layer of findViewById searching for the control and quickly locate the control.

1) Reuse ConvertView, use historical view, improve efficiency 200%,

2) Customize the static class ViewHolder to reduce the number of findViewById. Increase efficiency by 50%

3) Load data asynchronously and load data in pages.

4) Use WeakRefrence to reference the ImageView object

2. How to update the ListView when the ListView dataset changes?

Use the notifyDataSetChanger() method on the Adapter of the ListView. This method causes the ListView to be redrawn.

3. How does ListView implement pagination loading?

SetOnScrollListener (new OnScrollListener{}) setOnScrollListener(new OnScrollListener{})

There are two methods in the listener: the method where the scroll state changes (onScrollStateChanged) and the method onScroll that is called when the listView is rolled

2) There are three ways to change the scrolling state: SCROLL_STATE_TOUCH_SCROLL: // Touch sliding inertial scrolling (FLGIN) : SCROLL_STATEFLING// glide state

Stationary state: SCROLL_STATE_IDLE: stationary

Load data in batches and only care about the rest state; Care about the last visible entry; if the last visible entry is the last one in the data adapter (collection), more data can be loaded. At each load, count the number of scrolls. When the number of scrolls is greater than or equal to the total number, the user can be reminded that there is no more data.

4. Can the ListView display multiple types of items

Each item displayed by the listView is displayed by getView(Int Position,View convertView,ViewGroup parent) of the baseAdapter. In theory we could make each entry a different type of view.

For example, if an id=1 is retrieved from the server, we load the item of type 1 when ID =1, and type 2 when id=2. The common layout is often seen in information clients.

Adapter also provides getViewTypeCount() and getItemViewType(int Position) methods. In the getView method we can load different layout files for different viewtypes.

5. How to locate the listView to the specified position

Lv.setselection (view.getPosition ()) can be provided by listView; Methods.

6. How to embed ListView in ScrollView

Normally we do not nest listViews in a ScrollView. Adding a ListView to a ScrollView will result in an incomplete display of the ListView control. Usually only one of the ListView controls will be displayed because of a collision of scrolling events between the two controls. Therefore, we need to calculate the display height of the ListView by the number of items in the ListView, so as to make it complete display, the following provides a method for your reference:

Lv = (ListView) findViewById(R.I.V); adapter =new MyAdapter(); lv.setAdapter(adapter); setListViewHeightBasedOnChildren(lv); public void setListViewHeightBasedOnChildren(ListView listview){ ListAdapter listAdapter =listView.getAdapter(); if(listAdapter ==null){ 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); params.height+=5; listView.setLayoutParams(params); }}Copy the code

The best way to handle this at this stage is to customize the ListView, override the onMeasure() method, and set all display.

package com.meiya.ui; import.android.widget.ListView; / * * * scrollview nested listview simple implementation in * ScrolViewwithListView in Java * / public class ScrollViewwithListView extends listview { public ScrollViewWithListView(android.content.Context context, android.util.AttributeSet attrs) { super(context, attrs); } /** ** Integer.MAX_VALUE>>2. If not set, Public void onMeasure(int widthMeasureSpec, int heightMeasureSpec){ int expandSpec =MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE>>2,MeasureSpec.AT_MOST); super.onMeasure(widthMeasureSpec,expandSpec); }}Copy the code

7. How to optimize images in ListView

There are many optimization strategies for pictures.

1). The way of processing pictures:

If the ListView custom Item involves a large number of pictures, be sure to carefully process the picture, because the picture occupies the memory of the ListView Item is the most headache, processing picture methods generally have the following:

(1. Don’t loop bitmapFactory.decodefile directly; Use Options to save image size, do not load image into memory.

(2. The picture must be compressed by the boundary, especially the larger picture, if your picture is processed by the background server, it is not needed

(3. When taking pictures in ListView, do not take a path to take pictures directly, but use WeakReference(use WeakReference instead of strong reference. For instance you can use the WeakReference mContextRef), SoftReference, WeakHashMap, etc to store the image information.

4. When converting images in getView, intermediate variables must be released in time

2. Basic idea of loading pictures asynchronously:

(1. First get the image display from memory cache (memory buffer)

(2. If not, obtain from SD card (SD card buffer)

(3) If you can’t get any pictures, download them from the network and save them to the SD card, add them to the memory and display them (depending on the situation)

Principle:

Optimization 1: load from memory first, if there is no open thread from the SD card or network to obtain the picture from the SD card is in the sub-thread, otherwise it will be not smooth fast slide screen.

In the adapter, there is a busy variable that indicates whether the ListView is sliding or not. If the listView is sliding, it only retrieves images from memory. If the listView is sliding, there is no need to open the thread to retrieve images from memory or network.

The thread pool is used in ImageLoader to prevent too many threads from being created and destroyed too frequently. It is not desirable to always create a new thread each time. A better AsyncTask class uses thread pools internally. When obtaining images from the network, it is saved to the SD card first, and then loaded into memory. The advantage of this method is that the image can be compressed during loading into memory to reduce the memory occupied by the image.

How does the problem of image misplacement in listView arise

The essence of the image mismatch problem comes from the fact that our ListView uses the cache convertView. Suppose a scenario where a ListView displays nine items on one screen, then when pulling out the tenth item, it actually reuses the first item. In other words, when the first item downloads the picture from the network and finally displays it, in fact, the item is no longer in the current display area, and the result of display at this time may output the image on the tenth item, which leads to the problem of image dislocation. So the solution is to show it if you see it, and not show it if you don’t see it.

9. How to refresh the listView data for a single item without refreshing the listView data?

Modify the data for a single item, and then call the notifyDataSetChanged() method of the adapter