databinding view is not a binding layout root 以及不顯示的坑

好吧其實(shí)不是來(lái)講這個(gè)的,重點(diǎn)是說(shuō)可能換一種寫法解決,還是把過(guò)程寫下來(lái)吧.
通過(guò)幾個(gè)控件組合了一個(gè)自定義view,繼承自RelativeLayout,就叫IdCardView吧想用databinding來(lái)簡(jiǎn)化操作,去年怎么寫怎么爽,今天忽然就發(fā)現(xiàn)了好多坑:
先看怎么初始化的:

  public IDCardView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView();
    }
    private void initView() {
        LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context
                .LAYOUT_INFLATER_SERVICE);
//注意這的container是自己,是否綁定到container上設(shè)置為false
        binding = DataBindingUtil.inflate(inflater, R.layout.view_idcard, this, false);
    }

然后很happy的開始跑了起來(lái),發(fā)現(xiàn)WTF怎么不顯示,記得去年就是這樣寫的呀,哎當(dāng)時(shí)真的是不求甚解呀...而且記得跟著斷點(diǎn)走了一邊,最后DataBindingUtils調(diào)用了一個(gè)factory.inflate(....) 這個(gè)factory都是context湖片區(qū)的layoutinflator,應(yīng)該是沒(méi)問(wèn)題的,但是這是什么情況呢,算了試試直接用RelativeLayout的inflate方法寫一下試試,于是initView()就這樣寫:


    private void initView() {
        LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context
                .LAYOUT_INFLATER_SERVICE);
        View root = inflate(getContext(), R.layout.view_idcard, null);

        binding = DataBindingUtil.bind(root);
       // binding = DataBindingUtil.getBinding(root);
       // binding = DataBindingUtil.findBinding(root);
    }

然后就得到了標(biāo)題提到的view is not a binding layout root
后邊注釋掉的是后邊的嘗試,其中findBinding(root)的含義是

Retrieves the binding responsible for the given View. If view is not a binding layout root, its parents will be searched for the binding. If there is no binding, null will be returned.

意思是說(shuō),先從root中找綁定的東東,如果root不是個(gè)layout包裹的view,那就讓他的父控件找,找不到就返回null;

然后getBinding(root)的意思是

Retrieves the binding responsible for the given View layout root. If there is no binding, null
 will be returned. This uses the DataBindingComponent set in[setDefaultComponent(DataBindingComponent)](https://developer.android.com/reference/android/databinding/DataBindingUtil.html#setDefaultComponent(android.databinding.DataBindingComponent))
.

直接看這個(gè)view 是不是layout包裹的view,不是直接返回null

但是這個(gè)layout確實(shí)是layout標(biāo)簽包裹的,為什么么各種綁定獲得的都不是一個(gè)binding呢,這是一個(gè)問(wèn)題...我也不知道為什么...build后能夠獲取binding說(shuō)明layout文件沒(méi)有錯(cuò),也就是說(shuō)inflate的過(guò)程不會(huì)錯(cuò),那就是參數(shù)寫錯(cuò)了,于是往回看了一下最初寫的

 binding = DataBindingUtil.inflate(inflater, R.layout.view_idcard, this, false);

第四個(gè)參數(shù)是不將綁定到container上,因?yàn)閷慳dapter什么的不把inflate到的view填充到container上習(xí)慣了就直接寫了,但這里的container是自己,如果不綁上去那返回的不就是空了嗎,于是還按照最初的寫法,將最后一個(gè)參數(shù)false改為true,就能夠顯示了,也就不需要bind,getBinding,findbinding了,也算曲線解決了view is not a binding layout root的坑,
最終初始化代碼為:

 private void initView() {
        LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context
                .LAYOUT_INFLATER_SERVICE);
        DataBindingUtil.inflate(inflater, R.layout.view_idcard, this, false);
    }
最后編輯于
?著作權(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)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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

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