Change Fragment with ViewPager

后端 未结 2 1172
旧巷少年郎
旧巷少年郎 2021-01-25 04:43

I am using PagerSlidingTab Library for ViewPager. And I want to change Fragment while scrolling of tabs. It is working fine. Check out my code.

相关标签:
2条回答
  • 2021-01-25 05:20

    Explanation:

    It turns out there is an easier implementation for scrollable tabs which doesn't involve another library. You can easily implement tabs into your app using normal Android code straight from the default SDK.

    The Code

    Main Class:

    public class PageSlidingTabStripFragment extends Fragment {
    
        //Variables
        private ViewPager viewPager;
        private PagerTitleStrip pagerTitleStrip;
    
        public PageSlidingTabStripFragment() {
            // Required empty public constructor
        }
    
        @Override
        public void onViewCreated(View view, Bundle savedInstanceState) {
            super.onViewCreated(view, savedInstanceState);
        }
    
        @Override
        public void onActivityCreated(Bundle savedInstanceState) {
    
            super.onActivityCreated(savedInstanceState);
    
            //Find your pager declared in XML
            viewPager = (ViewPager) getView().findViewById(R.id.pager);
    
            //Set the viewPager to a new adapter (see below)
            viewPager.setAdapter(new MyAdapter(getFragmentManager()));
    
            //If your doing scrollable tabs as opposed to fix tabs,
            //you need to find a pagerTitleStrip that is declared in XML
            //just like the pager
            pagerTitleStrip = (PagerTitleStrip)
                getView().findViewById(R.id.pager_title_strip);
    
        }
    
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
            // Inflate the layout for this fragment
            return inflater.inflate(R.layout.[your layout name here], container, false);
        }
    
    }
    

    Adapter:

    //Note: this can go below all of the previous code. Just make sure it's
    //below the last curly bracket in your file!
    class MyAdapter extends FragmentStatePagerAdapter {
    
        public MyAdapter(FragmentManager fm) {
            super(fm);
        }
    
        @Override
        public Fragment getItem(int arg0) {
            Fragment fragment = null;
            if (arg0 == 0) {
                fragment = new InstantOpportunity();
            }
            if (arg0 == 1) {
                fragment = new Events();
            }
            if (arg0 == 2) {
                fragment = new Experts();
            }
            return fragment;
        }
    
        @Override
        public int getCount() {
            return 3;
        }
    
        @Override
        public CharSequence getPageTitle(int position) {
            if (position == 0) {
                return "Instant Opportunity";
            }
            if (position == 1) {
                return "Events";
            }
            if (position == 2) {
                return "Experts";
            }
            return null;
        }
    }
    

    Conclusion:

    I hope this helps you understand another way to make scrollable tabs! I have examples on my Github Page about how to make each type (That being Fixed or Scrollable).

    Links:

    1. Fixed Tabs Example - Click Here
    2. Scrollable Tabs Example - Click Here

    Hope this helps!

    Edit:

    When asked what to import, make sure you select the V4 support fragments.

    0 讨论(0)
  • 2021-01-25 05:27

    please use this example..its very easy.i already implement that.

    reference link

    hope its useful to you.its best example of pager-sliding-tabstrip.

    Use

    framelayout compulsory:
    FrameLayout fl = new FrameLayout(getActivity());
            fl.addView(urFragementView);
    

    and then set your fragement view in this framelayout.

    0 讨论(0)
提交回复
热议问题