ButterKnife 的简易替代品,不为findViewById折腰

为什么要造轮子

自己平常写代码一直用ButterKnife,功能太强大了有木有!但是最近有个需要在匿名内部类中去findView的时候却发现ButterKnife好像不支持在匿名内部类中进行find,(我也不确定支不支持,反正没找到相应的方法)编译后没有生成相应的ViewBinder类,应该是编译时故意忽略了匿名内部类吧。所以此时也很无奈啊。。。只能自己动手丰衣足食了。

内容为原创,希望能给需要的人帮助,不喜勿喷

一、思路

将需要查找的View包装成WrapView对象放到查找队列中,指定View的类型和id,在容器(activity,fragment等)初始化完View以后再调用ViewFinder的findFrom方法进行查找。
需要使用View时调用WrapView的get()方法将View取出。

使用起来确实稍微有点麻烦。。。不过作为强迫症患者,这样的代码↓↓↓能把我逼疯

public class MainActivity extends Activity{
    private TextView nameTv;
    private TextView ageTv;
    private TextView sexTv;
    private TextView nationTv;
    private TextView addressTv;
    private Button confirmBtn;
    private Button cancelBtn;
    private Button closeBtn;
    private LinearLayout mainLayout;
    private LinearLayout loadingLayout;
    private LinearLayout errorLayout;
    
    onCreate(){
        supper.onCreate();
        setConentView(R.layout.activity_main);
        findViews();
        initView();
    }

    public void findViews(){
        nameTv = (TextView) findViewById(R.id.tv_name);
        ageTv= (TextView) findViewById(R.id.tv_age);
        sexTv= (TextView) findViewById(R.id.tv_sex);
        nationTv = (TextView) findViewById(R.id.tv_nation);
        addressTv= (TextView) findViewById(R.id.tv_address);
        confirmBtn = (Button) findViewById(R.id.btn_confirm);
        cancelBtn= (Button) findViewById(R.id.btn_cancel);
        closeBtn = (Button) findViewById(R.id.btn_close);
        mainLayout= (LinearLayout) findViewById(R.id.layout_main);
        loadingLayout= (LinearLayout) findViewById(R.id.layout_loading);
        errorLayout= (LinearLayout) findViewById(R.id.layout_error);
    }

    public void initView(){
        nameTv.setText("张三");    
        ageTv.setText("26");      
        sexTv.setText("男");     
        ...
    }
    ...

}

经过改进后的代码是这样的


public class MainActivity extends Activity{

    private ViewFinder finder = new ViewFinder();
    private WrapView<TextView> nameTv= finder.apply(R.id.tv_name);
    private WrapView<TextView> ageTv= finder.apply(R.id.tv_age);
    private WrapView<TextView> sexTv= finder.apply(R.id.tv_sex);
    private WrapView<TextView> nationTv= finder.apply(R.id.tv_nation);
    private WrapView<TextView> addressTv= finder.apply(R.id.tv_address);
    private WrapView<Button> confirmBtn= finder.apply(R.id.btn_confirm);
    private WrapView<Button> cancelBtn= finder.apply(R.id.btn_cancel);
    private WrapView<Button> closeBtn= finder.apply(R.id.btn_close);
    private WrapView<LinearLayout > mainLayout= finder.apply(R.id.layout_main);
    private WrapView<LinearLayout > loadingLayout= finder.apply(R.id.layout_loading);
    private WrapView<LinearLayout > errorLayout= finder.apply(R.id.layout_error);
    
    onCreate(){
        supper.onCreate();
        setConentView(R.layout.activity_main);
        finder.findFrom(this);
        initView();
    }

    public void initView(){
        nameTv.get().setText("张三");    
        ageTv.get()..setText("26");      
        sexTv.get()..setText("男");   
        confirmBtn.get().setOnclickListener(this);  
        ...
    }
    ...

}

二、WrapView 和 ViewFinder的代码

WrapView

/**
 * Wapper class to maintain View.
 * Created by Leo on 2018/2/12.
 */
public class WrapView<T extends View> {
    private T view;
    @IdRes
    private int id;

    WrapView(@IdRes int id) {
        this.id = id;
    }

    public T get() {
        return view;
    }

    void setView(T view) {
        this.view = view;
    }

    void find(View from) {
        if (from == null) throw new NullPointerException("find view from a null object.");
        view = (T) from.findViewById(id);
        if (view == null) throw new ViewNotFoundException("View not found for id : " + id);
    }

    void find(Activity from) {
        if (from == null) throw new NullPointerException("find view from a null object.");
        view = (T) from.findViewById(id);
        if (view == null) throw new ViewNotFoundException("View not found for id : " + id);
    }
}

ViewFinder

/**
 * Helper class to maintain finding tasks.
 * Created by Leo on 2018/2/12.
 */
public class ViewFinder {
    private HashMap<Integer, WrapView<?>> views = new HashMap<>();

    public <T extends View> WrapView<T> apply(@IdRes int id){
        WrapView<T> wrapper = (WrapView<T>) views.get(id);
        if (wrapper == null){
            wrapper = new WrapView<>(id);
            views.put(id,wrapper);
        }
        return wrapper;
    }

    public void findFrom(View from) {
        Collection<WrapView<?>> wrapViews = views.values();
        for (WrapView<?> wrapView : wrapViews) {
            wrapView.find(from);
        }
    }

    public void findFrom(Activity from) {
        Collection<WrapView<?>> wrapViews = views.values();
        for (WrapView<?> wrapView : wrapViews) {
            wrapView.find(from);
        }
    }
}

代码量很少,需要的童鞋可以直接拿去用,祝大家新年快乐!

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 175,633评论 25 709
  • 0X0 前言 做过Android开发的猿类很多都知道ButterKnife这么个东西。这个库可以大大的简化我们的代...
    knightingal阅读 4,071评论 1 10
  • 俗话说的好“不想偷懒的程序员,不是好程序员”,我们在日常开发android的过程中,在前端activity或者fr...
    蛋西阅读 10,383评论 0 14
  • 天云被光映黄金,亮清奇峰见群松。 游客眺望江山喜,照留美景日后恋。
    海南黑哥阅读 1,384评论 0 2
  • 当今时代“梦想”这个词的使用频率很高,近日读到两位作家关于两部作品的评论,而这两部作品恰好都与梦想有关,她们的评论...
    茫然的蒲公英阅读 4,538评论 0 0