ButterKnife结合RecyclerView.Adapter一起使用

版权声明:本文为博主原创文章,转载请注明出处。 https://blog.csdn.net/harrain/article/details/72382560

直接上代码:

[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. @Override  
  2.     public MainHodler onCreateViewHolder(ViewGroup parent, int viewType) {  
  3.         View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_main, parent, false);  
  4.         return new MainHodler(view);  
  5.     }  


注入到ViewHolder中
[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. class MainHodler extends RecyclerView.ViewHolder {  
  2.         @Bind(R.id.item_main_txt_title)  
  3.         TextView itemMainTxtTitle;  
  4.   
  5.         @Bind(R.id.item_main_img)  
  6.         SimpleDraweeView itemMainImg;  
  7.   
  8.         @Bind(R.id.item_main_rl)  
  9.         RelativeLayout itemMainRl;  
  10.   
  11.         @Bind(R.id.item_main_card)  
  12.         CardView itemMainCard;  
  13.   
  14.   
  15.         public MainHodler(View itemView) {  
  16.             super(itemView);  
  17.             ButterKnife.bind(this, itemView);  
  18.         }  
  19.     }  

使用心得:

1.Activity ButterKnife.bind(this);必须在setContentView();之后,且父类bind绑定后,子类不需要再bind

2.Fragment ButterKnife.bind(this, mRootView);

3.属性布局不能用private or static 修饰,否则会报错

4.setContentView()不能通过注解实现。(其他的有些注解框架可以)

官网http://jakewharton.github.io/butterknife/


猜你喜欢

转载自blog.csdn.net/harrain/article/details/72382560