Encapsulation and abstraction of Android Adapter

Source: Internet
Author: User

Encapsulation and abstraction of Android Adapter

In the development process, ViewPager, ListView, and GridView are often used, and these view controls with items must all use their adapters, when we implement View display, we usually write a custom Adapter to inherit the PagerAdapter, Adapter, or Adapter subclass, because the adapters that come with Android source code are abstract, during our development, some of the custom adapters we write need to override some methods of the Adapter parent class. During the rewriting, there are a lot of universal code, which is relatively coupled, the following code encapsulates and abstracts the Child class BaseAdapter of PagerAdapter and Adapter, eliminating the need to overwrite these general codes every time. The Code is as follows:

1.ViewPagerAdapter

 

/*** General ViewPagerAdapter * @ author Jenly **/public class ViewPagerAdapter extends PagerAdapter {private List
 
  
ListViews = null; public ViewPagerAdapter (List
  
   
ListViews) {this. listViews = listViews;} @ Overridepublic void destroyItem (View container, int position, Object object) {(ViewPager) container ). removeView (listViews. get (position) ;}@ Overridepublic int getCount () {return listViews. size () ;}@ Overridepublic Object instantiateItem (View container, int position) {(ViewPager) container ). addView (listViews. get (position), 0); return listViews. get (position) ;}@ Overridepublic boolean isViewFromObject (View paramView, Object paramObject) {return paramView = paramObject;} public List
   
    
GetListViews () {return listViews;} public void setListViews (List
    
     
ListViews) {this. listViews = listViews ;}}
    
   
  
 
Because the items of ViewPager basically inherit the View, this ViewPagerAdapter can basically be used as a universal adapter for the ViewPager control.

 

 

2.AbstractAdapter

 

/*** Abstract adapter (removes some common code) * @ author Jenly ** @ param
 
  
*/Public abstract class AbstractAdapter
  
   
Extends BaseAdapter {protected Context context; protected List
   
    
ListData; protected LayoutInflater layoutInflater; public AbstractAdapter (Context context, List
    
     
ListData) {this. context = context; this. listData = listData; layoutInflater = LayoutInflater. from (context) ;}@ Overridepublic int getCount () {if (listData! = Null) {return listData. size () ;}return 0 ;}@ Overridepublic Object getItem (int position) {if (listData! = Null) {return listData. get (position);} return null ;}@ Overridepublic long getItemId (int position) {return position;} public List
     
      
GetListData () {return listData;} public void setListData (List
      
        ListData) {this. listData = listData ;}}
      
     
    
   
  
 

AbstractAdapter Override methods that have common characteristics in the parent class. When customizing an adapter, you only need to inherit the AbstractAdapter , Override the getView method.

 

 

3.HolderAdapter

 

/*** Common adapter (suitable for some common adapters) * @ author Jenly ** @ param
   
    
*/Public abstract class HolderAdapter
    
     
Extends AbstractAdapter
     
      
{Public HolderAdapter (Context context, List
      
       
ListData) {super (context, listData) ;}@ Overridepublic View getView (int position, View convertView, ViewGroup parent) {Object holder = null; if (convertView = null) {convertView = buildConvertView (layoutInflater); holder = buildHolder (convertView); convertView. setTag (holder);} else {holder = convertView. getTag ();} bindViewData (holder, listData. get (position), position); return convertView;}/*** create convertView * @ param layoutInflater * @ return */public abstract View buildConvertView (LayoutInflater layoutInflater ); /*** create a View Holder * @ param convertView * @ return */public abstract Object buildHolder (View convertView ); /*** bind data ** @ param holder * @ param t * @ param position */public abstract void bindViewDatas (Object holder, T t, int position );}
      
     
    
   

The HolderAdapter inherits from the AbstractAdapter class above and overwrites and abstracts the getView method to make the code more concise and easier to use, as long as it is a custom adapter class inherited from BaseAdapter, instead, it inherits from HolderAdapter, which is basically common. Then, you only need to implement the BuildConvertView, buildHolder, and bindViewDatas methods.

 

 

The following is a custom test adapter that inherits the three abstract methods implemented by the HolderAdapter:

 

public class TestHolderAdapter extends HolderAdapter
   
    {public TestHolderAdapter(Context context, List
    
      listData) {super(context, listData);}@Overridepublic View buildConvertView(LayoutInflater layoutInflater) {return layoutInflater.inflate(R.layout.activity_list_item, null);}@Overridepublic Object buildHolder(View convertView) {ViewHolder holder = new ViewHolder();holder.tv = (TextView)convertView.findViewById(R.id.tv);return holder;}@Overridepublic void bindViewDatas(Object holder, String t, int position) {((ViewHolder)holder).tv.setText(t);}private static class ViewHolder{TextView tv;}}
    
   


 

 


 

Related Article

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.