How to get which fragment has been selected

前端 未结 4 1120
余生分开走
余生分开走 2021-01-02 05:39

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. Whe

相关标签:
4条回答
  • 2021-01-02 05:52

    In your ActionBar.TabListener you can do this - (I'm assuming that you need the object to be in the Activity)

    class MyTabsListener implements ActionBar.TabListener {
        public Fragment fragment;
    
        public MyTabsListener(Fragment fragment) {
            this.fragment = fragment;
        }
    
    
        public void onTabReselected(Tab tab, FragmentTransaction ft) {
            Toast.makeText(MainActivity.appContext, "Reselected!", Toast.LENGTH_LONG).show();
        }
    
    
        public void onTabSelected(Tab tab, FragmentTransaction ft) {
            ft.replace(R.id.fragment_container, fragment);
    
            if(fragment instanceof SettingsTab) {
                // Create your object, call your function
            }
        }
    
    
        public void onTabUnselected(Tab tab, FragmentTransaction ft) {
            ft.remove(fragment);
    
            if(fragment instanceof SettingsTab) {
                // Destroy your object
            }
        }
    }
    
    0 讨论(0)
  • 2021-01-02 05:55

    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.

    0 讨论(0)
  • 2021-01-02 06:05

    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.

    public class GalleryFragment extends Fragment implements TabLayout.OnTabSelectedListener 
    {
    private ViewPager viewPager;public ViewPagerAdapter adapter;private TabLayout tabLayout;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_gallery, container, false);
    viewPager = (ViewPager) rootView.findViewById(R.id.viewpager);
    adapter = new ViewPagerAdapter(getChildFragmentManager());
    adapter.addFragment(new PaymentCardFragment(), "PAYMENT CARDS");
    adapter.addFragment(new LoyaltyCardFragment(), "LOYALTY CARDS");
    viewPager.setAdapter(adapter);
    tabLayout = (TabLayout) rootView.findViewById(R.id.tabs);
        tabLayout.setupWithViewPager(viewPager);
        tabLayout.setOnTabSelectedListener(this);
    }
    
    @Override
    public void onTabSelected(TabLayout.Tab tab) {
        //This will be called 2nd when you select a tab or swipe using viewpager
        final int position = tab.getPosition();
        Log.i("card", "Tablayout pos: " + position);
        //TabLayout.Tab tabdata=tabLayout.getTabAt(position);
        //tabdata.select();
        tabLayout.post(new Runnable() {
            @Override
            public void run() {
                if (position == 0) {
                    PaymentCardFragment paymentCardFragment = getPaymentCardFragment();
                    if (paymentCardFragment != null) {
                       VerticalViewpager vp = paymentCardFragment.mypager;
                        if(vp!=null)
                        {
                          //vp.setCurrentItem(position,true);
                          vp.setCurrentItem(vp.getAdapter().getCount()-1,true);
                        }
                      }
                }
                if (position == 1) {
                   LoyaltyCardFragment loyaltyCardFragment = getLoyaltyCardFragment();
                    if (loyaltyCardFragment != null) {
                       VerticalViewpager vp = loyaltyCardFragment.mypager;
                        if(vp!=null)
                        {
                            vp.setCurrentItem(position);
                        }
                      }
                }
            }
        });
    }
    
    @Override
    public void onTabUnselected(TabLayout.Tab tab) {
     //This will be called 1st when you select a tab or swipe using viewpager
    }
    @Override
    public void onTabReselected(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() {
        Fragment f = adapter.mFragmentList.get(viewPager.getCurrentItem());
    
       if(f instanceof PaymentCardFragment)
       {
        return (PaymentCardFragment) f;
       }
       return null;
     }
    private LoyaltyCardFragment getPaymentCardFragment() {
        Fragment f = adapter.mFragmentList.get(viewPager.getCurrentItem());
        if(f instanceof LoyaltyCardFragment)
       {
        return (LoyaltyCardFragment) f;
       }
       return null;
     }
      class ViewPagerAdapter extends FragmentPagerAdapter {
       public List<Fragment> mFragmentList = new ArrayList<>();
        private final List<String> mFragmentTitleList = new ArrayList<>();
       public void addFragment(Fragment fragment, String title) {
       mFragmentList.add(fragment);
       mFragmentTitleList.add(title);
      }
     }
    }
    
    0 讨论(0)
  • 2021-01-02 06:11

    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.

    class MyTabsListener implements ActionBar.TabListener {
        public Fragment fragment;
        private final Runnable onSelect;
    
        public MyTabsListener(Fragment fragment) {
            this.fragment = fragment;
            this.onSelect = null;
        }
    
        public MyTabsListener(Fragment fragment, Runnable onSelect) {
            this.fragment = fragment;
            this.onSelect = onSelect;
        }
    
    
        public void onTabReselected(Tab tab, FragmentTransaction ft) {
            Toast.makeText(MainActivity.appContext, "Reselected!", Toast.LENGTH_LONG).show();
        }
    
    
        public void onTabSelected(Tab tab, FragmentTransaction ft) {
            ft.replace(R.id.fragment_container, fragment);
            if(onSelect != null){
                fragment.getActivity().runOnUiThread(onSelect);
            }
        }
    
    
        public void onTabUnselected(Tab tab, FragmentTransaction ft) {
            ft.remove(fragment);
    
    
        }
    }
    
    0 讨论(0)
提交回复
热议问题