Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android PagerView between Activities

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!

like image 911
rabbitt Avatar asked Mar 24 '12 04:03

rabbitt


People also ask

What is difference between ViewPager and ViewPager2?

ViewPager2 is an improved version of the ViewPager library that offers enhanced functionality and addresses common difficulties with using ViewPager . If your app already uses ViewPager , read this page to learn more about migrating to ViewPager2 .

When should I use ViewPager?

ViewPager in Android is a class that allows the user to flip left and right through pages of data. This class provides the functionality to flip pages in app. It is a widget found in the support library. To use it you'll have to put the element inside your XML layout file that'll contain multiple child views.

How do you call ViewPager fragment from activity?

You'll just need to cast it to the real fragment class if you want to call a specific method. int pos = viewpager. getCurrentItem(); Fragment activeFragment = adapter. getItem(pos); if(pos == 0) ((NPListFragment)activeFragment).

Can we add activity in ViewPager?

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 .


2 Answers

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;
    }
}
like image 157
adneal Avatar answered Oct 18 '22 02:10

adneal


Fix For mPageradapter

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

    viewPager = (ViewPager) super.findViewById(R.id.pager);
    viewPager.setAdapter(pagerAdapter);
like image 28
user2212515 Avatar answered Oct 18 '22 02:10

user2212515