Android高效开发的ButterKnife view框架介绍

来源:深圳沃龙软件开发公司     2017-04-06

工具片

高效开发的ButterKnife

        Butterknife是JakeWharton大神写的一个专注于Android系统的View注入框架,可以减少大量的findViewById以及setOnClickListener代码,通过插件可视化一键生成。

先看一下Butterknife和传统的findViewById的区别


class ExampleActivity extends Activity {
  @BindView(R.id.title) TextView title;
  @BindView(R.id.subtitle) TextView subtitle;
  @BindView(R.id.footer) TextView footer;

  @Override public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.simple_activity);
    ButterKnife.bind(this);
    // TODO Use fields...
  }}

通过@BindView方法和一个view的id 去自动绑定layout中的View,减少大量代码。

那么Butterknife是如何工作的呢?


可能很多人都觉得ButterKnife在bind(this)方法执行的时候通过反射获取ExampleActivity中所有的带有@Bind注解的属性并且获得注解中的R.id.xxx值,最后还是通过反射拿到Activity.findViewById()方法获取View,并赋值给ExampleActivity中的某个属性。这是一个注解库的实现方式,比较原始,一个很大的缺点就是在Activity运行时大量使用反射会影响App的运行性能,造成卡顿以及生成很多临时Java对象更容易触发GC。ButterKnife显然没有使用这种方式,它用了Java Annotation Processing技术,就是在Java代码编译成Java字节码的时候就已经处理了@Bind、@OnClick(ButterKnife还支持很多其他的注解)这些注解。

所以在性能方面完全和原始方法一样。

Butterknife很强大,他不仅可以通过@BindView来绑定View,还有@BindBool@BindColor@BindDimen@BindDrawable@BindInt,@BindString来绑定你想绑定的res资源文件

class ExampleActivity extends Activity {
  @BindString(R.string.title) String title;
  @BindDrawable(R.drawable.graphic) Drawable graphic;
  @BindColor(R.color.red) int red; // int or ColorStateList field
  @BindDimen(R.dimen.spacer) Float spacer; // int (for pixel size) or float (for exact value) field
  // ...}

@OnClick( ) : 绑定控件点击事件

 @OnClick(R.id.submit)
 public void submit(View view) {
  // TODO submit data to server...
  }

@OnClick( ) : 绑定控件多个view的点击事件

 @OnClick({R.id.button1,R.id.button2,R.id.button3})
 public void OnClick(View view) {
     switch(view.getId){
         case R.id.button1:
         // TODO ...
         break;
         case R.id.button2:
         // TODO ...
         break;
         ...
     }
  }

还可以在fragment中使用

public class FancyFragment extends Fragment {
  @BindView(R.id.button1) Button button1;
  @BindView(R.id.button2) Button button2;

  @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fancy_fragment, container, false);
    ButterKnife.bind(this, view);
    // TODO Use fields...
    return view;
  }}

亦可在adapater中使用

 public class MyAdapter extends BaseAdapter {
  @Override public View getView(int position, View view, ViewGroup parent) {
    ViewHolder holder;
    if (view != null) {
      holder = (ViewHolder) view.getTag();
    } else {
      view = inflater.inflate(R.layout.whatever, parent, false);
      holder = new ViewHolder(view);
      view.setTag(holder);
    }

    holder.name.setText("John Doe");
    // etc...

    return view;
  }

  static class ViewHolder {
    @BindView(R.id.title) TextView name;
    @BindView(R.id.job_title) TextView jobTitle;

    public ViewHolder(View view) {
      ButterKnife.bind(this, view);
    }
  }}

有关ButterKnife 的更多内容,请看github地址:https://github.com/JakeWharton/butterknife

使用 Android ButterKnife Zelezny 插件 可以一键生成@bindview 

http://blog.csdn.net/dreamlivemeng/article/details/51261170


本内容皆为 深圳沃龙软件开发公司 原创
如需转载,请注明文章出处和来源网址:http://www.wooolong.com/blog/8.html

全部评论

ManuelGlic | 2023-07-25

