Getting the current Fragment instance in the viewpager
by selecting an option, I need to update the fragment that is
currently visible.
A simple way of doing this is using a trick related to the FragmentPagerAdapter
implementation:
case R.id.addText:
Fragment page = getSupportFragmentManager().findFragmentByTag("android:switcher:" + R.id.pager + ":" + ViewPager.getCurrentItem());
// based on the current position you can then cast the page to the correct
// class and call the method:
if (ViewPager.getCurrentItem() == 0 && page != null) {
((FragmentClass1)page).updateList("new item");
}
return true;
Please rethink your variable naming convention, using as the variable name the name of the class is very confusing(so no ViewPager ViewPager
, use ViewPager mPager
for example).
How to get current Fragment name in viewpager?
Inside CustomTabLayout
add
tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener()
{
@Override
public void onTabSelected(TabLayout.Tab tab)
{
int position = tab.getPosition();
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
How to get Current Fragment in ViewPager2
- Create property in your adapter to save all your fragments
- Save current position
- Get fragment from fragment list (property) by position when you need it
Something like this:
val fragments: MutableList<Fragment> = mutableListOf()
private val config: TribunConfig?
override fun createFragment(position: Int): Fragment {
val fragment = TribunNewsFragment.newInstance(if (menuTribun!!.get(position) == "News") "home"
else menuTribun!!.get(position)!!.replace(" ", "").toLowerCase(), config)!!
fragments.add(fragment)
return fragment
}
Parent:
yourTabLayout.addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener {
override fun onTabSelected(tab: TabLayout.Tab?) {
val fragment = adapter.fragments[tab!!.position]
// cast fragment to your fragment class and do what you want
}
override fun onTabUnselected(tab: TabLayout.Tab?) {
}
override fun onTabReselected(tab: TabLayout.Tab?) {
}
})
Also you can use when
or if-else
for your fragments position or create base class with methods you need and cast getting fragment to it.
How to get the current fragment displayed in a specific tab of a viewpager?
First define a SparseArray
in your ViewPagers
' adapters like below. In this array we'll hold the instance of fragments.
SparseArray<Fragment> registeredFragments = new SparseArray<>();
And Override your Adapters' instantiateItem
method.
@Override
public Object instantiateItem(ViewGroup container, int position) {
Fragment fragment = (Fragment) super.instantiateItem(container, position);
registeredFragments.put(position, fragment);
return fragment;
}
Also Override destroyItem
method of your ViewPagers
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
registeredFragments.remove(position);
super.destroyItem(container, position, object);
}
And define a new method to get your ViewPager
Fragments
instance.
public Fragment getRegisteredFragment(int position) {
return registeredFragments.get(position);
}
And finally set add a PageChangeListener
to your ViewPagers
:
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
// Here's your instance
YourFragment fragment =(YourFragment)yourPagerAdapter.getRegisteredFragment(position);
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
I hope this'll help you. Good luck.
Edit: I'm sorry i cannot understand exactly what you're planning to do but if you need to keep sub fragment (b_1, b_2) instance you can define a method to your activity such as
public void setCurrentFragment(Fragment fragment){
this.currentFragment = fragment;
}
and in your sub view pager's adapter you can call this method like below:
subViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
// Here's your instance
YourFragment fragment =(YourFragment)yourSubPagerAdapter.getRegisteredFragment(position);
((MyActivity)getActivity).setCurrentFragment(fragment);
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
With this way you can keep one instance and your top fragment.
Is it possible to access the current Fragment being viewed by a ViewPager?
I finally found an answer that worked for me. Basically, you can access the fragment for a viewPager page by using the tag "android:switcher:"+R.id.viewpager+":0".
How to get fragment reference from ViewPager
Use viewPager.getAdapter.getItem(position);
from your Activity
or parent Fragment
.
Description (Modified) - As you wrote addFragment()
adds a fragment to mFragmentList
and you are adding fragment by this method too by calling:
mSectionsPageAdapter.addFragment(new MyChatsFragment())
And getItem()
returns a fragment from mFragmentList
where you added your fragment. So if you want to get MyChatsFragment
reference just use:
MyChatsFragment myChatsFragment = (MyChatsFragment)
mSectionsPageAdapter .getItem(position);
or
MyChatsFragment myChatsFragment = (MyChatsFragment)
viewPager.getAdapter.getItem(position);
Get current fragment in viewpager2 in android
You can use HashMap to do this:
private HashMap<Integer, Fragment> hashMap = new HashMap<>();
@Override
public Fragment createFragment(int position) {
Fragment fragment = ScreenFragment.newInstance(position);
hashMap.put(position, fragment);
return fragment;
}
And then get current fragment at this way:
ScreenFragment screenFragment = (ScreenFragment) hashMap.get(viewPager.getCurrentItem());
Getting the current Fragment instance in the viewpager
by selecting an option, I need to update the fragment that is
currently visible.
A simple way of doing this is using a trick related to the FragmentPagerAdapter
implementation:
case R.id.addText:
Fragment page = getSupportFragmentManager().findFragmentByTag("android:switcher:" + R.id.pager + ":" + ViewPager.getCurrentItem());
// based on the current position you can then cast the page to the correct
// class and call the method:
if (ViewPager.getCurrentItem() == 0 && page != null) {
((FragmentClass1)page).updateList("new item");
}
return true;
Please rethink your variable naming convention, using as the variable name the name of the class is very confusing(so no ViewPager ViewPager
, use ViewPager mPager
for example).
Related Topics
How to Stop Activity Resizing/Pushing Up on Keyboard Open in Dialog
How to Get the Value of a Listview Item Which Is Clicked in Android
How to Resize the Avd Emulator Window in Android Studio
String Resource New Line /N Not Possible
How to Reduce an Image File Size Before Uploading to a Server
How to Know Location Area Code and Cell Id in Android Phone
Clear Cache in Android Application Programmatically
How to Close All Activities and Exit from an App
How to Convert Android.Media.Image to Bitmap Object
Android Push Notifications Not Being Received When App Closed
Remove Old and Add New Git in Android Studio
How to Remove the Black Bar Appearing At the Bottom
Socket.Io Client Getting Connected Disconnected Repeatedly
How to Emulate Gps Location in the Android Emulator
Image Scale Type Center Crop on Flutter