Adapters and lists (16) Best practices for using the AndroidAnnnotations injection framework)

Source: Internet
Author: User

Adapters and lists (16) Best practices for using the AndroidAnnnotations injection framework)

 

 

(1). Preface:

We have explained how to use the DI framework in Adapters and lists.

 

(2) Introduction to Apdaters and lists:

Here we will show you how to use AndroidAnnotations for simple processing of Adapter and AdapterView. The following is an example:

First, it consists of a Person entity class:

 

public class Person{    public final String firstName;    public final String lastName;     public Person(String firstName, StringlastName) {        this.firstName = firstName;        this.lastName = lastName;    }}
Then there is a PersoFinder interface:

 

 

public interfacePersonFinder {    List
 
   findAll();}
 

 

Now we need to create a PersonListActivity to list all people. To achieve this goal, we need a PersonListAdater adapter to bind data to the view. Also, you must use PersonItemView to represent each item in the list. The PersonItemView we use here will have two textviews. The example is as follows:

 

@EViewGroup(R.layout.person_item)public classPersonItemView extends LinearLayout {     @ViewById    TextView firstNameView;     @ViewById    TextView lastNameView;     public PersonItemView(Context context) {        super(context);    }     public void bind(Person person) {       firstNameView.setText(person.firstName);        lastNameView.setText(person.lastName);    }}
Now there is an InMemoryPersonFinder that implements the PersonFinder interface, which is annotated by @ EBean. The adapter directly operates on the data and creates a view to display the data;

 

 

@EBeanpublic classPersonListAdapter extends BaseAdapter {     List
 
   persons;     @Bean(InMemoryPersonFinder.class)    PersonFinder personFinder;     @RootContext    Context context;     @AfterInject    void initAdapter() {        persons = personFinder.findAll();    }     @Override    public View getView(int position, ViewconvertView, ViewGroup parent) {         PersonItemView personItemView;        if (convertView == null) {            personItemView =PersonItemView_.build(context);        } else {            personItemView = (PersonItemView)convertView;        }         personItemView.bind(getItem(position));         return personItemView;    }     @Override    public int getCount() {        return persons.size();    }     @Override    public Person getItem(int position) {        return persons.get(position);    }     @Override    public long getItemId(int position) {        return position;    }}
 
The PersonListActivity binds the PersonListAdapter to the ListView, but a toast is displayed when the PersonItemView is clicked.

 

 

@EActivity(R.layout.person_list)public classPersonListActivity extends Activity {    @ViewById    ListView personList;     @Bean    PersonListAdapter adapter;     @AfterViews    void bindAdapter() {        personList.setAdapter(adapter);    }     @ItemClick    void personListItemClicked(Person person) {        makeText(this, person.firstName +  + person.lastName, LENGTH_SHORT).show();    }}
(3). Use RecyclerView and ViewHolder:

 

If you are using RecyclerView instead of a simple ListView, you have to handle it specially. RecyclerView. Apdater creates ViewHolder instead of view. Therefore, you cannot simply inject ViewHolder class, but you can use @ EViewGroup for annotation.

Create a generic class to encapsulate all types of views into ViewHondler

 

public classViewWrapper
 
   extends RecyclerView.ViewHolder {     private V view;     public ViewWrapper(V itemView) {        super(itemView);        view = itemView;    }     public V getView() {        return view;    }}
 
Create a base class for all RecyclerView adapters

 

 

public abstractclass RecyclerViewAdapterBase
 
   extendsRecyclerView.Adapter
  
   > {     protected List
   
     items = newArrayList
    
     ();     @Override    public int getItemCount() {        return items.size();    }     @Override    public final ViewWrapper
     
      onCreateViewHolder(ViewGroup parent, int viewType) {        return newViewWrapper
      
       (onCreateItemView(parent, viewType)); } protected abstract VonCreateItemView(ViewGroup parent, int viewType); // additional methods to manipulate theitems}
      
     
    
   
  
 
You can start using

 

 

@EBeanpublic classPersonAdapter extends RecyclerViewAdapterBase
 
   {     @RootContext    Context context;     @Override    protected PersonItemViewonCreateItemView(ViewGroup parent, int viewType) {        return PersonItemView_.build(context);    }     @Override    public voidonBindViewHolder(ViewWrapper
  
    viewHolder, int position) {        PersonItemView view =viewHolder.getView();        Person person = items.get(position);         view.bind(person);    } }
  
 
If you create an interface for the binding method, you can move the onBindViewHondler implementation to the base class.

 

At this point, the best practices of AndroidAnnotations Apdaters, lists, RecyclerView, and ViewHolder have been fully explained.

 

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.