With thanks, Wonderful stuff! 《a href="HHtps://researchproposalforphd.com/"》custom research paper writing services《/a》 proposal writer 《a href="HHtps://writingresearchtermpaperservice.com/"》research proposal cover page《/a》 proposal essay 《a href=HHtps://hireawriterforanessay.com/》write an essay《/a》 website that writes essays for you 《a href=HHtps://theessayswriters.com/》what should i write my college essay about《/a》 write my thesis for me good college application essay HHtps://dissertat

Reply

Oscarsduam | 2023-07-26

Perfectly spoken certainly. ! 《a href=HHtps://domyhomeworkformecheap.com/》do my math homework《/a》 do my chemistry homework 《a href=HHtps://domycollegehomeworkforme.com/》homework help《/a》 homework help

Reply

Shaenpoirm | 2023-07-26

You've made your point. 《a href="HHtps://theessayswriters.com/"》write my essay online《/a》 writing essays for money 《a href="HHtps://bestcheapessaywriters.com/"》write me an essay《/a》 help me write my essay

Reply

ManuelGlic | 2023-08-04

Truly quite a lot of great info. 《a href="HHtps://dissertationwritingtops.com/"》writing a dissertation《/a》 writing help 《a href="HHtps://helpwritingdissertation.com/"》dissertation service《/a》 best dissertation 《a href=HHtps://bestpaperwritingservice.com/》pay for college papers《/a》 buy college paper 《a href=HHtps://bestonlinepaperwritingservices.com/》buy college paper《/a》 best college paper writing service umi thesis HHtps://bestonlinepaperwritingservices.com

Reply

Oscarsduam | 2023-08-05

You actually revealed it very well! 《a href=HHtps://essaypromaster.com/》paper writing service《/a》 write a research paper 《a href=HHtps://paperwritingservicecheap.com/》how to write a paper《/a》 paper writer online

Reply

Oscarsduam | 2023-08-06

You actually said it well! 《a href=HHtps://domyhomeworkformecheap.com/》cpm homework《/a》 pay someone to do my homework 《a href=HHtps://domycollegehomeworkforme.com/》xyz homework《/a》 hire someone to do my homework

Reply

Oscarsduam | 2023-08-08

Regards. I value this! 《a href=HHtps://domyhomeworkformecheap.com/》my homework《/a》 do my homework for money 《a href=HHtps://domycollegehomeworkforme.com/》do my math homework《/a》 should i do my homework

Reply

ManuelGlic | 2023-08-10

Kudos, Loads of facts! 《a href="HHtps://studentessaywriting.com/"》writing service essay《/a》 resume writing services 《a href="HHtps://essaywritingserviceahrefs.com/"》write paper service《/a》 fast essay writing service 《a href=HHtps://payforanessaysonline.com/》where to buy essays online《/a》 pay to write my essay 《a href=HHtps://buycheapessaysonline.com/》pay someone to write paper《/a》 essays for sale how to teach essay writing HHtps://custompaperwritersservices.com

Reply

Shaenpoirm | 2023-08-10

Kudos, Lots of write ups. 《a href="HHtps://writinganessaycollegeservice.com/"》writing an argumentative essay《/a》 writing an informative essay 《a href="HHtps://essayservicehelp.com/"》college essay writing《/a》 what is the best essay writing service

Reply

Shaenpoirm | 2023-08-19

Regards. Numerous material. 《a href="HHtps://service-essay.com/"》custom paper《/a》 buy a paper 《a href="HHtps://custompaperwritingservices.com/"》order custom paper《/a》 paper writing services

Reply

ManuelGlic | 2023-08-19

Incredible a good deal of awesome facts. 《a href="HHtps://argumentativethesis.com/"》write a thesis《/a》 tentative thesis 《a href="HHtps://bestmasterthesiswritingservice.com/"》phd thesis database《/a》 thesis paper 《a href=HHtps://hireawriterforanessay.com/》do my essay《/a》 essay writers online 《a href=HHtps://theessayswriters.com/》write my essay online《/a》 online essay writers writing an admissions essay HHtps://argumentativethesis.com

Reply

