ListView has multiple Item View Optimizations
The optimization of ListView has been explained on the Internet. The optimization of ListView is generally as follows:
1) Item View reuse optimization to prevent memory overflow)
2) View search optimization reduces execution time)
3) slide optimization: loading image data and other non-skid dynamic freezing during sliding)
The above is basically the optimization corresponding to a single ItemView. for the optimization of multiple itemviews, BaseAdapter is generally used
Two methods provided
GetItemViewType (): Return View type. The default value is 0.
GetViewTypeCount (): The number of itemviews returned. The default value is 1.
If there is only one type of item view, you do not need to override these two methods.
If there are multiple, You need to rewrite this method, and the returned results must meet the following requirements:
1) the return value of getItemViewType () must be greater than or equal to 0 and smaller than the number of types. Why is this range because
There is a data in ListView to cache the used Item View. For details, see the source code.
2) the return value of the getViewTypeCount () method is the maximum number of Item view types you may encounter. ListView will return the value based on this
Create a cache Array
If there are two View types, you can simply rewrite the getView method in the Adapter. You can create the corresponding View based on the getItemViewType) returned value.
But what if there are six and seven? So the getView method of the Adapter) How much code to write, How troublesome it will be to maintain at that time, you can see that you write well,
What if I change to another person? Very painful .....
I encountered this problem when I was doing our app. The Order List and the Item View corresponding to each order are very different, which is very inconvenient to operate, if there is a small
Changes will be delayed for a long time. The new type is not easy to handle.
I have made a little Optimization on the problem, but it may not be the best, but at least it can solve the problem. If you have a better way, please contact us.
Design:
Each Item View is provided using the provider's design method. Different itemviews have different providers. 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. The implementation method is the same as the getView () method of the Adapter, reducing the learning cost.
I also inherited BaseAdapter to implement an Adapter named MiltilViewListAdapter. java, which implements the two methods mentioned above and the getView () method.
The provider only needs to configure the instance to MiltilViewListAdapter.
Generally, a Bean set must be passed to the Adapter. In my design, all the beans here implement an interface to indicate which provider it corresponds.
Usage:
- [Mw_shl_code = java, true] private ListView mListView;
- Private List <IItemBean> mList = new ArrayList <IItemBean> ();
- @ 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 set that provides the provider class of all expected display types
- // The implementation of getView is implemented in provider, which is the same as that in adapter.
- List <Class <? Extends IViewProvider> providers = new ArrayList <Class <? Extends IViewProvider> ();
- Providers. add (FlightOrderViewProvider. class );
- Providers. add (SticketOrderViewProvider. class );
- MiltilViewListAdapter adpater = new MiltilViewListAdapter (getApplication (), mList, providers );
- MListView. setAdapter (adpater );
- } [/Mw_shl_code]
Link: http://www.eoeandroid.com/forum.php? Mod = viewthread & tid = 329890 & extra = page % 3D2% 26 filter % 3 Ddateline % 26 orderby % 3 Dlastpost % 26 dateline % 3D604800 & page = 1