10

I have five activities/screens that I would like to be able to swipe between, each has a different function but are interrelated hence the UI concept of swiping between each.

I have found many discussions around ViewPager and PagerAdapters etc. but cannot seem to find one where swiping switches between different activity screens.

Is this even possible? Could someone point me towards an example project of this with source code? Or show me how to adapt an existing tutorial to do what I wish?

Thanks so much, have a good one!

rabbitt
  • 2,558
  • 8
  • 28
  • 41
  • I cannot point you towards an example with activities. I can provide a code example of an application that uses Compatibility Pack Action Bar Tabs and Fragments with a ViewPager, if you like. Basically, each tab is a fragment, hosted in one main activity (my app is steadily growing other activities for other tasks, though). But you'd have to be willing to use fragments, not activities. Interested? – Davek804 Mar 24 '12 at 04:48

2 Answers2

28

You can't use ViewPager to swipe between Activities. You need to convert each of you five Activities into Fragments, then combine everything in one FragmentActivity with the Adapter you use with ViewPager.

Here's a link that goes into detail on converting your current Activities info Fragments.

This is the Fragment topic on the Android Developers website, it has a lot of useful info.

Here's another example (full source) that inflates TextViews on each page.

Here's an example that I typed up:

PagerAdapter:

public class PagerAdapter extends FragmentPagerAdapter {

    private final List<Fragment> mFragments = new ArrayList<Fragment>();

    public PagerAdapter(FragmentManager manager) {
        super(manager);
    }

    public void addFragment(Fragment fragment) {
        mFragments.add(fragment);
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return mFragments.size();
    }

    @Override
    public Fragment getItem(int position) {
        return mFragments.get(position);
    }
}

This should be called in onCreate of your FragmentActivity:

    private void initPaging() {

    FragmentOne fragmentOne = new FragmentOne();
    FragmentTwo fragmentTwo= new FragmentTwo();

    PagerAdapter pagerAdapter = new PagerAdapter(getSupportFragmentManager());
    pagerAdapter.addFragment(fragmentOne);
    pagerAdapter.addFragment(fragmentTwo);

    ViewPager viewPager = (ViewPager) findViewById(R.id.viewPager);
    viewPager.setAdapter(pagerAdapter);
}

This is an example of the layout you'd use for your FragmnetActivity:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

<android.support.v4.view.ViewPager
    android:id="@+id/viewPager"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

</LinearLayout>

To create Fragment create a new class that extends Fragment. The first two methods you'll want to override are onActivityCreated and onCreateView.

Here's how you could do that:

public class FragmentOne extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View view = inflater.inflate(THE_LAYOUT_FROM_YOUR_ORIGINAL_ACTIVITY, container, false);
    return view;
    }
}
adneal
  • 30,484
  • 10
  • 122
  • 151
  • Found this through google; I keep getting an error when setting the adapter: "this.mPagerAdapter". "mPagerAdapter cannot be resolved or is not a field". This is the only error I have. mPagerAdapter works in the PagerAdapter. No idea how to fix it. If I remove the "this." I get a warning that the initialisePaging() isn't used locally. Appreciate any help from those that see this – RED_ Nov 10 '12 at 19:48
  • I have one question, Aneal, How can I add a new fragment to this adapter from FragmentOne? Because I have this fragment "sort of home screen" and if I click something there, then it takes me to other screen, in this case, another fragment and so on... How can i achieve this? – ely87 Dec 18 '12 at 16:00
  • getItem(int position) is getting called twice and thus giving indexOutOfBound exception ... – Gaurav Tailor Sep 08 '14 at 10:55
  • To add to the accepted answer one can alternatively use `PagerAdapter` instead of `FragmentPagerAdapter` to switch between Views instead of Fragments. –  Feb 09 '16 at 13:41
2

Fix For mPageradapter

    PagerAdapter pagerAdapter = new PagerAdapter(getSupportFragmentManager());
    pagerAdapter.addFragment(fragmentOne);
    pagerAdapter.addFragment(fragmentEvents);

    viewPager = (ViewPager) super.findViewById(R.id.pager);
    viewPager.setAdapter(pagerAdapter);
user2212515
  • 1,220
  • 1
  • 12
  • 10