Full parsing of RecyclerView combined with AA (Android Annotations) injection framework instance (30th)

Source: Internet
Author: User

Full parsing of RecyclerView combined with AA (Android Annotations) injection framework instance (30th)

(1). Preface:

RecyclerView has been available for a long time and is widely used in many applications. It also has a good reputation in the entire developer circle, which means RecyclerView has a better reputation than ListView, controls such as GridView have many advantages, such as data binding, Item View creation, View recycling, and reuse. This series of articles includes the following three parts:

  1. Basic usage of the RecyclerView control, including basic, advanced, advanced, and animation (click to enter)
  2. RecyclerView controls (click to enter)
  3. RecyclerView control set AA (Android Annotations) injection framework instance

    Today, the third part of this series uses RecyclerView and AA (Android Annotations) injection framework instances. All Demo examples used in this tutorial have been updated to the following projects. Welcome to star and fork.

    [Note] for how to use the AA (Android Annotations) injection framework, I have updated a topic on CSDN (click to enter). If you are interested, learn more.

     

    (2). Basic implementation

    The demonstration here is also relatively simple, that is, using RecyclerView to implement the Vertical Slide list effect. The layout of each Item is as follows:

    1. Item layout file: item_user_item.xml

     

       
           
            
         
        
       

     

    The above layout has two text boxes, and then creates an object class with two attributes:

    2. TestUserBean. java

     

    public class TestUserBean {    private String firstName;    private String LastName;    public TestUserBean() {    }    public String getFirstName() {        return firstName;    }    public void setFirstName(String firstName){        this.firstName = firstName;    }    public String getLastName() {        return LastName;    }    public void setLastName(String lastName) {        LastName = lastName;    }    @Override    public String toString() {        return UserModel{ +                firstName=' +firstName + ''' +                , LastName=' +LastName + ''' +                '}';    }}

     

    3. Create a control that inherits LinearLayout to inject the layout, and add the method of Binding data to AAUserItemView. java. This class needs to be injected through @ EViewGroup:

     

    @EViewGroup(R.layout.item_user_item)public class AAUserItemView extends LinearLayout {    @ViewById    TextView tv_first;    @ViewById    TextView tv_last;    public AAUserItemView(Context context) {        super(context);    }    public void bind(TestUserBean userBean) {       tv_first.setText(userBean.getFirstName());       tv_last.setText(userBean.getLastName());    }}

     

    4. Because RecyclerView is used instead of a simple ListView, special processing is required. RecyclerView. Adapter creates ViewHolder instead of View, so ViewHolder class cannot be injected simply here. Here we create a generic class to wrap all views into ViewHolder.

     

    /*** Current class annotation: Create a generic class to wrap all types of views into ViewHonlder * Project name: FastDev4Android * package name: com. chinaztt. fda. test. recyclerViewAA * Author: Jiang qingqing on 15/11/21 * mailbox: jiangqqlmj@163.com * QQ: 781931404 * company: Jiangsu Zhongtian Technology Software Technology Co., Ltd. */public class ViewWrapper
       
        
    Extends RecyclerView. ViewHolder {public V view; public ViewWrapper (V itemView) {super (itemView); view = itemView;} public V getView () {return view ;}}
       

     

    5. Then we create a base class (abstract class) for all RecyclerView adapters, inheriting RecyclerView. Adapter >. At the same time, an onCreateItemView (ViewGroup parent, intviewType) abstract class is provided, allowing the caller to create the View by themselves. Then, a data set is provided to store the bound data.

     

    Import android. support. v7.widget. recyclerView; import android. view. view; import android. view. viewGroup; import java. util. arrayList; import java. util. list; public abstract class RecyclerViewAdapterBase
       
        
    Extends RecyclerView. Adapter
        
         
    > {/*** Stores the data to be bound */protected List
         
          
    Items = newArrayList
          
           
    (); Public List
           
            
    GetItems () {return items;} public void setItems (List
            
              Items) {this. items = items ;}@ Override public int getItemCount () {return items. size ();}/*** create a view bearer class * @ param parent * @ param viewType * @ return */@ Override public final ViewWrapper
             
               OnCreateViewHolder (ViewGroup parent, int viewType) {return new ViewWrapper
              
                (OnCreateItemView (parent, viewType);}/*** create a view Item, complete the assignment to the specific implementation class * @ param parent * @ param viewType * @ return */protected abstract VonCreateItemView (ViewGroup parent, int viewType );}
              
             
            
           
          
         
        
       

     

    6. Next we will implement a specific Adapter, create an AAUserAdapter, and use @ EBean for annotation in this class to inherit the Adapter base class, RecyclerViewAdapterBase . The following two methods are implemented:

    • Protected AAUserItemView onCreateItemView (ViewGroup parent, int viewType)
    • Public void onBindViewHolder (ViewWrapper holder, int position)

      Create views respectively, and call the onBindViewHolder () method to bind data.

       

      Package com. chinaztt. fda. test. recyclerViewAA; import android. content. context; import android. view. viewGroup; import org. androidannotations. annotations. EBean; import org. androidannotations. annotations. rootContext;/*** current class annotation: * Project name: FastDev4Android * package name: com. chinaztt. fda. test. recyclerViewAA * Author: Jiang qingqing on 15/11/21 * mailbox: jiangqqlmj@163.com * QQ: 781931404 * company: Jiangsu Zhongtian Technology Software Technology Co., Ltd. */@ EBeanpublic class AAUserAdapter extends RecyclerViewAdapterBase
           
            
      {@ RootContext Context context;/*** create an Item View * @ param parent * @ param viewType * @ return */@ Override protected AAUserItemView onCreateItemView (ViewGroup parent, int viewType) {return AAUserItemView _. build (context);}/*** bind data View * @ param holder * @ param position */@ Override public void onBindViewHolder (ViewWrapper holder, int position) {AAUserItemView view = holder. getView (); TestUserBean userBean = items. get (position); view. bind (userBean );}}
           

       

      7. Now we need to use RecyclerView and Adapter, but we need to prepare data before using it. Here we have prepared an interface class AAUserFinder. java

       

      public interface AAUserFinder {    List
           
             findAll();}
           

       

      Then there is the implementation class ImMemoryUserFinder of AAUserFinder. The findAll () method in the implementation interface here, and the class uses @ EBean label annotation.

       

      Packag ecom. chinaztt. fda. test. recyclerViewAA; import org. androidannotations. annotations. EBean; import java. util. arrayList; import java. util. list;/*** current class annotation: * Project name: FastDev4Android * package name: com. chinaztt. fda. test. recyclerViewAA * Author: Jiang qingqing on 15/11/21 * mailbox: jiangqqlmj@163.com * QQ: 781931404 * company: jiangsu Zhongtian Technology Software Technology Co., Ltd. */@ EBeanpublic class ImMemoryUserFinder implements AAUserFinder {@ Override public List
           
            
      FindAll () {List
            
             
      UserModels = new ArrayList
             
              
      (); For (int I = 1; I <= 45; I ++) {TestUserBean model = new TestUserBean (); model. setFirstName (First Zhang San: + I); model. setLastName (Last Li Si: + I); userModels. add (model) ;}return userModels ;}}
             
            
           

       

      8. the last step is to instantiate RecyclerView and use AAUserAdapter. This Activity uses @ EActivity for annotation, injects initialization controls through @ ViewById, and injects initialization AAUserAdapter and AAUserFinder instantiation through @ Bean (but according to polymorphism, the instantiated object here is ImMemoryUserFinder ). Call the @ AfterViews annotation method to create a layout manager, provide data to the Adapter, and bind the data. All code is as follows:

       

      Package com. chinaztt. fda. test; import android. OS. bundle; import android. support. v7.widget. linearLayoutManager; import android. support. v7.widget. orientationHelper; import android. support. v7.widget. recyclerView; import android. view. view; import android. widget. linearLayout; import android. widget. textView; import com. chinaztt. fda. test. recyclerViewAA. AAUserAdapter; import com. chinaztt. fda. test. recyclerViewAA. AAUserFinder; import com. chinaztt. fda. test. recyclerViewAA. imMemoryUserFinder; import com. chinaztt. fda. ui. r; import com. chinaztt. fda. ui. base. baseActivity; import org. androidannotations. annotations. afterInject; import org. androidannotations. annotations. afterViews; import org. androidannotations. annotations. bean; import org. androidannotations. annotations. click; import org. androidannotations. annotations. EActivity; import org. androidannotations. annotations. viewById;/*** current class annotation: RecyclerView set AA (Android Annotations) injection Framework Implementation instance * Project name: FastDev4Android * package name: com. chinaztt. fda. test * Author: Jiang Qing on 15/11/20 * mailbox: jiangqqlmj@163.com * QQ: 781931404 * company: Jiangsu Zhongtian Technology Software Technology Co., Ltd. */@ EActivity (R. layout. public class RecyclerViewAAActivity extends BaseActivity {@ ViewById LinearLayout comment; @ ViewById TextView top_bar_title; @ ViewById RecyclerView aa_recyclerview; @ Bean AAUserAdapter adapter; @ Bean (ImMemoryUserFinder. class) AAUserFinder userFinder; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState);} @ AfterViews public void initViews () {top_bar_title.setText (RecyclerView set AA injection framework instance); // set RecyerView, and bind the data LinearLayoutManager linearLayoutManager = new LinearLayoutManager (this); linearLayoutManager. setOrientation (OrientationHelper. VERTICAL); aa_recyclerview.setLayoutManager (linearLayoutManager); adapter. setItems (userFinder. findAll (); aa_recyclerview.setAdapter (adapter) ;}@ Click (R. id. top_bar_linear_back) public void clickButton (View view) {this. finish ();}}

       

      9. The running effect is as follows:

      10. the above class has a lot of Android Annotations injection framework labels. If you are not familiar with the AA framework, it will be a bit dizzy to read the above Code ~ However, I have updated the [AA (AndroidAnnotations) injection framework details topic for the AA injection learning topic. Click to enter...]

       

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.