如何使用Fragment的数据绑定

我试图从官方的谷歌文档https://developer.android.com/tools/data-binding/guide.html数据绑定的例子

除了我试图将数据绑定应用于片段而不是活动。

我正在编译的错误

Error:(37, 27) No resource type specified (at 'text' with value '@{marsdata.martianSols}.

onCreate的片段如下所示:

 @Override public void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); MartianDataBinding binding = MartianDataBinding.inflate(getActivity().getLayoutInflater()); binding.setMarsdata(this); } 

onCreateView的片段看起来像这样:

 @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { return inflater.inflate(R.layout.martian_data, container, false); } 

部分我的布局文件的片段如下所示:

 <?xml version="1.0" encoding="utf-8"?> <layout xmlns:android="http://schemas.android.com/apk/res/android"> <data> <variable name="marsdata" type="uk.co.darkruby.app.myapp.MarsDataProvider" /> </data> ... <TextView android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="@{marsdata.martianSols}" /> </RelativeLayout> </layout> 

我的怀疑是, MartianDataBinding不知道它应该绑定哪个布局文件 – 因此错误。 有什么build议么?

数据绑定实现必须在片段的onCreateView方法中,删除OnCreate方法中存在的任何数据绑定,你的onCreateView应该看起来像这样:

 public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { MartianDataBinding binding = DataBindingUtil.inflate( inflater, R.layout.martian_data, container, false); View view = binding.getRoot(); //here data must be an instance of the class MarsDataProvider binding.setMarsdata(data); return view; } 

实际上鼓励您使用您生成的绑定的inflate方法而不是DataBindingUtil:

 @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { MainFragmentBinding binding = MainFragmentBinding.inflate(inflater, container, false); //set variables in Binding return binding.getRoot(); } 

DataBindingUtil.inflate()的文档 :

仅当layoutId未知时才使用此版本。 否则,使用生成的绑定的膨胀方法来确保types安全的膨胀。

可以简单地检索下面提到的视图对象

 public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View view = DataBindingUtil.inflate(inflater, R.layout.layout_file, container, false).getRoot(); return view; } 

在我的代码工作。

 private FragmentSampleBinding dataBiding; private SampleListAdapter mAdapter; @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { super.onCreateView(inflater, container, savedInstanceState); dataBiding = DataBindingUtil.inflate(inflater, R.layout.fragment_sample, null, false); return mView = dataBiding.getRoot(); } 

在Android DataBinding中试试这个

 FragmentMainBinding binding; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { binding = DataBindingUtil.inflate(inflater, R.layout.fragment_main, container, false); View rootView = binding.getRoot(); initInstances(savedInstanceState); return rootView; } 

Kotlin语法:

 lateinit var binding: MartianDataBinding override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?, savedInstanceState: Bundle?): View? { binding = DataBindingUtil.inflate(inflater, R.layout.martian_data, container, false) return binding.root }