Extract the Adapter of RecyclerView,

Source: Internet
Author: User

Extract the Adapter of RecyclerView,
Extract the Adapter of RecyclerView. When a list is used on many pages in the project, we may use ListView or RecyclerView. In fact, no matter which control is used for implementation, adapter and hodler are required. Each time you write a ListView or RecyclerView, you must implement an adapter and hodler. Obviously, there are a lot of repeated code, so can we extract the common methods? Obviously, yes. ListView extraction has previously been written.

The Adapter of RecyclerView inherits from RecyclerView. Adapter. ViewHolder here I introduce it in the form of generics and implement it in the BaseAdapter class as follows:

public class BaseAdapter extends RecyclerView.Adapter
   
    {    @Override    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {        return null;    }    @Override    public void onBindViewHolder(ViewHolder holder, int position) {    }    @Override    public int getItemCount() {        return 0;    }    public static class ViewHolder extends RecyclerView.ViewHolder {        public ViewHolder(View itemView) {            super(itemView);        }    }}
   

Well, here are the specific methods. How can we start? First, let's analyze what conditions are required to implement an Adapter? First, data is indispensable and generally transmitted in the form of a set. Here we can pass in from the constructor to determine the set, but the type cannot be determined. We can use D to identify it, instance variables carry generics. Remember to add generics to the class. Here I wrote an empty interface for Bidirectional binding, as shown below:

public class BaseAdapter
     
       extends RecyclerView.Adapter
      
        {    private List
       
         dataList;    public BaseAdapter(List
        
          dataList) {        this.dataList = dataList;    }    @Override    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {        return null;    }    @Override    public void onBindViewHolder(ViewHolder holder, int position) {    }    @Override    public int getItemCount() {        return dataList.size();    }    public static class ViewHolder extends RecyclerView.ViewHolder {        public ViewHolder(View itemView) {            super(itemView);        }    }}
        
       
      
     

When the data is passed in, there is nothing left. Right, View view. Here I changed BaseAdapter to an abstract class, and implemented an abstract method for obtaining the View, which is used in the onCreateViewHolder method, as follows:

public abstract int getLayoutId();@Overridepublic ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {    View view = LayoutInflater.from(parent.getContext()).inflate(getLayoutId(),parent,false);    BaseAdapter.ViewHolder vh = new BaseAdapter.ViewHolder(view);    return vh;}

Next, we will continue to abstract a method for data binding. In fact, the onBindViewHolder method is abstracted to itself to implement its functions:

public abstract void createHolder(BaseAdapter.ViewHolder holder, D d);    @Override    public void onBindViewHolder(ViewHolder holder, int position) {        createHolder(holder,dataList.get(position));    }

So far, the extraction in the adapter has been completed. Next, let's take a look at ViewHolder. In fact, ViewHolder is mainly used to obtain the control:

Public static class ViewHolder extends RecyclerView. ViewHolder {private View rootView; private SparseArray
       
        
Views = new SparseArray <> (); public ViewHolder (View itemView) {super (itemView);} public
        
         
T get (int id) {T view = (T) views. get (id); if (view = null) {view = (T) rootView. findViewById (id); views. put (id, view);} return view;} // set public void setText (int viewId, String txt) {TextView mTextView = get (viewId); mTextView. setText (txt);} // set public void setPic (int viewId, String url) {ImageView mImageView = get (viewId); Picasso. with (mContext ). load (url ). fit (). error (R. drawable. ic_launcher ). placeholder (mImageView. getDrawable ()). memoryPolicy (MemoryPolicy. NO_CACHE ). into (mImageView );}}
        
       

Here I have written two methods: setText and setPic. The main purpose is to make our code simple and convenient for us to call. Here, we have extracted the BaseAdapter. Let's take a look at the entire class:

/*** Created by AbnerMing on. */public abstract class BaseAdapter
       
        
Extends RecyclerView. Adapter
        
         
{Private List
         
          
DataList; public abstract int getLayoutId (); public BaseAdapter (List
          
           
DataList) {this. dataList = dataList;} @ Override public ViewHolder onCreateViewHolder (ViewGroup parent, int viewType) {View view = LayoutInflater. from (parent. getContext ()). inflate (getLayoutId (), parent, false); BaseAdapter. viewHolder VL = new BaseAdapter. viewHolder (view); return vh ;}@ Override public void onBindViewHolder (ViewHolder holder, int position) {createHolder (holder, dataList. get (position) ;}@ Override public int getItemCount () {return dataList. size ();} public abstract void createHolder (BaseAdapter. viewHolder holder, D d); public static class ViewHolder extends RecyclerView. viewHolder {private View rootView; private SparseArray
           
            
Views = new SparseArray <> (); public ViewHolder (View itemView) {super (itemView);} public
            
              T get (int id) {T view = (T) views. get (id); if (view = null) {view = (T) rootView. findViewById (id); views. put (id, view);} return view;} // set public void setText (int viewId, String txt) {TextView mTextView = get (viewId); mTextView. setText (txt);} // set public void setPic (int viewId, String url) {ImageView mImageView = get (viewId); Picasso. with (mContext ). load (url ). fit (). error (R. drawable. ic_launcher ). placeholder (mImageView. getDrawable ()). memoryPolicy (MemoryPolicy. NO_CACHE ). into (mImageView );}}}
            
           
          
         
        
       

Specific call:

public class MyAdapter extends BaseAdapter
         
          {    public MyAdapter(List
          
            dataList) {        super(dataList);    }    @Override    public int getLayoutId() {        return 0;    }    @Override    public void createHolder(BaseAdapter.ViewHolder holder, Bean bean) {    }}
          
         

In the future, all the adapters can inherit the BaseAdapter. It is much more convenient to implement only two methods.

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.