Android: ViewPager and Fragments

The following is a simple Android sample application that uses a ViewPager, which is part
of the Support Package, and Fragments.

The FragmentPagerActivity class creates the ViewPager and the associated FragmentPagerAdapter.

 import android.os.Bundle;  
 import android.support.v4.app.Fragment;  
 import android.support.v4.app.FragmentActivity;  
 import android.support.v4.app.FragmentManager;  
 import android.support.v4.app.FragmentPagerAdapter;  
 import android.support.v4.view.ViewPager;  

 public class FragmentPagerActivity extends FragmentActivity {  

      private static final int NUMBER_OF_PAGES = 10;  

      private ViewPager mViewPager;  
      private MyFragmentPagerAdapter mMyFragmentPagerAdapter;  

      public void onCreate(Bundle savedInstanceState) {  

           super.onCreate(savedInstanceState);  
           setContentView(R.layout.main);  
           mViewPager = (ViewPager) findViewById(R.id.viewpager);  
           mMyFragmentPagerAdapter = new MyFragmentPagerAdapter(getSupportFragmentManager());  
           mViewPager.setAdapter(mMyFragmentPagerAdapter);  
      }  

      private static class MyFragmentPagerAdapter extends FragmentPagerAdapter {  

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

           @Override  
           public Fragment getItem(int index) {  

                return PageFragment.newInstance("My Message " + index);
           }  

           @Override  
           public int getCount() {  

                return NUMBER_OF_PAGES;  
           }  
      }  
 }  

The PageFragment only contains a TextView. It is instantiated in the above
MyFragmentPagerAdapter’s getItem(int index) method.

 package org.thomasamsler.android;  

 import android.os.Bundle;  
 import android.support.v4.app.Fragment;  
 import android.view.LayoutInflater;  
 import android.view.View;  
 import android.view.ViewGroup;  
 import android.widget.TextView;  

 public class PageFragment extends Fragment {  

      public static PageFragment newInstance(String title) {

          PageFragment pageFragment = new PageFragment();
          Bundle bundle = new Bundle();
          bundle.putString("title", title);
          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);  
          TextView textView = (TextView) view.findViewById(R.id.textView1);  
          textView.setText(getArguments().getString("title"));
          return view;  
      }  
 }  

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s