我正在尝试从官方谷歌文档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); }
我的片段布局文件的部分内容如下所示:
...
我怀疑是MartianDataBinding
不知道它应该绑定哪个布局文件 - 因此错误.有什么建议?
数据绑定实现必须在片段的onCreateView方法中,删除OnCreate方法中存在的任何数据Binding,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
生成的Binding方法,而不是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事先未知时才使用此版本.否则,使用生成的Binding的膨胀方法来确保类型安全的膨胀.
即使其他答案也可能运作良好,但我想告诉最佳方法.
Binding class's inflate
按照Android文档中的建议使用.一种选择是通过膨胀,DataBindingUtil
但只有你不知道生成了绑定类.
- 你有自动生成binding class
,使用该类而不是使用DataBindingUtil
.
在Java中
@Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { HomeFragmentBinding binding = HomeFragmentBinding.inflate(inflater, container, false); //set binding variables here return binding.getRoot(); }
在科特林
lateinit var binding: HomeFragmentBinding override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?, savedInstanceState: Bundle?): View? { binding = HomeFragmentBinding.inflate(inflater, container, false) return binding.root }
在DataBindingUtil类文档中,您可以看到.
膨胀
T inflate (LayoutInflater inflater, int layoutId, ViewGroup parent, boolean attachToParent)仅当layoutId事先未知时才使用此版本.否则,使用生成的Binding的膨胀方法来确保类型安全的膨胀.
如果未生成布局biniding类,请参阅此答案.
如果您使用的是ViewModel和LiveData,这是足够的语法
Kotlin语法:
override fun onCreateView( inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle? ): View? { return MartianDataBinding.inflate( inflater, container, false ).apply { setLifecycleOwner(this@MartianData) vm = viewModel // Attach your view model here }.root }
在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; }
可以简单地检索视图对象,如下所述
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(); }
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 }