Listview of Android interview questions

Preface

Autumn recruits are coming soon, Ji Meng will prepare a set of Android primary interview questions before the end of the National Day holiday. I hope it will be helpful to everyone.


Tip: The following is the content of this article

ListView

1. How to update ListView when the ListView data set is changed

Use the notifyDataSetChanged() method of the adapter of this ListView. This method causes the ListView to redraw.


2. How ListView implements paging loading

① Set the scroll listener of ListView: setOnScrollListener(new OnScrollListener{....})

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

② In the method of changing the scrolling state, there are three states:

  • The state of the finger pressing and moving: SCROLL_STATE_TOUCH_SCROLL: // Touch and slide
  • Inertial rolling (flgin state): SCROLL_STATE_FLING: // gliding
  • Static state: SCROLL_STATE_IDLE: // static

Deal with different states:

Load data in batches, and only care about the static state: care about the last visible item, if the last visible item is the last one in the data adapter (collection), more data can be loaded at this time. At each load, the number of scrolls is calculated. When the number of scrolls is greater than or equal to the total number, the user can be prompted that there is no more data.


3. How to refresh the data of a single item in the ListView without refreshing the data of the entire ListView?

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


4. How to implement pull-up loading and pull-down refresh?

Implement the OnScrollListener interface to rewrite the onScrollStateChanged and onScroll methods, use the onscroll method to implement "slide" post-processing to check whether there are new records, if so, call addFooterView, add records to the adapter, and the adapter calls notifyDataSetChanged to update the data; if there are no records, put Remove custom mFooterView. Use onScrollStateChanged to detect whether to scroll to the last line and stop scrolling and then perform loading.


5. How to optimize pictures in ListView?

There are many optimization strategies for pictures.

1. The way of processing pictures:

If a large number of pictures are involved in the custom Item in the ListView, you must process the pictures carefully, because the memory occupied by the pictures is the most troublesome of the ListView items. There are roughly the following ways to deal with pictures:

  • ① Don't loop BitmapFactory.decodeFile directly with the path; use Options to save the image size, and don't load the image to the memory.
  • ②. The picture must be compressed by the boundary, especially the larger picture, if your picture is processed by the background server, it is not necessary
  • ③. Don't take a path to fetch the picture directly when taking pictures in ListView, but use WeakReference (use WeakReference instead of strong references. For example, WeakReference mContextRef can be used), SoftReference, WeakHashMap, etc. to store picture information.
  • ④. When doing picture conversion in getView, the intermediate variables generated must be released in time

2. The basic idea of ​​loading pictures asynchronously:

  • 1), first get the picture display from the memory cache (memory buffer)
  • 2). If not available, get it from the SD card (SD card buffering)
  • 3) If you can't get it, download the picture from the network and save it to the SD card and add it to the memory and display it (depending on the situation, whether to display it)

principle:

Optimization 1: Load from the memory first, if not, start the thread to get it from the SD card or the network. Here, note that getting pictures from the SD card is executed in the sub-thread, otherwise the screen will not be smooth enough for fast sliding.

Optimization 2: At the same time, there is a busy variable in the adapter, which indicates whether the listview is in a sliding state. If it is in a sliding state, only the picture is obtained from the memory. If not, there is no need to start the thread to get the picture from external storage or the network.

Optimization 3: The threads in the ImageLoader use the thread pool, which avoids the frequent creation and destruction of too many threads. It is very undesirable if a new thread is used to execute each time. It is better to use the AsyncTask class, which is actually internal The thread pool is also used. When getting a picture from the network, first save it to the SD card and then load it into the memory. The advantage of this is that it can be compressed when loaded into the memory to reduce the memory occupied by the picture.


6. Is there a Button in the Listview that does not move?

The reason is that the button is forced to obtain the focus of the item, as long as the focusable of the button is set to false.


7. How does ListView improve its efficiency?

When convertView is empty, use the setTag() method to bind a ViewHolder object that stores the control to each View.
When the convertView is not empty and the created view is reused, the getTag() method is used to obtain the bound ViewHolder object, which avoids the layer-by-layer query of the control by findViewById, but quickly locates the control.

① Reuse ConvertView and use historical views to increase efficiency by 200%

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

③ Load data asynchronously and load data in pages.

④ Use WeakRefrence to reference the ImageView object


8. Can ListView display multiple types of items?

This is of course possible. Each item displayed by the ListView is displayed through the getView (int position, View convertView, ViewGroup parent) of the baseAdapter. In theory, we can make each item a different type of view.

For example, if we get back an identifier with id=1 from the server, then when id=1, we load an item of type 1, and when id=2, load an item of type 2. Common layouts can often be seen in news clients.

In addition, the adapter also provides two methods: getViewTypeCount() and getItemViewType(int position). In the getView method, we can load different layout files according to different viewtypes.


9. How to locate the ListView to the specified position?

You can use the lv.setSelection(listView.getPosition()); method provided by ListView.


10. How to embed ListView in ScrollView?

  • Normally, we don't nest ListView in ScrollView, but it's okay if the interviewer asks me to nest.
  • Adding a ListView to the ScrollView will cause the listview control to be displayed incompletely, usually only one is displayed. This is caused by the conflict between the scroll events of the two controls. Therefore, it is necessary to calculate the display height of the listview by the number of items in the listview to make it fully displayed.
  • The processing method at this stage is: custom ListView, overload onMeasure () method, set all display.

11. How does the problem of picture misalignment in ListView occur?

The nature of the picture misalignment problem stems from the use of cache convertView in our listview. Assuming a scenario where a listview displays nine items on one screen, then when the tenth item is pulled out, the item is actually reused as the first An item, that is to say, when the first item downloads a picture from the network and finally displays it, the item is no longer in the current display area. The consequences of displaying at this time may output an image on the tenth item. This leads to the problem of picture misalignment. So the solution is to display if it is visible, and not display if it is invisible.


12. Do you know the setEmptyView method of ListView?

In Android development, when using ListView to display data, if the data is empty, usually a prompt view needs to be displayed, and setEmptyView can solve this problem


About sorting out

When everything is sorted, it will be sorted into pdf format for easy reading. The file is obtained as shown below (after October 8)!


Insert picture description here

Guess you like

Origin blog.csdn.net/qq_42761395/article/details/108893360