Skip to content Skip to sidebar Skip to footer

Android Swipe View Help Needed

I have a swipe Activity, with 2 swipe pages, I added the content for the first page and on the second page the content is duplicated, how can I set different content to the second

Solution 1:

Android Developers site really has very good explanation of ViewPager. You should check it out: http://developer.android.com/training/animation/screen-slide.html

Here is an example I wrote:

activity_screen_slide.xml

<android.support.v4.view.ViewPager
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

Java Code:

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
...

publicclassScreenSlidePagerActivityextendsFragmentActivity {

    privatestaticfinalintNUM_PAGES=2;
    private ViewPager mPager;
    private PagerAdapter mPagerAdapter;

    @OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_screen_slide);

        mPager = (ViewPager) findViewById(R.id.pager);
        mPagerAdapter = newScreenSlidePagerAdapter(getSupportFragmentManager());
        mPager.setAdapter(mPagerAdapter);
    }

    @OverridepublicvoidonBackPressed() {
        if (mPager.getCurrentItem() == 0) {
            super.onBackPressed();
        } else {
            mPager.setCurrentItem(mPager.getCurrentItem() - 1);
        }
    }

    privateclassScreenSlidePagerAdapterextendsFragmentStatePagerAdapter {
        publicScreenSlidePagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Overridepublic Fragment getItem(int position) {
            //Create these fragments with your preferable namesswitch (position) {
                case0:
                    returnnewScreenSlidePageFragment();
                case1:
                    returnnewScreenSlidePageFragment2();
                default:
                    break;
            }
        }

        @OverridepublicintgetCount() {
            return NUM_PAGES;
        }
    }
}

And here is one of your views that should look like it where fragment_ screen_slide_page is one of your layouts:

import android.support.v4.app.Fragment;
...

publicclassScreenSlidePageFragmentextendsFragment {

    @Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        ViewGrouprootView= (ViewGroup) inflater.inflate(R.layout.fragment_screen_slide_page, container, false);

        return rootView;
    }
}

You should really read the android developers site for further details.

Solution 2:

You can return different fragments like this:

@Overridepublic Fragment getItem(int position) {
    if (position == 0) {
        return PlaceholderFragment.newInstance(position + 1);
    } else {
        return anotherFragment.newInstance();
    }
}

If you want to use the same Fragment, you can change the content depending on the position you are passing to the fragment.

Post a Comment for "Android Swipe View Help Needed"