Oscarsduam | 2023-08-19

Lovely content, Many thanks. 《a href=HHtps://argumentativethesis.com/》working thesis《/a》 college thesis 《a href=HHtps://bestmasterthesiswritingservice.com/》argumentative thesis statement《/a》 tentative thesis

Reply

Shaenpoirm | 2023-08-24

This is nicely said! ! 《a href="HHtps://service-essay.com/"》order custom paper《/a》 buy a paper for college 《a href="HHtps://custompaperwritingservices.com/"》buy a paper《/a》 pay for college papers

Reply

ManuelGlic | 2023-08-25

You explained that well. 《a href="HHtps://payforanessaysonline.com/"》pay for essay online《/a》 buy essays cheap 《a href="HHtps://buycheapessaysonline.com/"》pay for papers《/a》 pay for an essay 《a href=HHtps://homeworkcourseworkhelps.com/》reddit do my homework《/a》 homework help cpm 《a href=HHtps://helpmedomyxyzhomework.com/》homework《/a》 do my homework for money how to write an essay about a quote HHtps://argumentativethesis.com

Reply

Oscarsduam | 2023-08-25

You said this fantastically. 《a href=HHtps://customthesiswritingservice.com/》research thesis《/a》 thesis statements 《a href=HHtps://writingthesistops.com/》strong thesis statement《/a》 thesis statment

Reply

Shaenpoirm | 2023-08-25

Terrific tips. Cheers. 《a href="HHtps://englishessayhelp.com/"》assignment help《/a》 cheap essay help 《a href="HHtps://essaywritinghelperonline.com/"》help essay《/a》 helping others essay

Reply

ManuelGlic | 2023-08-26

You actually stated this perfectly. 《a href="HHtps://payforanessaysonline.com/"》pay for essay《/a》 pay someone to write paper 《a href="HHtps://buycheapessaysonline.com/"》pay for paper《/a》 buy essay online 《a href=HHtps://essaytyperhelp.com/》essay writing service《/a》 essay writer 《a href=HHtps://helptowriteanessay.com/》help writing essay《/a》 essay helper online persuasive essay on why college athletes should be paid HHtps://buyanessayscheaponline.com

Reply

Oscarsduam | 2023-08-26

Many thanks, I enjoy this. 《a href=HHtps://quality-essays.com/》pay for essay《/a》 where to buy essays online 《a href=HHtps://buyanessayscheaponline.com/》buy essays cheap《/a》 order essay online

Reply

Shaenpoirm | 2023-08-27

You suggested that well. 《a href="HHtps://essayssolution.com/"》do my essay for me《/a》 write my paper for me free 《a href="HHtps://cheapessaywriteronlineservices.com/"》do my essay free《/a》 do my essay

Reply

Oscarsduam | 2023-08-28

Fantastic write ups. Thanks! 《a href=HHtps://ouressays.com/》buy research paper《/a》 term papers 《a href=HHtps://researchpaperwriterservices.com/》proposal essay《/a》 college term papers

Reply

Shaenpoirm | 2023-08-28

You have made your stand extremely nicely.. 《a href="HHtps://ouressays.com/"》proposal writing《/a》 online proposal 《a href="HHtps://researchpaperwriterservices.com/"》buy research paper《/a》 research paper writers

Reply

ManuelGlic | 2023-09-01

Cheers! Awesome information. 《a href="HHtps://dissertationwritingtops.com/"》phd paper《/a》 define dissertation 《a href="HHtps://helpwritingdissertation.com/"》best dissertation《/a》 dissertation editing services 《a href=HHtps://writingpaperforme.com/》paper writer online《/a》 website that writes papers for you 《a href=HHtps://custompaperwritersservices.com/》how to write a response paper《/a》 paper writers for college phd writer HHtps://argumentativethesis.com

Reply

Shaenpoirm | 2023-09-02

You reported that wonderfully. 《a href="HHtps://argumentativethesis.com/"》thesis writing service《/a》 argumentative thesis statement 《a href="HHtps://bestmasterthesiswritingservice.com/"》write a thesis《/a》 a thesis statement

