ViewPager使用方法及子View的获取
具体不解释,直接上代码,嘿嘿.....
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 List<View> listViews;
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<View>();
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<View> mListView;
54
55 private MyPagerAdapter(List<View> 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 <?xml version="1.0" encoding="utf-8"?>
2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3 android:layout_width="fill_parent"
4 android:layout_height="fill_parent"
5 android:orientation="vertical" >
6
7 <TextView
8 android:layout_width="fill_parent"
9 android:layout_height="wrap_content"
10 android:text="@string/hello" />
11 <android.support.v4.view.ViewPager
12 android:id="@+id/v_Pager"
13 android:layout_width="fill_parent"
14 android:layout_height="fill_parent"
15 ></android.support.v4.view.ViewPager>
16 </LinearLayout>
其余三个布局:layout_1.xml,layout_2.xml,layout_3.xml
View Code
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView android:id="@+id/text_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="15"
android:textSize="150sp"
android:layout_gravity="center_horizontal"/>
<Button
android:id="@+id/button_1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="第1页面"
android:textSize="30sp"
></Button>
</LinearLayout>
View Code
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView android:id="@+id/text_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="25"
android:textSize="150sp"
android:layout_gravity="center_horizontal"/>
<Button
android:id="@+id/button_2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="第2页面"
android:textSize="30sp"
></Button>
</LinearLayout>
View Code
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView android:id="@+id/text_3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="35"
android:textSize="150sp"
android:layout_gravity="center_horizontal"/>
<Button
android:id="@+id/button_3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="第3页面"
android:textSize="30sp"
></Button>
</LinearLayout>
详细看截图
posted on 2012-04-05 23:37 simpleceo 阅读(4174) 评论(0) 编辑 收藏 举报