0
    public class SectionsPagerAdapter extends FragmentPagerAdapter {

    public SectionsPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int i) {
        Fragment fragment = new DummySectionFragment();
        Bundle args = new Bundle();
        args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, i + 1);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public int getCount() {
        return 3;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        switch (position) {
            case 0: return getString(R.string.title_section1).toUpperCase();
            case 1: return getString(R.string.title_section2).toUpperCase();
            case 2: return getString(R.string.title_section3).toUpperCase();
        }
        return null;
    }
}

/**
 * A dummy fragment representing a section of the app, but that simply displays dummy text.
 */
public static class DummySectionFragment extends Fragment {
    public DummySectionFragment() {
    }

    public static final String ARG_SECTION_NUMBER = "section_number";

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        TextView textView = new TextView(getActivity());
        textView.setGravity(Gravity.CENTER);
        Bundle args = getArguments();
        textView.setText(Integer.toString(args.getInt(ARG_SECTION_NUMBER)));
        return inflater.inflate(R.layout.fragment_content1, container, false);
    }
}
}

The code above is the template from the SDK for ViewPager, it only put text 1 to 3 when I swipe from one page to one page. The question is how can I replace that text and put my own layout xml when I swipe it, and is it possible to use WebView with this feature. For example: when the activity loads it goes to google.com then when i swipe to other page it change the url of the WebView each time I swipe? Hope someone can help me...

homi3kh
  • 241
  • 1
  • 6
  • 17

2 Answers2

1

You have to create classes that extend from android.support.v4.app.Fragment just like the class DummySectionFragment. Then, into the method getItem(int i) you should instantiate each class you want for each index and the method getCount() must return the number of Fragments you are instantiating.

igorcadelima
  • 136
  • 9
  • 1
    can you make example? I don't really get the fragment thing, it's kinda complicated to me, I will learn fast with a few example :D – homi3kh Aug 28 '12 at 02:54
0

viewpager.setCurrentItem(index) where index is the desired fragment index.

Mohammad Ersan
  • 12,304
  • 8
  • 54
  • 77
  • I don't quite get how to use it, may you explain me? – homi3kh Aug 27 '12 at 02:55
  • 1
    in your code, count is 3, so you can choose between 3 fragments, like `viewpager.setCurrentItem(0);` this will show fragment0, and `viewpager.setCurrentItem(1); & viewpager.setCurrentItem(2);` and these will show fragment1 and then fragment2, try it by your self... – Mohammad Ersan Aug 27 '12 at 06:37