1.
onCreateView()
is for creating the View hierarchy(等级) that will eventually contain the Preference UI.
onBindView()
is for binding actual data to that View hierarchy created in onCreateView()
.
The pattern separates the creation of the View hierarchy - which is cached - from the binding of data to that View hierarchy.
In the case of Preference, onCreateView()
is only called once, but onBindView()
is called every time the UI needs to load the Preference View.
转载自:http://en.efreedom.net/Question/1-7346890/Difference-Preferences-OnCreateView-OnBindView-Methods
2. 举例 自定义preference 有时想让其UI更丰富更好看,这里我们可以通过引用一个layout 文件为其指定UI,可以通过实现如下两个回调函数:
protected View onCreateView(ViewGroup parent) {
// TODO Auto-generated method stub
return LayoutInflater.from(getContext()).inflate(
R.layout.preference_screen, parent, false );
}
此回调函数与onBindView 一一对应,并优先执行于onBindView ,当创建完后将得到的VIEW返回出去给onBindView处理,如下代码:
protected void onBindView(View view) {
// TODO Auto-generated method stub
super.onBindView(view);
canlendar = Calendar.getInstance();
layout = (RelativeLayout) view.findViewById(R.id.area);
title = (TextView) view.findViewById(R.id.title);
summary = (TextView) view.findViewById(R.id.summary);
layout.setOnClickListener( this );
title.setText(getTitle());
summary.setText(getPersistedString(canlendar. get (Calendar.YEAR) + " / "
+ (canlendar. get (Calendar.MONTH) + 1 ) + " / "
+ canlendar. get (Calendar.DAY_OF_MONTH)));
}