标签:blank 3.2 over medium find include his ima oid
简介
ButterKnife是一个专注于Android系统的View注入框架,当一个布局十分复杂时,需要引入执行大量的findViewById代码来找到View的对象,有了ButterKnife可以很轻松的省去这些步骤。是大神JakeWharton的力作,目前使用很广。最重要的一点,使用ButterKnife对性能基本没有损失,因为ButterKnife用到的注解并不是在运行时反射的,而是在编译的时候生成新的class。项目集成起来也是特别方便,使用起来也是特别简单。
ButterKnife的优势
github 地址: https://github.com/JakeWharton/butterknife
如何使用?
在Android Studio 项目中配置ButterKnife
1.在Project的 build.gradle 中添加如下代码:
buildscript { repositories { google() jcenter() } dependencies { classpath ‘com.android.tools.build:gradle:3.2.1‘ //添加如下代码 classpath ‘com.jakewharton:butterknife-gradle-plugin:9.0.0-rc2‘ // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files } }
2.在App的 build.gradle 中添加如下代码:
apply plugin: ‘com.android.application‘ apply plugin: ‘com.jakewharton.butterknife‘//添加这一行 android { compileSdkVersion 28 defaultConfig { applicationId "com.hejun.loader" minSdkVersion 26 targetSdkVersion 28 versionCode 1 versionName "1.0" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile(‘proguard-android.txt‘), ‘proguard-rules.pro‘ } } } dependencies { implementation fileTree(dir: ‘libs‘, include: [‘*.jar‘]) implementation ‘com.android.support:appcompat-v7:28.0.0‘ implementation ‘com.android.support.constraint:constraint-layout:1.1.3‘ testImplementation ‘junit:junit:4.12‘ androidTestImplementation ‘com.android.support.test:runner:1.0.2‘ androidTestImplementation ‘com.android.support.test.espresso:espresso-core:3.0.2‘ implementation ‘com.jakewharton:butterknife:9.0.0-rc2‘ annotationProcessor ‘com.jakewharton:butterknife-compiler:9.0.0-rc2‘//添加这一行 }
ButterKnife插件的添加:可以快速绑定
File -> Settings -> Plugins -> 搜索ButterKnife,找到Android ButterKnife Zeleany进行安装重启AndroidStudio
插件的使用:在布局资源上右键点击,如下图
选中需要绑定的view ,点击confim会自动生成代码
1.绑定注解,视图,资源等等
2.绑定事件
注意:在使用Butterknife 是要 绑定Butterknife .如下
1.在activity中绑定
public class MainActivity extends AppCompatActivity{ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //绑定初始化ButterKnife ButterKnife.bind(this); } }
2.在Fragment中绑定
Fragment的生命周期不同于activity。在onCreateView中绑定一个Fragment时,在onDestroyView中将视图设置为null。当你调用bind来为你绑定一个Fragment时,Butter Knife会返回一个Unbinder的实例。在适当的生命周期(onDestroyView)回调中调用它的unbind方法进行Fragment解绑。使用ButterKnife.bind(this, view)进行绑定。
public class ButterknifeFragment extends Fragment{ private Unbinder unbinder; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment, container, false); //返回一个Unbinder值(进行解绑),注意这里的this不能使用getActivity() unbinder = ButterKnife.bind(this, view); return view; } /** * onDestroyView中进行解绑操作 */ @Override public void onDestroyView() { super.onDestroyView(); unbinder.unbind(); } }
3.在Adapter中绑定
在Adapter的ViewHolder中使用,将ViewHolder加一个构造方法,在new ViewHolder的时候把view传递进去。使用ButterKnife.bind(this, view)进行绑定
class Adapter extends BaseAdapter { private Context context; private List<Userbean> users = new ArrayList<>(); public Adapter(Context context) { this.context = context; } public void addUsers(List<Userbean> userList) { users.addAll(userList); notifyDataSetChanged(); } @Override public int getCount() { return users.size(); } @Override public Userbean getItem(int position) { return users.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder viewHolder = null; if (convertView == null) { convertView = LayoutInflater.from(context).inflate(R.layout.item, parent, false); viewHolder = new ViewHolder(convertView); convertView.setTag(viewHolder); } else { viewHolder = (ViewHolder) convertView.getTag(); } Userbean userbean = users.get(position); viewHolder.name.setText(userbean.getName()); viewHolder.password.setText(userbean.getPassword()); return convertView; } class ViewHolder { @BindView(R.id.name) TextView name; @BindView(R.id.password) TextView password; ViewHolder(View view) { ButterKnife.bind(this, view); } } }
标签:blank 3.2 over medium find include his ima oid
原文地址:https://www.cnblogs.com/conglingkaishi/p/10090801.html