具体不解释,直接上代码,嘿嘿.....
View Code
1 package com.demo.viewpager2; 2 3 import java.util.ArrayList; 4 import java.util.List; 5 6 7 import android.app.Activity; 8 import android.os.Bundle; 9 import android.os.Parcelable; 10 import android.support.v4.view.PagerAdapter; 11 import android.support.v4.view.ViewPager; 12 import android.view.View; 13 import android.view.View.OnClickListener; 14 import android.view.ViewGroup; 15 import android.widget.Button; 16 import android.widget.ListView; 17 import android.widget.TextView; 18 import android.widget.Toast; 19 20 public class ViewPager_2Activity extends Activity { 21 private ListlistViews; 22 private ViewPager viewPager; 23 24 25 /** Called when the activity is first created. */ 26 @Override 27 public void onCreate(Bundle savedInstanceState) { 28 super.onCreate(savedInstanceState); 29 setContentView(R.layout.main); 30 listViews = new ArrayList (); 31 listViews.add(View.inflate(getApplicationContext(), R.layout.layout_1, null)); 32 listViews.add(View.inflate(getApplicationContext(), R.layout.layout_2, null)); 33 listViews.add(View.inflate(getApplicationContext(), R.layout.layout_3, null)); 34 35 viewPager = (ViewPager) findViewById(R.id.v_Pager); 36 viewPager.setAdapter(new MyPagerAdapter(listViews)); 37 viewPager.setCurrentItem(0); 38 View view = listViews.get(0); 39 TextView textView = (TextView) view.findViewById(R.id.text_1); 40 textView.setText("10"); 41 Button button = (Button) view.findViewById(R.id.button_1); 42 button.setOnClickListener(new OnClickListener() { 43 44 public void onClick(View v) { 45 // TODO Auto-generated method stub 46 Toast.makeText(getApplicationContext(), "你点击了按钮", Toast.LENGTH_SHORT).show(); 47 } 48 }); 49 } 50 51 private class MyPagerAdapter extends PagerAdapter{ 52 53 private List mListView; 54 55 private MyPagerAdapter(List list) { 56 // TODO Auto-generated method stub 57 this.mListView= list; 58 } 59 60 @Override 61 public void destroyItem(View arg0, int arg1, Object arg2) { 62 // TODO Auto-generated method stub 63 ((ViewGroup)arg0).removeView(mListView.get(arg1)); 64 } 65 66 @Override 67 public void finishUpdate(View arg0) { 68 // TODO Auto-generated method stub 69 70 } 71 72 @Override 73 public int getCount() { 74 // TODO Auto-generated method stub 75 return mListView.size(); 76 } 77 78 @Override 79 public Object instantiateItem(View arg0, int arg1) { 80 // TODO Auto-generated method stub 81 ((ViewGroup)arg0).addView(mListView.get(arg1), 0); 82 return mListView.get(arg1); 83 } 84 85 @Override 86 public boolean isViewFromObject(View arg0, Object arg1) { 87 // TODO Auto-generated method stub 88 return arg0==(arg1); 89 } 90 91 @Override 92 public void restoreState(Parcelable arg0, ClassLoader arg1) { 93 // TODO Auto-generated method stub 94 95 } 96 97 @Override 98 public Parcelable saveState() { 99 // TODO Auto-generated method stub 100 return null; 101 } 102 103 @Override 104 public void startUpdate(View arg0) { 105 // TODO Auto-generated method stub 106 107 } 108 109 } 110 }
main.xml代码
View Code
1 26 7 11 16
其余三个布局:layout_1.xml,layout_2.xml,layout_3.xml
View Code
View Code
View Code
详细看截图