Reply

ManuelGlic | 2023-09-05

Info effectively used!! 《a href="HHtps://essaywritingservicehelp.com/"》writing a good essay《/a》 affordable essay writing service 《a href="HHtps://essaywritingservicebbc.com/"》essay writing meme《/a》 best essay writing service australia 《a href=HHtps://phdthesisdissertation.com/》dissertation abstract《/a》 what is a phd 《a href=HHtps://writeadissertation.com/》dissertation service《/a》 dissertation meaning write essay online HHtps://dissertationwritingtops.com

Reply

Oscarsduam | 2023-09-06

Effectively voiced indeed! ! 《a href=HHtps://customthesiswritingservice.com/》thesis creator《/a》 argumentative thesis 《a href=HHtps://writingthesistops.com/》thesis writer《/a》 writing a thesis statement

Reply

Shaenpoirm | 2023-09-07

Terrific information. Many thanks! 《a href="HHtps://essayssolution.com/"》writing essays《/a》 write essay 《a href="HHtps://cheapessaywriteronlineservices.com/"》who can write my essay《/a》 writing an opinion essay

Reply

ManuelGlic | 2023-09-07

You explained that exceptionally well! 《a href="HHtps://helpwithdissertationwriting.com/"》dissertations《/a》 phd dissertation 《a href="HHtps://dissertationwritingtops.com/"》dissertation help services《/a》 dissertation proposal 《a href=HHtps://theessayswriters.com/》essay writers《/a》 ai essay writer 《a href=HHtps://bestcheapessaywriters.com/》writing essays for money《/a》 essay writer website writer essay HHtps://domycollegehomeworkforme.com

Reply

Oscarsduam | 2023-09-08

Truly many of terrific advice. 《a href=HHtps://englishessayhelp.com/》free writing assistant《/a》 essay helper online 《a href=HHtps://essaywritinghelperonline.com/》writing helper《/a》 essay writing help

Reply

Shaenpoirm | 2023-09-09

Truly plenty of good tips. 《a href="HHtps://writingpaperforme.com/"》writing a research paper《/a》 professional paper writers 《a href="HHtps://custompaperwritersservices.com/"》paper writer《/a》 write my paper for me

Reply

ManuelGlic | 2023-09-09

You actually said this wonderfully. 《a href="HHtps://writinganessaycollegeservice.com/"》which essay writing service is the best《/a》 professional essay writing 《a href="HHtps://essayservicehelp.com/"》essay writing companies《/a》 research paper writing service 《a href=HHtps://writingpaperforme.com/》paper writers for college《/a》 paper writing services 《a href=HHtps://custompaperwritersservices.com/》professional paper writers《/a》 research paper writer services why us college essay HHtps://bestc

Reply

Oscarsduam | 2023-09-10

Great info. Thanks! 《a href=HHtps://customthesiswritingservice.com/》thesis writing《/a》 thesis writer 《a href=HHtps://writingthesistops.com/》thesis help《/a》 thesis binding

Reply

Shaenpoirm | 2023-09-11

Nicely put. Cheers! 《a href="HHtps://hireawriterforanessay.com/"》essay writer no plagiarism《/a》 writing a good essay 《a href="HHtps://theessayswriters.com/"》help me write my essay《/a》 write my paper for me

Reply

ManuelGlic | 2023-09-11

You actually suggested it fantastically. 《a href="HHtps://writinganessaycollegeservice.com/"》essay writing《/a》 best assignment writing service 《a href="HHtps://essayservicehelp.com/"》coursework writing service《/a》 essay writing 《a href=HHtps://essayssolution.com/》do my essay for me《/a》 writing an opinion essay 《a href=HHtps://cheapessaywriteronlineservices.com/》write my essay for cheap《/a》 online essay writers steps to write a narrative essay HHtps://payforanessaysonline.com

Reply

Oscarsduam | 2023-09-12

Cheers. Plenty of data! 《a href=HHtps://dissertationwritingtops.com/》define dissertation《/a》 buy dissertations 《a href=HHtps://helpwritingdissertation.com/》dissertation abstracts《/a》 dissertation help services

