ListView has multiple Item View Optimizations

Source: Internet
Author: User

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:

 
 
  1. public interface IViewProvider { 
  2.     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:

 
 
  1. [Mw_shl_code = java, true] private ListView mListView;
  2. Private List <IItemBean> mList = new ArrayList <IItemBean> ();
  3. @ Override
  4. Protected void onCreate (Bundle savedInstanceState ){
  5. Super. onCreate (savedInstanceState );
  6. SetContentView (R. layout. activity_main );
  7. CreateData ();
  8. MListView = (ListView) findViewById (R. id. my_listview );
  9. // The difference is that there is an additional provider set that provides the provider class of all expected display types
  10. // The implementation of getView is implemented in provider, which is the same as that in adapter.
  11. List <Class <? Extends IViewProvider> providers = new ArrayList <Class <? Extends IViewProvider> ();
  12. Providers. add (FlightOrderViewProvider. class );
  13. Providers. add (SticketOrderViewProvider. class );
  14. MiltilViewListAdapter adpater = new MiltilViewListAdapter (getApplication (), mList, providers );
  15. MListView. setAdapter (adpater );
  16. } [/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

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.