How To Get Which Fragment Has Been Selected
I am creating a tab application, using fragments. I have successfully created tabs, but now I need to create an object when a specific fragment is selected. e.g. When I select Sett
Solution 1:
In your ActionBar.TabListener
you can do this - (I'm assuming that you need the object to be in the Activity
)
classMyTabsListenerimplementsActionBar.TabListener {
publicFragment fragment;
publicMyTabsListener(Fragment fragment) {
this.fragment = fragment;
}
publicvoidonTabReselected(Tab tab, FragmentTransaction ft) {
Toast.makeText(MainActivity.appContext, "Reselected!", Toast.LENGTH_LONG).show();
}
publicvoidonTabSelected(Tab tab, FragmentTransaction ft) {
ft.replace(R.id.fragment_container, fragment);
if(fragment instanceofSettingsTab) {
// Create your object, call your function
}
}
publicvoidonTabUnselected(Tab tab, FragmentTransaction ft) {
ft.remove(fragment);
if(fragment instanceofSettingsTab) {
// Destroy your object
}
}
}
Solution 2:
Try this
public void onTabSelected(Tab tab, FragmentTransaction ft) {
ft.replace(R.id.fragment_container, fragment);
if(fragment instanceof SettingsTab){
doSettingsMethod();
}
if(fragment instanceof Tab1){
doOtherMethod1();
}
if(fragment instanceof Tab2){
doOtherMethod2();
}
if(fragment instanceof Tab3){
doOtherMethod3();
}
}
Also you may change TabListener class in this way. As for me it's a better design.
classMyTabsListenerimplementsActionBar.TabListener {
public Fragment fragment;
privatefinal Runnable onSelect;
publicMyTabsListener(Fragment fragment) {
this.fragment = fragment;
this.onSelect = null;
}
publicMyTabsListener(Fragment fragment, Runnable onSelect) {
this.fragment = fragment;
this.onSelect = onSelect;
}
publicvoidonTabReselected(Tab tab, FragmentTransaction ft) {
Toast.makeText(MainActivity.appContext, "Reselected!", Toast.LENGTH_LONG).show();
}
publicvoidonTabSelected(Tab tab, FragmentTransaction ft) {
ft.replace(R.id.fragment_container, fragment);
if(onSelect != null){
fragment.getActivity().runOnUiThread(onSelect);
}
}
publicvoidonTabUnselected(Tab tab, FragmentTransaction ft) {
ft.remove(fragment);
}
}
Solution 3:
The only official source I found to this was on the developer site: http://developer.android.com/training/implementing-navigation/lateral.html
They simply put arguments for each created fragment, and read the arguments when the fragments get selected.
Solution 4:
By default if you select a tab it will be highlighted. If you want to select Explicitly means use the given commented code under onTabSelected(TabLayout.Tab tab) with your specified tab index position. This code will explains about change fragment on tab selected position using viewpager.
publicclassGalleryFragmentextendsFragmentimplementsTabLayout.OnTabSelectedListener
{
private ViewPager viewPager;public ViewPagerAdapter adapter;private TabLayout tabLayout;
@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
ViewrootView= inflater.inflate(R.layout.fragment_gallery, container, false);
viewPager = (ViewPager) rootView.findViewById(R.id.viewpager);
adapter = newViewPagerAdapter(getChildFragmentManager());
adapter.addFragment(newPaymentCardFragment(), "PAYMENT CARDS");
adapter.addFragment(newLoyaltyCardFragment(), "LOYALTY CARDS");
viewPager.setAdapter(adapter);
tabLayout = (TabLayout) rootView.findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
tabLayout.setOnTabSelectedListener(this);
}
@OverridepublicvoidonTabSelected(TabLayout.Tab tab) {
//This will be called 2nd when you select a tab or swipe using viewpagerfinalintposition= tab.getPosition();
Log.i("card", "Tablayout pos: " + position);
//TabLayout.Tab tabdata=tabLayout.getTabAt(position);//tabdata.select();
tabLayout.post(newRunnable() {
@Overridepublicvoidrun() {
if (position == 0) {
PaymentCardFragmentpaymentCardFragment= getPaymentCardFragment();
if (paymentCardFragment != null) {
VerticalViewpagervp= paymentCardFragment.mypager;
if(vp!=null)
{
//vp.setCurrentItem(position,true);
vp.setCurrentItem(vp.getAdapter().getCount()-1,true);
}
}
}
if (position == 1) {
LoyaltyCardFragmentloyaltyCardFragment= getLoyaltyCardFragment();
if (loyaltyCardFragment != null) {
VerticalViewpagervp= loyaltyCardFragment.mypager;
if(vp!=null)
{
vp.setCurrentItem(position);
}
}
}
}
});
}
@OverridepublicvoidonTabUnselected(TabLayout.Tab tab) {
//This will be called 1st when you select a tab or swipe using viewpager
}
@OverridepublicvoidonTabReselected(TabLayout.Tab tab) {
//This will be called only when you select the already selected tab(Ex: selecting 3rd tab again and again)
}
private PaymentCardFragment getLoyaltyCardFragment() {
Fragmentf= adapter.mFragmentList.get(viewPager.getCurrentItem());
if(f instanceof PaymentCardFragment)
{
return (PaymentCardFragment) f;
}
returnnull;
}
private LoyaltyCardFragment getPaymentCardFragment() {
Fragmentf= adapter.mFragmentList.get(viewPager.getCurrentItem());
if(f instanceof LoyaltyCardFragment)
{
return (LoyaltyCardFragment) f;
}
returnnull;
}
classViewPagerAdapterextendsFragmentPagerAdapter {
public List<Fragment> mFragmentList = newArrayList<>();
privatefinal List<String> mFragmentTitleList = newArrayList<>();
publicvoidaddFragment(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
}
}
Post a Comment for "How To Get Which Fragment Has Been Selected"