Reply

Shaenpoirm | 2023-09-12

Amazing write ups. Thanks a lot! 《a href="HHtps://customthesiswritingservice.com/"》thesis writer《/a》 strong thesis statement 《a href="HHtps://writingthesistops.com/"》research thesis《/a》 thesis binding

Reply

ManuelGlic | 2023-09-13

You revealed that really well. 《a href="HHtps://essaywritingservicelinked.com/"》best essay service《/a》 writing an essay introduction 《a href="HHtps://essaywritingservicetop.com/"》cheap essay service《/a》 essay writting service 《a href=HHtps://domyhomeworkformecheap.com/》do my math homework for me《/a》 cpm homework 《a href=HHtps://domycollegehomeworkforme.com/》reddit do my homework《/a》 online coursework george orwell essay on writing HHtps://bestonlinepaperwritingservices.com

Reply

Oscarsduam | 2023-09-13

Nicely put, Many thanks. 《a href=HHtps://bestpaperwritingservice.com/》buy a paper《/a》 cheap research paper writing service 《a href=HHtps://bestonlinepaperwritingservices.com/》paper help《/a》 paper writer services

Reply

Shaenpoirm | 2023-09-14

Thanks. Quite a lot of posts. 《a href="HHtps://homeworkcourseworkhelps.com/"》do my homework for me《/a》 coursework 《a href="HHtps://helpmedomyxyzhomework.com/"》can you do my homework《/a》 do my college homework

Reply

ManuelGlic | 2023-09-15

You actually explained it very well! 《a href="HHtps://essaytyperhelp.com/"》paper writing help《/a》 essay helper online 《a href="HHtps://helptowriteanessay.com/"》help with essay writing《/a》 buy essay 《a href=HHtps://studentessaywriting.com/》essay writing sites《/a》 fast essay writing service 《a href=HHtps://essaywritingserviceahrefs.com/》writing a narrative essay《/a》 best essay writing website how to write an essay for college application HHtps://writingresearchtermpaperservice.com

Reply

Shaenpoirm | 2023-09-16

Thank you! An abundance of information. 《a href="HHtps://bestpaperwritingservice.com/"》buy a paper《/a》 buy a paper 《a href="HHtps://bestonlinepaperwritingservices.com/"》order custom paper《/a》 paper writing services

Reply

ManuelGlic | 2023-09-17

Thanks! Wonderful information! 《a href="HHtps://quality-essays.com/"》pay for essays《/a》 buy an essay online 《a href="HHtps://buyanessayscheaponline.com/"》buy essay《/a》 order essay online 《a href=HHtps://researchproposalforphd.com/》research paper writers《/a》 write my research paper for me 《a href=HHtps://writingresearchtermpaperservice.com/》research paper help《/a》 term paper help letter writing service HHtps://writinganessaycollegeservice.com

Reply

Oscarsduam | 2023-09-17

Nicely put. Thank you! 《a href=HHtps://homeworkcourseworkhelps.com/》do my college homework《/a》 homework help cpm 《a href=HHtps://helpmedomyxyzhomework.com/》do my homework《/a》 homework help

Reply

Shaenpoirm | 2023-09-18

With thanks. Lots of data. 《a href="HHtps://argumentativethesis.com/"》thesis titles《/a》 a thesis statement 《a href="HHtps://bestmasterthesiswritingservice.com/"》thesis statements《/a》 writing a thesis statement

Reply

ManuelGlic | 2023-09-19

Thank you. Very good information! 《a href="HHtps://writingpaperforme.com/"》best essay writers《/a》 write paper for me 《a href="HHtps://custompaperwritersservices.com/"》how to write a reflection paper《/a》 writing a paper 《a href=HHtps://writingpaperforme.com/》paper writer services《/a》 essay writers online 《a href=HHtps://custompaperwritersservices.com/》how to write an analysis paper《/a》 paper writer services write my essay south park HHtps://studentessaywriting.com

Reply

Oscarsduam | 2023-09-19

