标签:
采用PagerAdapter中的FragmentPagerAdapter来实现页面切换,适用于a handful of typically more static fragments to be paged through
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <android.support.v4.view.ViewPager android:id="@+id/vpPager" android:layout_width="match_parent" android:layout_height="wrap_content"> <!-- PagerTabStrip automatically display the page indicator for your pager --> <android.support.v4.view.PagerTabStrip android:id="@+id/pager_header" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="top" android:paddingBottom="4dp" android:paddingTop="4dp" /> </android.support.v4.view.ViewPager> </LinearLayout>
FirstFragment is like SecondFragment
1 package com.example.fragmentpagerdemo; 2 3 import android.os.Bundle; 4 import android.support.v4.app.Fragment; 5 import android.view.LayoutInflater; 6 import android.view.View; 7 import android.view.ViewGroup; 8 import android.widget.TextView; 9 10 public class FirstFragment extends Fragment{ 11 12 //Store instance variables 13 private String title; 14 private int page; 15 16 /** 17 * 每一个fragment实例都可以附带一个bundle对象: bundle-->(key-value) 18 * @param title 19 * @param page 20 * @return 21 */ 22 //newInstance constructor for creating fragment with arguments 23 public static FirstFragment newsInstance(String title, int page) 24 { 25 //首先创建fragment实例 26 FirstFragment fragment = new FirstFragment(); 27 //创建bundle对象 28 Bundle bundle = new Bundle(); 29 //向该bundle对象中添加参数 30 bundle.putInt("page", page); 31 bundle.putString("title", title); 32 //将bundle附加给fragment 33 fragment.setArguments(bundle); 34 return fragment; 35 } 36 // Store instance variables based on arguments passed 37 @Override 38 public void onCreate(Bundle savedInstanceState) { 39 // 40 super.onCreate(savedInstanceState); 41 page = getArguments().getInt("page", 0); 42 title = getArguments().getString("title", null); 43 } 44 45 @Override 46 public View onCreateView(LayoutInflater inflater, ViewGroup container, 47 Bundle savedInstanceState) { 48 // 49 View view = inflater.inflate(R.layout.first_fragment, container, false); 50 TextView text = (TextView) view.findViewById(R.id.textView); 51 text.setText(page + "--" + title); 52 return view; 53 } 54 55 56 57 58 }
1 package com.example.fragmentpagerdemo; 2 3 import android.annotation.SuppressLint; 4 import android.os.Bundle; 5 import android.support.v4.app.Fragment; 6 import android.support.v4.app.FragmentActivity; 7 import android.support.v4.app.FragmentManager; 8 import android.support.v4.app.FragmentPagerAdapter; 9 import android.support.v4.view.ViewPager; 10 import android.support.v4.view.ViewPager.OnPageChangeListener; 11 import android.widget.Toast; 12 13 public class MainActivity extends FragmentActivity{ 14 FragmentPagerAdapter adapterViewPager; 15 ViewPager vpPager; 16 @Override 17 protected void onCreate(Bundle savedInstanceState) { 18 super.onCreate(savedInstanceState); 19 setContentView(R.layout.activity_main); 20 21 vpPager = (ViewPager) findViewById(R.id.vpPager); 22 adapterViewPager = new MyPagerAdapter(getSupportFragmentManager()); 23 vpPager.setAdapter(adapterViewPager); 24 //setup OnPageChangeListener 25 vpPager.setOnPageChangeListener(new OnPageChangeListener() 26 { 27 28 // Called when the scroll state changes: 29 // SCROLL_STATE_IDLE, SCROLL_STATE_DRAGGING, SCROLL_STATE_SETTLING 30 @Override 31 public void onPageScrollStateChanged(int arg0) { 32 // 33 34 } 35 // This method will be invoked when the current page is scrolled 36 @Override 37 public void onPageScrolled(int arg0, float arg1, int arg2) { 38 // 39 40 } 41 // This method will be invoked when a new page becomes selected. 42 @SuppressLint("ShowToast") 43 @Override 44 public void onPageSelected(int arg0) { 45 // 46 System.out.println("The page is cahnged to:" + vpPager.getCurrentItem()); 47 // Toast.makeText(this, "The page is changed to:" + vpPager.getCurrentItem(), Toast.LENGTH_LONG); 48 } 49 }); 50 } 51 public static class MyPagerAdapter extends FragmentPagerAdapter 52 { 53 private static int NUM_ITEMS = 3; 54 public MyPagerAdapter(FragmentManager fm) { 55 super(fm); 56 } 57 @Override 58 public Fragment getItem(int position) { 59 // 60 switch(position) 61 { 62 case 0: 63 return FirstFragment.newsInstance("Page @ 1", 0); 64 case 1: 65 return FirstFragment.newsInstance("Page @ 2", 1); 66 case 2: 67 return SecondFragment.newsInstance("Page @ 3", 2); 68 default: 69 return null; 70 } 71 } 72 @Override 73 public int getCount() { 74 // 75 return NUM_ITEMS; 76 } 77 // Returns the page title for the top indicator 78 @Override 79 public CharSequence getPageTitle(int position) { 80 // 81 return "Page" + position; 82 } 83 } 84 }
ViewPager With FragmentPagerAdapter
标签:
原文地址:http://www.cnblogs.com/CoolRandy/p/4340295.html