0

I'm looking for a short example for usage of Fragments via ViewPager/PageIndicator. All Tutorials/Examples I found provide fancy explanations with generated texts or images that are passed to one single Fragment.

Does anyone can provide a short example with just 2 separate Fragments (incl. xml-layout-part).

A link to an example or tutorial would be also fine! :-)

Kody
  • 1,154
  • 3
  • 14
  • 31
  • Please refer this link http://www.edumobile.org/android/android-beginner-tutorials/view-pager-example-in-android-development/ – Arun PS Apr 18 '13 at 12:41

2 Answers2

5

In the MainActivity that will host the fragments you need to create a fragment adapter class in which in getItem() method you will return a new instance of the fragment. Take a look at this example:

public class MainActivity extends FragmentActivity {

static int numberOfPages = 2;
ViewPager myViewPager;
MyFragmentPagerAdapter myFragmentPagerAdapter;
    String text = "test";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_activity);

    myViewPager = (ViewPager) findViewById(R.id.view_pager);
    myFragmentPagerAdapter = new MyFragmentPagerAdapter(getSupportFragmentManager());
    myViewPager.setAdapter(myFragmentPagerAdapter);
}

// Adapters
private static class MyFragmentPagerAdapter extends FragmentPagerAdapter {
    public MyFragmentPagerAdapter(FragmentManager fm)   {
        super(fm);
    }

    @Override
    public Fragment getItem(int index) {

        return PageFragment.newInstance(text);
    }

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

}

// Fragment class

public class PageFragment extends Fragment {

TextView tv;

public static PageFragment newInstance(String text) {
    PageFragment pageFragment = new PageFragment();
    Bundle bundle = new Bundle();
    bundle.putString("test", text);
    pageFragment.setArguments(bundle);

    return pageFragment;
}

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

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)   {
    View view = inflater.inflate(R.layout.fragment, container, false);
    tv = (TextView) view.findViewById(R.id.tv);
    tv.setText(getArguments().getString("test");        

    return view;
}
Marcin S.
  • 11,161
  • 6
  • 50
  • 63
  • Thanks for your quick reply! How can I add another Fragment? – Kody Feb 26 '13 at 08:01
  • How do you add elements for specific fragments? – Nelson.b.austin Mar 20 '13 at 22:55
  • in getItem() method of the FragmentPagerAdapter you return a Fragment kept in the viewPager 'index' position. For example to refer to the first fragment you could add if condition like if (index == 0) then pass some value to fragment contructor and return it, if (index == 1) passa another value and so on depending on number of fragments you have. – Marcin S. Mar 20 '13 at 23:15
  • Thanks a lot you helped me doing my task – Yousef Zakher Sep 21 '13 at 23:49
4

Open Eclipse --> new Android Application Project --> Press Next --> enter application name and in minimum sdk select anything above 4.0 -- > Press Next 3 times

In New Blank Activity in Navigation Type select "Swipe Views + Title Strip" --> finish

This is pretty nice and simple example of ViewPager.

gladmax
  • 347
  • 1
  • 4
  • One Fix: Eclipse nags that such kind od apps requires sdk version 11 or above. So select your sdk 11 or above and after creating project go to manifest file and change min sdk version from 11 to whatever version you prefer. – Ali Behzadian Nejad May 30 '13 at 14:50
  • He asked for a tutorial, plus that won't work with ex API8. – basickarl Oct 22 '13 at 01:00