Binding fragments in listview - android

I have a MvvmCross Android application and let's say it contains some Fragment and ViewModel. I set the ViewModel for the Fragment inside OnCreate call of the Activity which hosts the fragment as it is shown at N+1 Fragments Sample All bindings inside the fragment work fine.
I also have another Activity with a view that contains MvxListView. The row template of the listview contains the fragment I described above. How can I set the ViewModel of the fragment so that bindings work correctly?

We have found that you can't get hold of the MvxListView in the OnCreateView method to bind to the ViewModel.
We have managed to get it work work if you use OnActivityCreated method and bind your ViewModel to the View there.

Related

Android Independent viewModel for recyclerView's adapter

We have a problem in mvvm design pattern with recyclerView:
We have a fragment with own view model (FragmentViewModel)
and a list adapter with it's own view model (ListViewModel)
the problems are these:
Inside our fragment's view model(FragmentViewModel), we get our data from api repository,How to set these values to our adapter's view model(ListViewModel)?
Inside of adapter's view model(ListViewModel),when row item clicked, How to call a function inside our fragments's view model(FragmentViewModel) to do something with fragment's view?
Notes:
- We use dagger to inject our viewModels and adapter.
- Our list adapter will reuse with other fragments.
My suggestion would be, /n
Never use Fragment own's ViewModel, instead used activity one with a key. which differentiate between different fragment.
ViewModelProviders.of(requireActivity(), factory).get(detail.getJobId(), TimelineActivityViewModel.class);
here detail.getJobId() is an ID.

Fragments without onCreateView

recently i saw some articles about inflating the fragment layout directly from the Fragment()
class ExampleFragment: Fragment(R.layout.example_fragment)
does this stand only for fragments that dont hold any data and interaction, when should we use this method
This was added in androidx.fragment version 1.1.0:
Fragment LayoutId constructor: Subclasses of Fragment can now optionally call into a constructor on Fragment that takes an R.layout ID, indicating the layout that should be used for this fragment as an alternative to overriding onCreateView(). The inflated layout can be configured in onViewCreated().
So in essence it's a shorthand for overriding onCreateView() that just inflates a layout and returns it, and encourages a style of configuring the view in onViewCreated() rather than onCreateView().

How can I call a view that is in main activity from fragment?

I have a fragment that has a main activity with several views. One of them is a loading that I wanted to recycle between fragments. Is there a way to call a view in a fragment from main activity using kotlin?
In your fragment, use activity!!.findViewById<View>(R.id.your_view_id) or better still,simply use activity!!.your_view_id then you can call any operation on the view.
Note: replace View with your type of view e.g TextView

GlobalLayoutLIstener behavior for Fragment?

I am using GlobalLayoutLIstener in an fragment that is associate with an Activity, and this activity has other associative fragment as well, what I am observing is global layout listener calling for every fragment, even I didn't set it in those fragment.
any one have idea how to use GlobalLayoutLIstener using single context in different-2 fragment?

Fragment basics

I am working on a application for which I have some issues with fragments. First of all , I want to know if it is possible to make one fragment inside another fragment. And the second one is when to call onCreateView() and onActivityCreated() and which is best?
Per the Android sources:
onCreateView is called by the Activity during the construction of the view hierarchy. This is where the Fragment has the opportunity to instantiate its own user interface view.
onActivityCreated is called when the Activity has been fully created and finished instantiating the view hierarchy. At this point it's safe for the Fragment to access the its views and restore itself from some saved state.
You cannot create a fragment inside another fragment. You should communicate between fragments thru activity.See http://developer.android.com/guide/components/fragments.html#CommunicatingWithActivity
I didn't understand your second question clearly. But I generally leave onActivityCreated() blank without changing it. And use onCreateView() in a similar way with onCreate().

Categories

Resources