Preference中 onCreateView and onBindView methods的区别

本文详细解析了onCreateView与onBindView方法的区别及应用。onCreateView用于创建包含偏好设置UI的视图层级,而onBindView则用于将实际数据绑定到该视图层级。在自定义偏好设置时,通过这两个方法可以实现更加丰富的UI效果。

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,可以通过实现如下两个回调函数:

@Override
     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处理,如下代码:

 

@Override
     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)));

    }
转载自:http://quanminchaoren.iteye.com/blog/867605
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值