Reputation: 15042
In an activity can bind and access myView
which is a subview in the layout like this:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ActivityExampleBinding views = DataBindingUtil.setContentView(this, R.layout.activity_example);
views.myView.doSomething();
}
How can I do that in a Fragment? I can return the inflated root view like this:
@Override
public @Nullable View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
ViewDataBinding views = DataBindingUtil.inflate(inflater, R.layout.fragment_example, container,false);
return views.getRoot();
}
But how can I access a subview in the layout?
Upvotes: 2
Views: 368
Reputation: 1006869
Your current code is fine, if you want to stick with it — just change the view
variable to be a FragmentExampleBinding
(matching your fragment_example
layout resource name).
Alternatively, use FragmentExampleBinding.inflate()
, as you do not need to pass in the layout resource ID.
Upvotes: 1