How to use data-binding with Fragment

后端 未结 14 1028
不思量自难忘°
不思量自难忘° 2020-11-29 16:48

I\'m trying to follow data-binding example from official google doc https://developer.android.com/tools/data-binding/guide.html

except that I\'m trying to apply data

相关标签:
14条回答
  • 2020-11-29 17:09

    Everyone says about inflate(), but what if we want to use it in onViewCreated()?

    You can use bind(view) method of concrete binding class to get ViewDataBinding instance for the view.


    Usually we write BaseFragment something like this (simplified):

    // BaseFragment.kt
    abstract fun layoutId(): Int
    
    override fun onCreateView(inflater, container, savedInstanceState) = 
        inflater.inflate(layoutId(), container, false)
    

    And use it in child fragment.

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


    If all Fragments uses data binding, you can even make it simpler using type parameter.

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

    I don't know it's okay to assert non-null there, but.. you get the idea. If you want it to be nullable, you can do it.

    0 讨论(0)
  • 2020-11-29 17:12

    working in my code.

    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();
    }
    
    0 讨论(0)
  • 2020-11-29 17:13

    Try this in 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;
    }
    
    0 讨论(0)
  • 2020-11-29 17:14

    Another example in 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
    }
    

    Note that the name "MartianDataBinding" depends on the name of the layout file. If the file is named "martian_data" then the correct name would be MartianDataBinding.

    0 讨论(0)
  • 2020-11-29 17:16

    The data binding implementation must be in the onCreateView method of the fragment, delete any data Binding that exist in your OnCreate method, your onCreateView should look like this:

    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;
    }
    
    0 讨论(0)
  • 2020-11-29 17:18

    Kotlin syntax:

    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
    }
    
    0 讨论(0)
提交回复
热议问题