首页 > 系统 > Android > 正文

Android基于ViewPager Fragment实现选项卡

2019-10-24 20:33:10
字体:
来源:转载
供稿:网友

这篇文章主要介绍了Android基于ViewPager Fragment实现选项卡的相关资料,需要的朋友可以参考下

先给大家展示效果图:

Android基于ViewPager Fragment实现选项卡

1.新建TestFragmen继承Fragment

 

 
  1. public class TestFragment extends Fragment { 
  2. private static final String TAG = "TestFragment"
  3. private String hello;// = "hello android"; 
  4. private String defaultHello = "default value"
  5. private Map<string, object=""> maplist; 
  6. static TestFragment newInstance(String s, Map<string, object=""> map) { 
  7. TestFragment newFragment = new TestFragment(); 
  8. // Bundle bundle = new Bundle(); 
  9. // bundle.putString("hello", s); 
  10. // newFragment.setArguments(bundle); 
  11. final SerializableMap myMap=new SerializableMap(); 
  12. myMap.setMap(map); 
  13. Bundle bundle = new Bundle(); 
  14. bundle.putSerializable("map", myMap); 
  15. newFragment.setArguments(bundle); 
  16. return newFragment; 
  17. @Override 
  18. public void onCreate(Bundle savedInstanceState) { 
  19. super.onCreate(savedInstanceState); 
  20. Log.d(TAG, "TestFragment-----onCreate"); 
  21. Bundle args = getArguments(); 
  22. // hello = args != null ? args.getString("hello") : defaultHello; 
  23. Bundle bundle = getArguments(); 
  24. SerializableMap serializableMap = (SerializableMap) bundle.get("map"); 
  25. maplist =serializableMap.getMap(); 
  26. @Override 
  27. public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) { 
  28. Log.d(TAG, "TestFragment-----onCreateView"); 
  29. View view = inflater.inflate(R.layout.lay1, container, false); 
  30. // TextView viewhello = (TextView) view.findViewById(R.id.tv_hello); 
  31. //viewhello.setText(maplist.get("userid")+"time"); 
  32. ListView lv = (ListView) view.findViewById(R.id.listView3); 
  33. ContactAdapter hc = new ContactAdapter(getActivity().getApplicationContext(),getContact()); 
  34. lv.setAdapter(hc); 
  35. lv.setCacheColorHint(0); 
  36. return view; 
  37. private ArrayList<contact> getContact(){ 
  38. ArrayList<contact> hcList = new ArrayList<contact>(); 
  39. for(int i=0;i<10;i++) 
  40. Contact c0 = new Contact(); 
  41. c0.setTxPath(R.drawable.more_game+""); 
  42. c0.setName(maplist.get("userid")+" 年龄:"+maplist.get("age")); 
  43. hcList.add(c0); 
  44. return hcList; 
  45. @Override 
  46. public void onDestroy() { 
  47. super.onDestroy(); 
  48. Log.d(TAG, "TestFragment-----onDestroy"); 
  49. </contact></contact></contact></string,></string,> 

2.MyFragmentPagerAdapter继承FragmentPagerAdapter

 

 
  1. public class MyFragmentPagerAdapter extends FragmentPagerAdapter { 
  2. private ArrayList<fragment> fragmentsList; 
  3. public MyFragmentPagerAdapter(FragmentManager fm) { 
  4. super(fm); 
  5. public MyFragmentPagerAdapter(FragmentManager fm, ArrayList<fragment> fragments) { 
  6. super(fm); 
  7. this.fragmentsList = fragments; 
  8. @Override 
  9. public int getCount() { 
  10. return fragmentsList.size(); 
  11. @Override 
  12. public Fragment getItem(int arg0) { 
  13. return fragmentsList.get(arg0); 
  14. @Override 
  15. public int getItemPosition(Object object) { 
  16. return super.getItemPosition(object); 
  17. </fragment></fragment> 

3.MainActivity 要继承FragmentActivity

 

 
  1. public class MainActivity extends FragmentActivity { 
  2. private static final String TAG = "MainActivity"
  3. private ViewPager mPager; 
  4. private ArrayList<fragment> fragmentsList; 
  5. private ImageView ivBottomLine; 
  6. private TextView tvTabActivity, tvTabGroups, tvTabFriends, tvTabChat; 
  7. private int currIndex = 0; 
  8. private int bottomLineWidth; 
  9. private int offset = 0; 
  10. private int position_one; 
  11. private int position_two; 
  12. private int position_three; 
  13. private Resources resources; 
  14. @Override 
  15. public void onCreate(Bundle savedInstanceState) { 
  16. super.onCreate(savedInstanceState); 
  17. requestWindowFeature(Window.FEATURE_NO_TITLE); 
  18. setContentView(R.layout.main); 
  19. resources = getResources(); 
  20. InitWidth(); 
  21. InitTextView(); 
  22. InitViewPager(); 
  23. private void InitTextView() { 
  24. tvTabActivity = (TextView) findViewById(R.id.tv_tab_activity); 
  25. tvTabGroups = (TextView) findViewById(R.id.tv_tab_groups); 
  26. tvTabFriends = (TextView) findViewById(R.id.tv_tab_friends); 
  27. tvTabChat = (TextView) findViewById(R.id.tv_tab_chat); 
  28. tvTabActivity.setOnClickListener(new MyOnClickListener(0)); 
  29. tvTabGroups.setOnClickListener(new MyOnClickListener(1)); 
  30. tvTabFriends.setOnClickListener(new MyOnClickListener(2)); 
  31. tvTabChat.setOnClickListener(new MyOnClickListener(3)); 
  32. private void InitViewPager() { 
  33. mPager = (ViewPager) findViewById(R.id.vPager); 
  34. fragmentsList = new ArrayList<fragment>(); 
  35. Map<string, object=""> paramMap = new HashMap<string, object="">(); 
  36. paramMap.put("userid","小洪"); 
  37. paramMap.put("age",23); 
  38. Map<string, object=""> paramMap2 = new HashMap<string, object="">(); 
  39. paramMap2.put("userid","vatty"); 
  40. paramMap2.put("age",24); 
  41. Map<string, object=""> paramMap3 = new HashMap<string, object="">(); 
  42. paramMap3.put("userid","小明"); 
  43. paramMap3.put("age",25); 
  44. Map<string, object=""> paramMap4 = new HashMap<string, object="">(); 
  45. paramMap4.put("userid","hongshengpeng.com"); 
  46. paramMap4.put("age",26); 
  47. Fragment activityfragment = TestFragment.newInstance("Hello Activity.",paramMap); 
  48. Fragment groupFragment = TestFragment.newInstance("Hello Group.",paramMap2); 
  49. Fragment friendsFragment=TestFragment.newInstance("Hello Friends.",paramMap3); 
  50. Fragment chatFragment=TestFragment.newInstance("Hello Chat.",paramMap4); 
  51. fragmentsList.add(activityfragment); 
  52. fragmentsList.add(groupFragment); 
  53. fragmentsList.add(friendsFragment); 
  54. fragmentsList.add(chatFragment); 
  55. mPager.setAdapter(new MyFragmentPagerAdapter(getSupportFragmentManager(), fragmentsList)); 
  56. mPager.setCurrentItem(0); 
  57. mPager.setOnPageChangeListener(new MyOnPageChangeListener()); 
  58. private void InitWidth() { 
  59. ivBottomLine = (ImageView) findViewById(R.id.iv_bottom_line); 
  60. bottomLineWidth = ivBottomLine.getLayoutParams().width; 
  61. Log.d(TAG, "cursor imageview width=" + bottomLineWidth); 
  62. DisplayMetrics dm = new DisplayMetrics(); 
  63. getWindowManager().getDefaultDisplay().getMetrics(dm); 
  64. int screenW = dm.widthPixels; 
  65. offset = (int) ((screenW / 4.0 - bottomLineWidth) / 2); 
  66. Log.i("MainActivity""offset=" + offset); 
  67. position_one = (int) (screenW / 4.0); 
  68. position_two = position_one * 2; 
  69. position_three = position_one * 3; 
  70. public class MyOnClickListener implements View.OnClickListener { 
  71. private int index = 0; 
  72. public MyOnClickListener(int i) { 
  73. index = i; 
  74. @Override 
  75. public void onClick(View v) { 
  76. mPager.setCurrentItem(index); 
  77. }; 
  78. public class MyOnPageChangeListener implements OnPageChangeListener { 
  79. @Override 
  80. public void onPageSelected(int arg0) { 
  81. Animation animation = null
  82. switch (arg0) { 
  83. case 0: 
  84. if (currIndex == 1) { 
  85. animation = new TranslateAnimation(position_one, 0, 0, 0); 
  86. tvTabGroups.setTextColor(resources.getColor(R.color.lightwhite)); 
  87. else if (currIndex == 2) { 
  88. animation = new TranslateAnimation(position_two, 0, 0, 0); 
  89. tvTabFriends.setTextColor(resources.getColor(R.color.lightwhite)); 
  90. else if (currIndex == 3) { 
  91. animation = new TranslateAnimation(position_three, 0, 0, 0); 
  92. tvTabChat.setTextColor(resources.getColor(R.color.lightwhite)); 
  93. tvTabActivity.setTextColor(resources.getColor(R.color.white)); 
  94. break
  95. case 1: 
  96. if (currIndex == 0) { 
  97. animation = new TranslateAnimation(0, position_one, 0, 0); 
  98. tvTabActivity.setTextColor(resources.getColor(R.color.lightwhite)); 
  99. else if (currIndex == 2) { 
  100. animation = new TranslateAnimation(position_two, position_one, 0, 0); 
  101. tvTabFriends.setTextColor(resources.getColor(R.color.lightwhite)); 
  102. else if (currIndex == 3) { 
  103. animation = new TranslateAnimation(position_three, position_one, 0, 0); 
  104. tvTabChat.setTextColor(resources.getColor(R.color.lightwhite)); 
  105. tvTabGroups.setTextColor(resources.getColor(R.color.white)); 
  106. break
  107. case 2: 
  108. if (currIndex == 0) { 
  109. animation = new TranslateAnimation(0, position_two, 0, 0); 
  110. tvTabActivity.setTextColor(resources.getColor(R.color.lightwhite)); 
  111. else if (currIndex == 1) { 
  112. animation = new TranslateAnimation(position_one, position_two, 0, 0); 
  113. tvTabGroups.setTextColor(resources.getColor(R.color.lightwhite)); 
  114. else if (currIndex == 3) { 
  115. animation = new TranslateAnimation(position_three, position_two, 0, 0); 
  116. tvTabChat.setTextColor(resources.getColor(R.color.lightwhite)); 
  117. tvTabFriends.setTextColor(resources.getColor(R.color.white)); 
  118. break
  119. case 3: 
  120. if (currIndex == 0) { 
  121. animation = new TranslateAnimation(0, position_three, 0, 0); 
  122. tvTabActivity.setTextColor(resources.getColor(R.color.lightwhite)); 
  123. else if (currIndex == 1) { 
  124. animation = new TranslateAnimation(position_one, position_three, 0, 0); 
  125. tvTabGroups.setTextColor(resources.getColor(R.color.lightwhite)); 
  126. else if (currIndex == 2) { 
  127. animation = new TranslateAnimation(position_two, position_three, 0, 0); 
  128. tvTabFriends.setTextColor(resources.getColor(R.color.lightwhite)); 
  129. tvTabChat.setTextColor(resources.getColor(R.color.white)); 
  130. break
  131. currIndex = arg0; 
  132. animation.setFillAfter(true); 
  133. animation.setDuration(300); 
  134. ivBottomLine.startAnimation(animation); 
  135. @Override 
  136. public void onPageScrolled(int arg0, float arg1, int arg2) { 
  137. @Override 
  138. public void onPageScrollStateChanged(int arg0) { 
  139. }</string,></string,></string,></string,></string,></string,></string,></string,></fragment></fragment> 

4.分别新建lay1.xml、 lay2.xml 、lay3.xml

lay1.xml

 

 
  1. <!--?xml version="1.0" encoding="utf-8"?--> 
  2. <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#fff" android:orientation="vertical"
  3. <textview android:id="@+id/tv_hello" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="TextView" android:visibility="gone"
  4. <listview android:id="@+id/listView3" android:layout_width="match_parent" android:layout_height="wrap_content" android:cachecolorhint="@android:color/transparent" android:divider="@drawable/reader_item_divider" android:listselector="@android:color/transparent"
  5. </listview> 
  6. </textview></linearlayout> 

lay2.xml

 

 
  1. <!--?xml version="1.0" encoding="utf-8"?--> 
  2. <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" android:background="#FF8684"
  3. </linearlayout> 

lay3.xml与lay2.xml类型

 

  1. <!--?xml version="1.0" encoding="utf-8"?--> 
  2. <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" android:background="#1586FF"
  3. </linearlayout> 

main.xml

 

 
  1. <!--?xml version="1.0" encoding="utf-8"?--> 
  2. <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:umadsdk="http://schemas.android.com/apk/res/com.LoveBus" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical"
  3. <linearlayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"
  4. <linearlayout android:layout_width="match_parent" android:layout_height="48dip" android:background="@drawable/android_title_bg"
  5. <imageview android:id="@+id/imageView1" android:layout_width="36dip" android:layout_height="36dip" android:layout_gravity="center_vertical" android:layout_marginleft="10dip" android:src="@drawable/more_game"
  6. <textview android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:layout_marginleft="10dip" android:text="乐够GO" android:textappearance="?android:attr/textAppearanceLarge"
  7. <imageview android:id="@+id/imageView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:paddingleft="10dip" android:src="@drawable/status_online" android:visibility="gone"
  8. </imageview></textview></imageview></linearlayout> 
  9. <linearlayout android:id="@+id/linearLayout1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:paddingbottom="5dip" android:paddingtop="10dip" android:background="@color/coral"
  10. <textview android:id="@+id/tv_tab_activity" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1.0" android:gravity="center" android:text="@string/tab_1" android:textcolor="@color/white" android:textsize="18sp"
  11. <textview android:id="@+id/tv_tab_groups" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1.0" android:gravity="center" android:text="@string/tab_2" android:textcolor="@color/lightwhite" android:textsize="18sp"
  12. <textview android:id="@+id/tv_tab_friends" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1.0" android:gravity="center" android:text="@string/tab_3" android:textcolor="@color/lightwhite" android:textsize="18sp"
  13. <textview android:id="@+id/tv_tab_chat" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1.0" android:gravity="center" android:text="@string/tab_4" android:textcolor="@color/lightwhite" android:textsize="18sp"
  14. </textview></textview></textview></textview></linearlayout> 
  15. <linearlayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="bottom" android:orientation="vertical" android:paddingbottom="3dip"
  16. <imageview android:id="@+id/iv_bottom_line" android:layout_width="40dip" android:layout_height="2dip" android:layout_marginleft="20dip" android:scaletype="matrix" android:src="#fff"
  17. </imageview></linearlayout> 
  18. </linearlayout> 
  19. </android.support.v4.view.viewpager></linearlayout> 

ps:ViewPager + Fragment实现滑动标签页

ViewPager 结合Fragment实现一个Activity里包含多个可滑动的标签页,每个标签页可以有独立的布局及响应。

activity_main.xml

 

 
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  3. xmlns:tools="http://schemas.android.com/tools" 
  4. android:layout_width="match_parent" 
  5. android:layout_height="match_parent" 
  6. android:orientation="vertical">  
  7. <LinearLayout  
  8. android:layout_width="match_parent" 
  9. android:layout_height="wrap_content" 
  10. android:orientation="horizontal">  
  11. <TextView  
  12. android:id="@+id/tv_guid1" 
  13. android:layout_width="wrap_content" 
  14. android:layout_height="wrap_content" 
  15. android:layout_weight="1.0" 
  16. android:gravity="center" 
  17. android:text="特性1" 
  18. android:textSize="18sp"/>  
  19. <TextView  
  20. android:id="@+id/tv_guid2" 
  21. android:layout_width="wrap_content" 
  22. android:layout_height="wrap_content" 
  23. android:layout_weight="1.0" 
  24. android:gravity="center" 
  25. android:text="特性2" 
  26. android:textSize="18sp"/>  
  27. <TextView  
  28. android:id="@+id/tv_guid3" 
  29. android:layout_width="wrap_content" 
  30. android:layout_height="wrap_content" 
  31. android:layout_weight="1.0" 
  32. android:gravity="center" 
  33. android:text="特性3 " 
  34. android:textSize="18sp"/>  
  35. <TextView  
  36. android:id="@+id/tv_guid4" 
  37. android:layout_width="wrap_content" 
  38. android:layout_height="wrap_content" 
  39. android:layout_weight="1.0" 
  40. android:gravity="center" 
  41. android:text="特性4" 
  42. android:textSize="18sp"/>  
  43. </LinearLayout>  
  44. <ImageView  
  45. android:id="@+id/cursor" 
  46. android:layout_width="wrap_content" 
  47. android:layout_height="wrap_content" 
  48. android:scaleType="matrix" 
  49. android:src="@drawable/cursor"/>  
  50. <android.support.v4.view.ViewPager  
  51. android:id="@+id/viewpager" 
  52. android:layout_width="fill_parent" 
  53. android:layout_height="fill_parent" 
  54. android:flipInterval="30" 
  55. android:persistentDrawingCache="animation"/>  
  56. </LinearLayout> 

MainActivity.java

 

 
  1. package com.example.viewpagernfragment;  
  2. import java.util.ArrayList;  
  3. import android.graphics.BitmapFactory;  
  4. import android.graphics.Matrix;  
  5. import android.os.Bundle;  
  6. import android.support.v4.app.Fragment;  
  7. import android.support.v4.app.FragmentActivity;  
  8. import android.support.v4.view.ViewPager;  
  9. import android.support.v4.view.ViewPager.OnPageChangeListener;  
  10. import android.util.DisplayMetrics;  
  11. import android.view.Menu;  
  12. import android.view.View;  
  13. import android.view.animation.Animation;  
  14. import android.view.animation.TranslateAnimation;  
  15. import android.widget.ImageView;  
  16. import android.widget.TextView;  
  17. import android.widget.Toast;  
  18. public class MainActivity extends FragmentActivity {  
  19. private ViewPager mPager;  
  20. private ArrayList<Fragment> fragmentList;  
  21. private ImageView image;  
  22. private TextView view1, view2, view3, view4;  
  23. private int currIndex;//当前页卡编号  
  24. private int bmpW;//横线图片宽度  
  25. private int offset;//图片移动的偏移量  
  26. @Override 
  27. protected void onCreate(Bundle savedInstanceState) {  
  28. super.onCreate(savedInstanceState);  
  29. setContentView(R.layout.activity_main);  
  30. InitTextView();  
  31. InitImage();  
  32. InitViewPager();  
  33. }  
  34. /*  
  35. * 初始化标签名  
  36. */  
  37. public void InitTextView(){  
  38. view1 = (TextView)findViewById(R.id.tv_guid1);  
  39. view2 = (TextView)findViewById(R.id.tv_guid2);  
  40. view3 = (TextView)findViewById(R.id.tv_guid3);  
  41. view4 = (TextView)findViewById(R.id.tv_guid4);  
  42. view1.setOnClickListener(new txListener(0));  
  43. view2.setOnClickListener(new txListener(1));  
  44. view3.setOnClickListener(new txListener(2));  
  45. view4.setOnClickListener(new txListener(3));  
  46. }  
  47. public class txListener implements View.OnClickListener{  
  48. private int index=0;  
  49. public txListener(int i) {  
  50. index =i;  
  51. }  
  52. @Override  
  53. public void onClick(View v) {  
  54. // TODO Auto-generated method stub  
  55. mPager.setCurrentItem(index);  
  56. }  
  57. }  
  58. /*  
  59. * 初始化图片的位移像素  
  60. */  
  61. public void InitImage(){  
  62. image = (ImageView)findViewById(R.id.cursor);  
  63. bmpW = BitmapFactory.decodeResource(getResources(), R.drawable.cursor).getWidth();  
  64. DisplayMetrics dm = new DisplayMetrics();  
  65. getWindowManager().getDefaultDisplay().getMetrics(dm);  
  66. int screenW = dm.widthPixels;  
  67. offset = (screenW/4 - bmpW)/2;  
  68. //imgageview设置平移,使下划线平移到初始位置(平移一个offset)  
  69. Matrix matrix = new Matrix();  
  70. matrix.postTranslate(offset, 0);  
  71. image.setImageMatrix(matrix);  
  72. }  
  73. /*  
  74. * 初始化ViewPager  
  75. */ 
  76. public void InitViewPager(){  
  77. mPager = (ViewPager)findViewById(R.id.viewpager);  
  78. fragmentList = new ArrayList<Fragment>();  
  79. Fragment btFragment= new ButtonFragment();  
  80. Fragment secondFragment = TestFragment.newInstance("this is second fragment");  
  81. Fragment thirdFragment = TestFragment.newInstance("this is third fragment");  
  82. Fragment fourthFragment = TestFragment.newInstance("this is fourth fragment");  
  83. fragmentList.add(btFragment);  
  84. fragmentList.add(secondFragment);  
  85. fragmentList.add(thirdFragment);  
  86. fragmentList.add(fourthFragment);  
  87. //给ViewPager设置适配器  
  88. mPager.setAdapter(new MyFragmentPagerAdapter(getSupportFragmentManager(), fragmentList));  
  89. mPager.setCurrentItem(0);//设置当前显示标签页为第一页  
  90. mPager.setOnPageChangeListener(new MyOnPageChangeListener());//页面变化时的监听器  
  91. }  
  92. public class MyOnPageChangeListener implements OnPageChangeListener{  
  93. private int one = offset *2 +bmpW;//两个相邻页面的偏移量  
  94. @Override 
  95. public void onPageScrolled(int arg0, float arg1, int arg2) {  
  96. // TODO Auto-generated method stub  
  97. }  
  98. @Override 
  99. public void onPageScrollStateChanged(int arg0) {  
  100. // TODO Auto-generated method stub  
  101. }  
  102. @Override 
  103. public void onPageSelected(int arg0) {  
  104. // TODO Auto-generated method stub  
  105. Animation animation = new TranslateAnimation(currIndex*one,arg0*one,0,0);//平移动画  
  106. currIndex = arg0;  
  107. animation.setFillAfter(true);//动画终止时停留在最后一帧,不然会回到没有执行前的状态  
  108. animation.setDuration(200);//动画持续时间0.2秒  
  109. image.startAnimation(animation);//是用ImageView来显示动画的  
  110. int i = currIndex + 1;  
  111. Toast.makeText(MainActivity.this"您选择了第"+i+"个页卡", Toast.LENGTH_SHORT).show();  
  112. }  
  113. }  
  114. @Override 
  115. public boolean onCreateOptionsMenu(Menu menu) {  
  116. // Inflate the menu; this adds items to the action bar if it is present.  
  117. getMenuInflater().inflate(R.menu.main, menu);  
  118. return true;  
  119. }  

谷歌官方认为,ViewPager应该和Fragment一起使用时,此时ViewPager的适配器是FragmentPagerAdapter,当你实现一个FragmentPagerAdapter,你必须至少覆盖以下方法:

getCount()

getItem()

如果ViewPager没有和Fragment一起,ViewPager的适配器是PagerAdapter,它是基类提供适配器来填充页面ViewPager内部,当你实现一个PagerAdapter,你必须至少覆盖以下方法:

 

 
  1. instantiateItem(ViewGroup, int
  2. destroyItem(ViewGroup, int, Object) 
  3. getCount() 
  4. isViewFromObject(View, Object) 
  5. [java] view plaincopy 
  6. package com.example.viewpagernfragment;  
  7. import java.util.ArrayList;  
  8. import android.support.v4.app.Fragment;  
  9. import android.support.v4.app.FragmentManager;  
  10. import android.support.v4.app.FragmentPagerAdapter;  
  11. public class MyFragmentPagerAdapter extends FragmentPagerAdapter{  
  12. ArrayList<Fragment> list;  
  13. public MyFragmentPagerAdapter(FragmentManager fm,ArrayList<Fragment> list) {  
  14. super(fm);  
  15. this.list = list;  
  16. }  
  17. @Override 
  18. public int getCount() {  
  19. return list.size();  
  20. }  
  21. @Override 
  22. public Fragment getItem(int arg0) {  
  23. return list.get(arg0);  
  24. }  
  25. }  
  26. [java] view plaincopy 
  27. package com.example.viewpagernfragment;  
  28. import android.os.Bundle;  
  29. import android.support.v4.app.Fragment;  
  30. import android.view.LayoutInflater;  
  31. import android.view.View;  
  32. import android.view.ViewGroup;  
  33. import android.widget.Button;  
  34. import android.widget.Toast;  
  35. public class ButtonFragment extends Fragment{  
  36. Button myButton;  
  37. @Override 
  38. public View onCreateView(LayoutInflater inflater, ViewGroup container,  
  39. Bundle savedInstanceState) {  
  40. View rootView = inflater.inflate(R.layout.guide_1, container, false);//关联布局文件  
  41. myButton = (Button)rootView.findViewById(R.id.mybutton);//根据rootView找到button  
  42. //设置按键监听事件  
  43. myButton.setOnClickListener(new View.OnClickListener() {  
  44. @Override 
  45. public void onClick(View v) {  
  46. // TODO Auto-generated method stub  
  47. Toast.makeText(ButtonFragment.this.getActivity(), "button is click!", Toast.LENGTH_SHORT).show();  
  48. }  
  49. });  
  50. return rootView;  
  51. }  
  52. }  
  53. [java] view plaincopy 
  54. package com.example.viewpagernfragment;  
  55. import android.os.Bundle;  
  56. import android.support.v4.app.Fragment;  
  57. import android.util.Log;  
  58. import android.view.LayoutInflater;  
  59. import android.view.View;  
  60. import android.view.ViewGroup;  
  61. import android.widget.TextView;  
  62. public class TestFragment extends Fragment {  
  63. private static final String TAG = "TestFragment";  
  64. private String hello;// = "hello android";  
  65. private String defaultHello = "default value";  
  66. static TestFragment newInstance(String s) {  
  67. TestFragment newFragment = new TestFragment();  
  68. Bundle bundle = new Bundle();  
  69. bundle.putString("hello", s);  
  70. newFragment.setArguments(bundle);  
  71. //bundle还可以在每个标签里传送数据  
  72. return newFragment;  
  73. }  
  74. @Override 
  75. public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {  
  76. Log.d(TAG, "TestFragment-----onCreateView");  
  77. Bundle args = getArguments();  
  78. hello = args != null ? args.getString("hello") : defaultHello;  
  79. View view = inflater.inflate(R.layout.guide_2, container, false);  
  80. TextView viewhello = (TextView) view.findViewById(R.id.tv);  
  81. viewhello.setText(hello);  
  82. return view;  
  83. }  
  84. }  
  85.  
  86. <?xml version="1.0" encoding="UTF-8"?>  
  87. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  88. android:layout_width="fill_parent" 
  89. android:layout_height="fill_parent" 
  90. android:background="#ff0000ff" >  
  91. <Button  
  92. android:id="@+id/mybutton" 
  93. android:layout_width="wrap_content" 
  94. android:layout_height="wrap_content" 
  95. android:text="hit me" 
  96. android:gravity="center"/>  
  97. </RelativeLayout>  
  98. [html] view plaincopy 
  99. <?xml version="1.0" encoding="utf-8"?>  
  100. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  101. android:layout_width="fill_parent" 
  102. android:layout_height="fill_parent" 
  103. android:orientation="vertical" 
  104. android:background="#158684" >  
  105. <TextView  
  106. android:id="@+id/tv" 
  107. android:layout_width="wrap_content" 
  108. android:layout_height="wrap_content" 
  109. android:text="TextView" />  
  110. </RelativeLayout> 


注:相关教程知识阅读请移步到Android开发频道。
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表