247

我正在尝试遵循官方谷歌文档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不知道它应该绑定哪个布局文件 - 因此出现错误。有什么建议么?

4

16 回答 16

441

数据绑定实现必须在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;
}
于 2016-01-11T10:50:23.763 回答
86

实际上鼓励您使用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 的 inflate 方法来确保类型安全的膨胀。

于 2016-11-10T12:37:26.583 回答
33

即使其他答案也可能效果很好,但我想告诉最好的方法。

按照Android 文档Binding class's inflate中的建议使用。

一种选择是膨胀,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 的 inflate 方法来确保类型安全的膨胀。

如果您的布局绑定类未生成@See this answer

于 2018-08-01T12:44:52.893 回答
27

如果您使用ViewModelLiveData这就是足够的语法

Kotlin 语法:

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
}
于 2018-12-26T13:17:50.030 回答
11

正如大多数人所说,但不要忘记在 Java 中设置LifeCycleOwner
Sample,

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();
}
于 2019-07-04T17:36:59.293 回答
10

在 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;
}
于 2016-07-01T23:55:15.307 回答
10

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
}
于 2017-09-26T12:56:25.473 回答
7

可以简单地检索视图对象,如下所述

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;

}
于 2016-06-09T11:10:35.767 回答
6

数据绑定 Fragments 中的完整示例

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

    }
}
于 2018-09-19T12:39:53.913 回答
5

在我的代码中工作。

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();
}
于 2016-09-22T09:37:12.603 回答
3

我一直在为我的应用程序寻找答案,这里是 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
}
于 2020-07-30T10:58:02.077 回答
3

在 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。

于 2021-09-08T18:11:39.057 回答
2

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”取决于布局文件的名称。如果文件名为“martian_data”,那么正确的名称应该是 MartianDataBinding。

于 2017-12-31T14:17:00.990 回答
2

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

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 中像这样声明绑定 val:

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

不要忘记在片段中写这个

override fun onCreateView(
    inflater: LayoutInflater,
    container: ViewGroup?,
    savedInstanceState: Bundle?
): View? {
    return binding.root
}
于 2019-11-22T07:41:14.713 回答
1

每个人都说inflate(),但是如果我们要使用它onViewCreated()呢?

您可以使用bind(view)具体绑定类的方法来ViewDataBinding获取view.


通常我们会这样写 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)
}


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

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

我不知道在那里断言非空是可以的,但是......你明白了。如果你希望它可以为空,你可以这样做。

于 2019-05-15T08:46:38.403 回答
0

科特林

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

}

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

首先,您需要将此行添加到您的 build.Gradle(app module) 文件中。

buildFeatures{
        viewBinding true
    }
于 2022-01-28T13:13:42.670 回答