In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces "what are the optimization methods of Item View in ListView". In daily operation, I believe that many people have doubts about the optimization methods of Item View in ListView. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubts about "what are the optimization methods of Item View in ListView?" Next, please follow the editor to study!
The optimization of ListView has been explained a lot on the Internet. The general optimization of ListView is as follows:
(1) Item View reuse optimization (preventing memory overflow)
(2) View lookup optimization (reduce execution time)
(3) sliding optimization, loading picture data when sliding (anti-sliding stutter)
The above is basically the optimization corresponding to a single ItemView, and BaseAdapter is generally used to optimize a variety of ItemView.
Two methods provided
GetItemViewType (): returns View type. Default returns 0.
GetViewTypeCount (): returns how many types of itemView there are. Default returns 1.
If there is only one type of item view, there is no need to override these two methods.
If there are more than one, you need to override this method, and the return must meet the following requirements:
(1) the return value of getItemViewType () must be greater than or equal to 0 and less than the number of types. Why this range? it's because
There is a data in ListView, which is used to cache the Item View that has been used. See the source code for details.
(2) the return value of getViewTypeCount () is the number of Item view types that you may encounter, and ListView will return the value based on this.
To create a cache array
If there are two View types, you can simply override the getView () method in Adapter and create a corresponding View based on the return value of getItemViewType ().
But what if there are six and seven? So how much code does the getView () method of Adapter have to write, and how troublesome it will be to maintain it? you can see for yourself that it's okay.
What if it were someone else? It's very painful.
When I was doing our app, I encountered such a problem, the order list, the Item View corresponding to each order will be very different, and it is extremely inconvenient to operate, such as small ones.
Changes will take a long time. New types are also difficult to deal with.
I have made a little optimization for the problems encountered, which can not be a show, but at least it can solve the above problems. If you have a better way, you are also welcome to communicate.
Design:
Using the provider's design to provide each ItemView, different ItemView will have different providers, and the provider needs to implement an interface:
Public interface IViewProvider {public abstract View getItemView (View convertView, LayoutInflater inflater, Object data);}
The provider only needs to implement this interface and then implement the getItemView () method in exactly the same way as Adapter's getView (), reducing learning costs.
I also inherit BaseAdapter to implement a special Adapter called MiltilViewListAdapter.java, which implements the two methods mentioned earlier, and the getView () method
The provider only needs to configure the instance of MiltilViewListAdapter.
Normally, a Bean collection is passed to Adapter, and my design is to have the bean in it implement an interface that identifies which provider it corresponds to.
How to use it:
[mw_shl_code=java,true] private ListView mListView; private List mList = new ArrayList (); @ Override protected void onCreate (Bundle savedInstanceState) {super.onCreate (savedInstanceState); setContentView (R.layout.activity_main); createData (); mListView = (ListView) findViewById (R.id.my_listview) / / the difference is that there is an additional provider collection that provides the implementation of provider class / / getView for all desired display types implemented in provider, just as List is used in adapter
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.