Monday, August 24, 2015

Android ViewPager 예제 (FragmentStatePagerAdapter 사용)

Android에서 많이 사용하는 ViewPager의 예제이다. ViewPager를 사용하려면 필수적으로 FragmentActivity를 사용해야하는데, ActionBarActivity를 사용하고 있으면 무시하고 ActionBarActivity를 그대로 사용해도 된다.(FragmentActivity를 상속받고 있기 때문이다.)

public class MainActivity extends ActionBarActivity {
 private ViewPager mViewPager;
 private PagerAdapter mPagerAdapter;
 private class PagerAdapter extends FragmentStatePagerAdapter {
  public PagerAdapter(FragmentManager fm) {
   super(fm);
  }
  @Override
  public Fragment getItem(int position) {
                        // 해당하는 page의 Fragment를 생성합니다.
   return PageFragment.create(position);
  }
  @Override
  public int getCount() {
   return 5;  // 총 5개의 page를 보여줍니다.
  }

 }

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

  mViewPager = (ViewPager) findViewById(R.id.pager);
  mPagerAdapter = new PagerAdapter(getSupportFragmentManager());
  mViewPager.setAdapter(mPagerAdapter);
 }
 @Override
 public boolean onCreateOptionsMenu(Menu menu) {
  // Inflate the menu; this adds items to the action bar if it is present.
  getMenuInflater().inflate(R.menu.main, menu);
  return true;
 }
 @Override
 public boolean onOptionsItemSelected(MenuItem item) {
  // Handle action bar item clicks here. The action bar will
  // automatically handle clicks on the Home/Up button, so long
  // as you specify a parent activity in AndroidManifest.xml.
  int id = item.getItemId();
  if (id == R.id.action_settings) {
   return true;
  }
  return super.onOptionsItemSelected(item);
 }
}

public class PageFragment extends Fragment {
 private int mPageNumber;
 public static PageFragment create(int pageNumber) {
  PageFragment fragment = new PageFragment();
  Bundle args = new Bundle();
  args.putInt("page", pageNumber);
  fragment.setArguments(args);
  return fragment;
 }
 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  mPageNumber = getArguments().getInt("page");
 }
 @Override
 public View onCreateView(LayoutInflater inflater, ViewGroup container,
   Bundle savedInstanceState) {
  ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.fragment_page, container, false);
  ((TextView) rootView.findViewById(R.id.number)).setText(mPageNumber + "");
  return rootView;
 }
}
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.viewpagerexample.MainActivity" >
    <android.support.v4.view.ViewPager
        android:id="@+id/pager"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >
    </android.support.v4.view.ViewPager>
</RelativeLayout>

fragment_page.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
    <TextView
        android:id="@+id/number"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="@string/hello_world" />
</RelativeLayout>

https://github.com/duongame/ViewPagerExample1

No comments:

Post a Comment