Useful content. Thanks. 《a href=HHtps://writingpaperforme.com/》writing paper《/a》 write a paper 《a href=HHtps://custompaperwritersservices.com/》cheap paper writers《/a》 kindergarten writing paper

Reply

Shaenpoirm | 2023-09-20

Many thanks. Ample information! 《a href="HHtps://dissertationwritingtops.com/"》dissertation data analysis help《/a》 best dissertation 《a href="HHtps://helpwritingdissertation.com/"》dissertation writing service《/a》 dissertation proposal

Reply

ManuelGlic | 2023-09-21

You explained this really well! 《a href="HHtps://helpwithdissertationwriting.com/"》dissertation proposal《/a》 phd dissertation 《a href="HHtps://dissertationwritingtops.com/"》writing a dissertation《/a》 phd paper 《a href=HHtps://englishessayhelp.com/》essay writer《/a》 help me write an essay 《a href=HHtps://essaywritinghelperonline.com/》essay help《/a》 helping others essay writing a narrative essay HHtps://writingthesistops.com

Reply

Oscarsduam | 2023-09-21

Beneficial data. Many thanks. 《a href=HHtps://service-essay.com/》buy a paper for college《/a》 custom handwriting paper 《a href=HHtps://custompaperwritingservices.com/》college paper writing service《/a》 paper writer services

Reply

Shaenpoirm | 2023-09-22

Incredible a lot of great information! 《a href="HHtps://studentessaywriting.com/"》top essay writing《/a》 admission essay services 《a href="HHtps://essaywritingserviceahrefs.com/"》best essay service《/a》 best essay writing service in usa

Reply

ManuelGlic | 2023-09-23

You stated that effectively! 《a href="HHtps://essaywritingservicelinked.com/"》best online resume writing service《/a》 essay writing service 3 hours 《a href="HHtps://essaywritingservicetop.com/"》best resume writing service 2020《/a》 writing service 《a href=HHtps://studentessaywriting.com/》spongebob writing essay《/a》 article writing service 《a href=HHtps://essaywritingserviceahrefs.com/》essay writing service uk《/a》 best custom essay writing service custom essay writing online HHtps://buycheape

Reply

Oscarsduam | 2023-09-23

Cheers! Fantastic stuff! 《a href=HHtps://writingpaperforme.com/》write a research paper《/a》 write my paper for cheap 《a href=HHtps://custompaperwritersservices.com/》do my paper《/a》 essay writter

Reply

Shaenpoirm | 2023-09-24

Cheers! Plenty of facts! 《a href="HHtps://researchproposalforphd.com/"》termpaper《/a》 research paper helper 《a href="HHtps://writingresearchtermpaperservice.com/"》research proposal《/a》 custom research paper writing services

Reply

ManuelGlic | 2023-09-25

Thanks. Ample data! 《a href="HHtps://researchproposalforphd.com/"》research proposals《/a》 termpaper 《a href="HHtps://writingresearchtermpaperservice.com/"》proposal writer《/a》 research paper proposal 《a href=HHtps://dissertationwritingtops.com/》dissertation paper《/a》 dissertation editing services 《a href=HHtps://helpwritingdissertation.com/》dissertation abstracts international《/a》 dissertation abstracts international essay writing website reviews HHtps://researchpaperwriterservices.com

Reply

Oscarsduam | 2023-09-25

Amazing plenty of useful facts. 《a href=HHtps://essaytyperhelp.com/》buy essay《/a》 help essay 《a href=HHtps://helptowriteanessay.com/》essay helper free《/a》 essay helper free

Reply

Shaenpoirm | 2023-09-26

Helpful forum posts. Thank you. 《a href="HHtps://researchproposalforphd.com/"》research proposal cover page《/a》 buy term paper 《a href="HHtps://writingresearchtermpaperservice.com/"》research paper writer services《/a》 write my research paper for me

Reply

Arletha | 2024-03-09

I've been surfing online more than 3 hours today, yet I never found any interesting article like yours. It's pretty worth enough for me. In my view, if all web owners and bloggers made good content as you did, the web will be a lot more useful than ever before.

Reply

发表评论