ButterKnife 的簡(jiǎn)易替代品,不為findViewById折腰

為什么要造輪子

自己平常寫(xiě)代碼一直用ButterKnife,功能太強(qiáng)大了有木有!但是最近有個(gè)需要在匿名內(nèi)部類中去findView的時(shí)候卻發(fā)現(xiàn)ButterKnife好像不支持在匿名內(nèi)部類中進(jìn)行find,(我也不確定支不支持,反正沒(méi)找到相應(yīng)的方法)編譯后沒(méi)有生成相應(yīng)的ViewBinder類,應(yīng)該是編譯時(shí)故意忽略了匿名內(nèi)部類吧。所以此時(shí)也很無(wú)奈啊。。。只能自己動(dòng)手豐衣足食了。

內(nèi)容為原創(chuàng),希望能給需要的人幫助,不喜勿噴

一、思路

將需要查找的View包裝成WrapView對(duì)象放到查找隊(duì)列中,指定View的類型和id,在容器(activity,fragment等)初始化完View以后再調(diào)用ViewFinder的findFrom方法進(jìn)行查找。
需要使用View時(shí)調(diào)用WrapView的get()方法將View取出。

使用起來(lái)確實(shí)稍微有點(diǎn)麻煩。。。不過(guò)作為強(qiáng)迫癥患者,這樣的代碼↓↓↓能把我逼瘋

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("男");     
        ...
    }
    ...

}

經(jīng)過(guò)改進(jìn)后的代碼是這樣的


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);
        }
    }
}

代碼量很少,需要的童鞋可以直接拿去用,祝大家新年快樂(lè)!

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢(shì)1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 179,045評(píng)論 25 709
  • 0X0 前言 做過(guò)Android開(kāi)發(fā)的猿類很多都知道ButterKnife這么個(gè)東西。這個(gè)庫(kù)可以大大的簡(jiǎn)化我們的代...
    knightingal閱讀 792評(píng)論 1 10
  • 俗話說(shuō)的好“不想偷懶的程序員,不是好程序員”,我們?cè)谌粘i_(kāi)發(fā)android的過(guò)程中,在前端activity或者fr...
    蛋西閱讀 5,072評(píng)論 0 14
  • 天云被光映黃金,亮清奇峰見(jiàn)群松。 游客眺望江山喜,照留美景日后戀。
    海南黑哥閱讀 227評(píng)論 0 2
  • 當(dāng)今時(shí)代“夢(mèng)想”這個(gè)詞的使用頻率很高,近日讀到兩位作家關(guān)于兩部作品的評(píng)論,而這兩部作品恰好都與夢(mèng)想有關(guān),她們的評(píng)論...
    茫然的蒲公英閱讀 1,040評(píng)論 0 0

友情鏈接更多精彩內(nèi)容