如何使用Fragment进行数据绑定

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

除了我试图将数据投标应用到一个片段,而不是一个活动。

编译时我目前得到的错误是

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

onCreate for fragment看起来像这样:

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

onCreateView for fragment看起来像这样:

@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不知道它应该与哪个布局文件绑定——因此出现错误。有什么建议吗?

210133 次浏览
数据绑定实现必须在片段的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;
}

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

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;


}

在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;
}

在我的代码中工作。

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();
}

你实际上被鼓励使用你生成的Binding的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();
}

(MainFragmentBinding为r.b ayout.main_fragment自动生成)

databindingutil . inflation()文档:

仅在layoutId预先未知的情况下使用此版本。否则,使用生成的Binding的膨胀方法来确保类型安全的膨胀。

芬兰湾的科特林语法:

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
}

Kotlin中的另一个例子:

override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val binding = DataBindingUtil
.inflate< MartianDataBinding >(
inflater,
R.layout.bla,
container,
false
)


binding.modelName = // ..


return binding.root
}

注意,“MartianDataBinding”的名称取决于布局文件的名称。如果文件命名为“marti_data”,那么正确的名称应该是MartianDataBinding。

一个完整的数据绑定片段的例子

FragmentMyProgramsBinding是为res/layout/fragment_my_programs生成的绑定类

public class MyPrograms extends Fragment {
FragmentMyProgramsBinding fragmentMyProgramsBinding;


public MyPrograms() {
// Required empty public constructor
}




@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
FragmentMyProgramsBinding    fragmentMyProgramsBinding = DataBindingUtil.inflate(inflater, R
.layout.fragment_my_programs, container, false);
return fragmentMyProgramsBinding.getRoot();
}


@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);


}
}

如果你正在使用视图模型LiveData,这是足够的语法

芬兰湾的科特林语法:

override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
return MartianDataBinding.inflate(
inflater,
container,
false
).apply {
lifecycleOwner = viewLifecycleOwner
vm = viewModel    // Attach your view model here
}.root
}

每个人都在谈论inflate(),但是如果我们想在onViewCreated()中使用它呢?

你可以使用具体绑定类的bind(view)方法来获取view. xml对象的ViewDataBinding实例。

< br >

通常我们这样写BaseFragment(简化):

// BaseFragment.kt
abstract fun layoutId(): Int


override fun onCreateView(inflater, container, savedInstanceState) =
inflater.inflate(layoutId(), container, false)

并在子片段中使用它。

// ConcreteFragment.kt
override fun layoutId() = R.layout.fragment_concrete


override fun onViewCreated(view, savedInstanceState) {
val binding = FragmentConcreteBinding.bind(view)
// or
val binding = DataBindingUtil.bind<FragmentConcreteBinding>(view)
}

< br >

如果所有fragment都使用数据绑定,您甚至可以使用类型参数使其更简单。

abstract class BaseFragment<B: ViewDataBinding> : Fragment() {
abstract fun onViewCreated(binding: B, savedInstanceState: Bundle?)


override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
onViewCreated(DataBindingUtil.bind<B>(view)!!, savedInstanceState)
}
}

我不知道是否可以在这里断言非空,但是。你懂的。如果你想让它为空,你可以这样做。

就像大多数人说的,但是不要忘记设置LifeCycleOwner
Java示例 即< / p >
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
BindingClass binding = DataBindingUtil.inflate(inflater, R.layout.fragment_layout, container, false);
ModelClass model = ViewModelProviders.of(getActivity()).get(ViewModelClass.class);
binding.setLifecycleOwner(getActivity());
binding.setViewmodelclass(model);


//Your codes here


return binding.getRoot();
}

非常有用的博客关于数据绑定: https://link.medium.com/HQY2VizKO1 < / p >

class FragmentBinding<out T : ViewDataBinding>(
@LayoutRes private val resId: Int
) : ReadOnlyProperty<Fragment, T> {


private var binding: T? = null


override operator fun getValue(
thisRef: Fragment,
property: KProperty<*>
): T = binding ?: createBinding(thisRef).also { binding = it }


private fun createBinding(
activity: Fragment
): T = DataBindingUtil.inflate(LayoutInflater.from(activity.context),resId,null,true)
}

在Fragment中像这样声明binding val:

private val binding by FragmentBinding<FragmentLoginBinding>(R.layout.fragment_login)

别忘了把这段话分段写下来

override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
return binding.root
}

我一直在为我的应用程序寻找答案,这里是Kotlin语言的答案。


private lateinit var binding: FragmentForgetPasswordBinding


override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?): View? {
binding=DataBindingUtil.inflate(inflater,R.layout.fragment_forget_password,container,false)
    

val viewModel=ViewModelProvider(this).get(ForgetPasswordViewModel::class.java)
binding.recoveryViewModel=viewModel
viewModel.forgetPasswordInterface=this
return binding.root
}

这是你在kotlin中可以做到的:

//Pass the layout as parameter to the fragment constructor
class SecondFragment : Fragment(R.layout.fragment_second) {


private var _binding: FragmentSecondBinding? = null
private val binding  get() = _binding!!


override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
_binding = FragmentSecondBinding.bind(view)  //if the view is already inflated then we can just bind it to view binding.


}


//Note: Fragments outlive their views. Make sure you clean up any references to the binding class
// instance in the fragment's onDestroyView() method.
override fun onDestroyView() {
Toast.makeText(activity, "On destroy", Toast.LENGTH_SHORT).show()
super.onDestroyView()
_binding = null
}
}

你可以像这样从你的布局中访问视图元素:

binding.tvName.text = "Messi"

其中tvName是视图元素的id。

芬兰湾的科特林

         override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View?
{
val binding = FragmentFirstBinding.inflate(inflater,container,false)
return  binding.root;
            



}

其中FragmentFirstBinding是android studio使用视图绑定自动生成的。在我的代码片段名称是FirstFragment

首先,您需要将这一行添加到构建中。Gradle(app模块)文件。

buildFeatures{
viewBinding true
}

Kotlin中的最短路径;

class HomeFragment : Fragment(R.layout.fragment_home) {




override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
val binding = FragmentHomeBinding.bind(view)
// todo
}