Reputation: 827
Using a PagerSlidingTabStrip with a Viewpager inside of a fragment:
Any ideas?
Upvotes: 6
Views: 4258
Reputation: 2710
If you have multiple fragments under your main fragment A and move from Fragment B to Fragment A and this doesnt show the UI its only because you have been using getSupportFragmentManager()
in your code, which was updating the parent fragment.
Instead of getSupportFragmentManager()
, use: getChildFragmentManager()
in your code.
Upvotes: 0
Reputation: 89
Sorry to reply on an old post, but writing this because non of Stackoverflow solutions for my particular problem helped me.
If you are using new architecture components view model with a master detail shared view model and after returning from detail fragment get blank view pager, do the view model initialization in onViewCreated
method of master fragment and not in onCreate
(only needed in master fragment).
Also as other answers say remember to use childFragmentManager in view pager adapter.
like this:
class SharedViewModel : ViewModel() {
val selected = MutableLiveData<Item>()
fun select(item: Item) {
selected.value = item
}
}
class MasterFragment : Fragment() {
private lateinit var itemSelector: Selector
private lateinit var model: SharedViewModel
// In the master fragment do the view model initialization in onViewCreated
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
model = activity?.run {
ViewModelProviders.of(this).get(SharedViewModel::class.java)
} ?: throw Exception("Invalid Activity")
model.selected.observe(this, Observer<Item> { item ->
// Update the UI
})
}
}
class DetailFragment : Fragment() {
private lateinit var model: SharedViewModel
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
model = activity?.run {
ViewModelProviders.of(this).get(SharedViewModel::class.java)
} ?: throw Exception("Invalid Activity")
model.selected.observe(this, Observer<Item> { item ->
// Update the UI
})
}
}
Upvotes: 0
Reputation: 11555
Replacing getFragmentManager()
with getChildFragmentManager()
helped me.
Upvotes: 1
Reputation: 753
I had a problem like that
try this
mPager.setAdapter(new BasePagerAdapter(getChildFragmentManager(), getResources()));
you probably have this
mPager.setAdapter(new BasePagerAdapter(getFragmentManager(), getResources()));
EDIT: and in your BasePagerAdapter extend FragmentStatePagerAdapter
public class BasePagerAdapter extends FragmentStatePagerAdapter {
Upvotes: 21
Reputation: 711
Write ur code ie you are using for setting up your pager adapter inside
@Override
public void onResume() {
// TODO Auto-generated method stub
super.onResume();
pager.setAdapter(adapter);
}
Upvotes: 1