Implementing View bindings using Butterknife

Source: Internet
Author: User

Using the Butterknife framework, you can easily use annotations to implement view bindings, resource bindings, event bindings, and so on. The most convenient use of butterknife is to view the bindings so as to bid farewell to the cumbersome through Findviewbyid () to get the view reference. Let's use a very simple example to illustrate this.

The example is very simple and displays a custom layout using a ListView.

Main layout Activity_main.xml:

<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools "android:layout_width=" match_parent "android:layout_height=" Match_parent "Tools:context =". Mainactivity "> <listview android:id=" @+id/listview "android:layout_width=" Match_parent "Android oid:layout_height= "Wrap_content"/></relativelayout>

The view that needs to be bound in the main layout is Id/listview

Custom layout Item_layt.xml:

<?xml version= "1.0"  encoding= "Utf-8"? ><relativelayout xmlns:android= "http// Schemas.android.com/apk/res/android "    android:layout_width=" Match_parent "     android:layout_height= "70DP"     android:gravity= "center_vertical"      android:orientation= "Horizontal" ><imageview android:id= "@+id/item_imageview"      android:layout_width= "48DP"     android:layout_height= "48DP"      android:layout_alignparentleft= "true"     android:layout_marginleft= "10DP"/>     <textview android:id= "@+id/item_textview"          android:layout_width= "Wrap_content"         android:layout_ height= "Wrap_content"         android:textsize= "18SP"          aNdroid:layout_alignparentright= "true"         android:layout_ marginright= "10DP"         android:gravity= "center"          android:textcolor= "@color/dodgerblue"/></relativelayout>

This custom layout is the layout required by Viewholder and requires binding Item_imageview and Item_textview.

Next, customize a adapter.

package com.app.wx.butterknifetraning.adapter;import android.content.context;import  android.view.layoutinflater;import android.view.view;import android.view.viewgroup;import  Android.widget.baseadapter;import android.widget.imageview;import android.widget.textview;import  com.app.wx.butterknifetraning.R;import java.util.List;import java.util.Random;import  Butterknife. Bind;import butterknife. butterknife;/** * created by ubuntu on 15-9-1. */public class  myadapter extends baseadapter {    private int[] drawables =  new int[]{R.drawable.mark01, R.drawable.mark02,             R.drawable.mark03, R.drawable.mark04};    private  context mcontext;    private list<string> mcontents;     private layoutinflater mlayoutinflater;    public myadapter (Context  context, list<string> contents)  {         this.mcontext = context;        this.mcontents =  Contents;        mlayoutinflater = layoutinflater.from ( Context);    }     @Override     public int  getcount ()  {        return mcontents.size ();     }     @Override     public object getitem ( Int position)  {        return position;     }     @Override     public long getitemid (int  position)  {        return position;    }     @Override     public view getview (int position, view convertview,  viewgroup parent)  {    //using Viewhold to lift high performance          viewholder holder;        if (convertView !=  null) {             holder = ( Viewholder)  convertview.gettag ();        }else{             convertview = mlayoutinflater.inflate ( R.layout.item_layot, parent, false);             holder = new viewholder (Convertview);             convertview.settAG (Holder);        }         Holder.mTextView.setText (Mcontents.get (position));        random  Random = new random ();         int index =  random.nextint (drawables.length);         Holder.mImageView.setImageResource (Drawables[index]);         return  convertview;    }    static class viewholder {          @Bind (R.id.item_imageview)     // Binding imageview        imageview mimageview;          @Bind (R.id.item_textview)      //binding textview         textview mtextview;&nbSp;       public viewholder (View view)  {             butterknife.bind (This, view);    // View binding           }    }}


Main mainactivity

package com.app.wx.butterknifetraning;import android.support.v7.app.appcompatactivity;import  android.os.bundle;import android.view.menu;import android.view.menuitem;import  android.widget.listview;import com.app.wx.butterknifetraning.adapter.myadapter;import  Java.util.arraylist;import java.util.list;import butterknife. Bind;import butterknife. butterknife;public class mainactivity extends appcompatactivity {      @Bind (R.id.listview)         ListView mListView;     private List<String> myList;     @Override      protected void oncreate (bundle savedinstancestate)  {         super.oncreate (savedinstancestate);         Setcontentview (r.layout.activity_main);          butterknife.bind (This);        myadapter  Adapter = new myadapter (this, mylist);         Mlistview.setadapter (adapter);     }    private void initdata ()  {        myList = new ArrayList<String> () ;         mylist.add ("Item1");         mylist.add ("item2");         mylist.add ("Item3");         mylist.add ("Item4");         Mylist.add ("Item5");         mylist.add ("Item6");         mylist.add ("Item7");     }}

It is very convenient to use a @bind (r.id.idname) and then use the Butterknife.bind () to bind the view.

Implementing View bindings using Butterknife

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.