Change Fragment with ViewPager

感情迁移 提交于 2019-12-02 06:57:45

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.

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!