简体   繁体   中英

Change Fragment with ViewPager

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.

I am using AsynTask() on each Fragment .
When the App opens with the MainActivity, First Fragment is attached to the activity, But It shows two AsynTask() dialog message, one from First and another from Second Fragment . And When I scroll to second tab, It shows dialog message of Third Fragment .

So, If I scroll from left to right in tabs, the Fragment right to the current fragment is displayed and if i scroll from right to left, the Fragment left to the current Fragment is displayed.

Please help me to solve the problem.

My Code:

public class PageSlidingTabStripFragment extends Fragment {

    public static final String TAG = PageSlidingTabStripFragment.class
            .getSimpleName();

    public static PageSlidingTabStripFragment newInstance() {
        return new PageSlidingTabStripFragment();
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setRetainInstance(true);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        return inflater.inflate(R.layout.pager, container, false);
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        PagerSlidingTabStrip tabs = (PagerSlidingTabStrip) view
                .findViewById(R.id.tabs);
        ViewPager pager = (ViewPager) view.findViewById(R.id.pager);

        MyPagerAdapter adapter = new MyPagerAdapter(getChildFragmentManager());

        pager.setAdapter(adapter);
        tabs.setViewPager(pager);
    }

    public class MyPagerAdapter extends FragmentPagerAdapter {

        public MyPagerAdapter(android.support.v4.app.FragmentManager fm) {
            super(fm);
        }

        private final String[] TITLES = { "Instant Opportunity", "Events",
                "Experts" };

        @Override
        public CharSequence getPageTitle(int position) {
            return TITLES[position];
        }

        @Override
        public int getCount() {
            return TITLES.length;
        }

        @Override
        public Fragment getItem(int position) {
            switch (position) {
            case 0:
                return new InstantOpportunity();
            case 1:
                return new Events();
            case 2:
                return new Experts();
            default:
                break;
            }

            return null;
        }
    }
}

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM