我最喜欢的框架,没有之一: 编译期生成代码的方式,对运行时没有任何副作用。 加上AndroidStudio快捷键,简直好用之至。
二、Fragment中使用:
public class MyFragment extends Fragment { @BindView(R.id.imageView) ImageView mImageView; Unbinder unbinder; @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fg_test, container, false); //绑定View unbinder = ButterKnife.bind(this, view); return view; } @Override public void onDestroyView() { super.onDestroyView(); unbinder.unbind(); } //销毁时解绑View @OnClick(R.id.imageView) public void onViewClicked() { Toast.makeText(getContext(), "hello", Toast.LENGTH_LONG).show(); } }附:使用Fragment
FragmentTransaction ft = getSupportFragmentManager().beginTransaction(); ft.add(R.id.id_fl,new MyFragment()); ft.commit();还有其他很多注解,感觉都没用太大用,下面看一下源码是怎么工作的
bind有6个重载的方法:这里使用的是一参Activity的bind方法
@NonNull @UiThread public static Unbinder bind(@NonNull Activity target) { //获取Activity对应窗口上的最顶端布局 View sourceView = target.getWindow().getDecorView(); //调用createBinding方法,见--B1 return createBinding(target, sourceView); }这算一个非常核心的方法,6个bind()方法都是调用它
private static Unbinder createBinding(@NonNull Object target, @NonNull View source) { //获取target的class Class<?> targetClass = target.getClass(); if (debug) Log.d(TAG, "Looking up binding for " + targetClass.getName()); //获取绑定Class的构造函数,见--B2 Constructor<? extends Unbinder> constructor = findBindingConstructorForClass(targetClass); //如果构造函数是空的,返回EMPTY的Unbinder枚举 if (constructor == null) { return Unbinder.EMPTY; } //noinspection TryWithIdenticalCatches Resolves to API 19+ only type. try { //返回使用构造函数创建MainActivity_ViewBinding实例: return constructor.newInstance(target, source); } catch (IllegalAccessException e) { throw new RuntimeException("Unable to invoke " + constructor, e); } catch (InstantiationException e) { throw new RuntimeException("Unable to invoke " + constructor, e); } catch (InvocationTargetException e) { Throwable cause = e.getCause(); if (cause instanceof RuntimeException) { throw (RuntimeException) cause; } if (cause instanceof Error) { throw (Error) cause; } throw new RuntimeException("Unable to create binding instance.", cause); } }通过字节码文件获取类的构造函数
@Nullable @CheckResult @UiThread private static Constructor<? extends Unbinder> findBindingConstructorForClass(Class<?> cls) { //BINDINGS的声明:可见是一个LinkedHashMap,以class为键,构造函数为值。 //static final Map<Class<?>, Constructor<? extends Unbinder>> BINDINGS = new LinkedHashMap<>(); //从map中拿传入的cls的构造函数 Constructor<? extends Unbinder> bindingCtor = BINDINGS.get(cls); //如果不为空 if (bindingCtor != null) { if (debug) Log.d(TAG, "HIT: Cached in binding map."); //就返回拿到的构造函数 return bindingCtor; } //否则,获取字节码文件的名字:如:com.toly1994.butterknifetest.MainActivity String clsName = cls.getName(); //如果名字的字符串,是以android.或java.开头的 if (clsName.startsWith("android.") || clsName.startsWith("java.")) { if (debug) Log.d(TAG, "MISS: Reached framework class. Abandoning search."); //返回空 return null; } try { //加载com.toly1994.butterknifetest.MainActivity_ViewBinding类生成Clazz对象bindingClass:见:--B3 Class<?> bindingClass = cls.getClassLoader().loadClass(clsName + "_ViewBinding"); //noinspection unchecked //获取自动生成的MainActivity_ViewBinding中的两参构造函数 bindingCtor = (Constructor<? extends Unbinder>) bindingClass.getConstructor(cls, View.class); if (debug) Log.d(TAG, "HIT: Loaded binding class and constructor."); } catch (ClassNotFoundException e) { if (debug) Log.d(TAG, "Not found. Trying superclass " + cls.getSuperclass().getName()); bindingCtor = findBindingConstructorForClass(cls.getSuperclass()); } catch (NoSuchMethodException e) { throw new RuntimeException("Unable to find binding constructor for " + clsName, e); } //将cls和获取到的构造函数放入map BINDINGS.put(cls, bindingCtor); return bindingCtor; }Butter Knife会自动创建这个类,我们来看看它的庐山真面目
MainActivity_ViewBinding.png可见bind方法,主要是把XxxActivity创建一个XxxActivity_ViewBinding,并创建一个XxxActivity_ViewBinding对象
根据类型查询需要的View 这个who只是在抛异常的时候告诉你,是谁异常
public static <T> T findRequiredViewAsType(View source, @IdRes int id, String who, Class<T> cls) { //findRequiredView(视图,id,字段介绍):见--B4-1 View view = findRequiredView(source, id, who); //castView(视图,id,字段, Class):见--B4-2 return castView(view, id, who, cls); }看到findViewById有没有小激动
public static View findRequiredView(View source, @IdRes int id, String who) { //真正的findViewById操作 View view = source.findViewById(id); if (view != null) { //如果视图不为空就返回找到的视图 return view; } //视图为空,就抛出一个IllegalStateException异常: String name = getResourceEntryName(source, id); throw new IllegalStateException("Required view '" + name + "' with ID " + id + " for " + who + " was not found. If this view is optional add '@Nullable' (fields) or '@Optional'"cast()方法是Clazz的一个公共方法:由下可见它反会一个由传入值强转成的T类型对象
@SuppressWarnings("unchecked") public T cast(Object obj) { if (obj != null && !isInstance(obj)) throw new ClassCastException(cannotCastMsg(obj)); return (T) obj; }继承自:View.OnClickListener
public abstract class DebouncingOnClickListener implements View.OnClickListener { //是否可用 static boolean enabled = true; //可以再次使用 private static final Runnable ENABLE_AGAIN = new Runnable() { @Override public void run() { enabled = true; } }; @Override public final void onClick(View v) { //如果可用 if (enabled) { //设置为不可用 enabled = false; // v.post(ENABLE_AGAIN); doClick(v);//模板方法 } } public abstract void doClick(View v); }[1]本文由张风捷特烈原创,转载请注明 [2]欢迎广大编程爱好者共同交流 [3]个人能力有限,如有不正之处欢迎大家批评指证,必定虚心改正 [4]你的喜欢与支持将是我最大的动力
更多安卓技术欢迎访问:安卓技术栈 我的github地址:欢迎star 简书首发,腾讯云+社区同步更新 张风捷特烈个人网站,编程笔记请访问:http://www.toly1994.com
QQ:1981462002 邮箱:1981462002@qq.com 微信:zdl1994328
转载于:https://www.cnblogs.com/toly-top/p/9781872.html
相关资源:butterknife 8.8.1