為什么要造輪子
自己平常寫(xiě)代碼一直用ButterKnife洒闸,功能太強(qiáng)大了有木有染坯!但是最近有個(gè)需要在匿名內(nèi)部類(lèi)中去findView的時(shí)候卻發(fā)現(xiàn)ButterKnife好像不支持在匿名內(nèi)部類(lèi)中進(jìn)行find,(我也不確定支不支持丘逸,反正沒(méi)找到相應(yīng)的方法)編譯后沒(méi)有生成相應(yīng)的ViewBinder類(lèi)单鹿,應(yīng)該是編譯時(shí)故意忽略了匿名內(nèi)部類(lèi)吧。所以此時(shí)也很無(wú)奈啊深纲。仲锄。。只能自己動(dòng)手豐衣足食了湃鹊。
內(nèi)容為原創(chuàng)儒喊,希望能給需要的人幫助,不喜勿噴
一涛舍、思路
將需要查找的View包裝成WrapView對(duì)象放到查找隊(duì)列中澄惊,指定View的類(lèi)型和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);
}
}
}