1. 程式人生 > >Android ViewPager放入多個XML。如何監聽其的控制元件?

Android ViewPager放入多個XML。如何監聽其的控制元件?

我在一個Activity裡面加入了ViewPager。 ViewPager裡面放了兩個XML。XML裡面有幾個TextView控制元件。我想在這個Activity裡面加入ViewPager中XML裡面的控制元件監聽,並且響應點選TextView之後彈出提示框的事件。但是卻一直苦於無法通過findById()方法繫結該TextView控制元件。因為普通情況下一個Activity只能通過setContentView(R.layout.XXXX)繫結顯示一個XML,只能對那一個XML裡面的控制元件進行操作。而我放在ViewPager裡面的XML中的控制元件是不能直接拿出來做操作的。跪求各位高手指出一條明路.......<原始碼奉上,求各位高手幫忙解決一下,謝謝了!>
唯一一個Activity:

  1. package com.demo; 
  2. import java.util.ArrayList; 
  3. import java.util.List; 
  4. import android.graphics.BitmapFactory; 
  5. import android.graphics.Matrix; 
  6. import android.os.Bundle; 
  7. import android.os.Parcelable; 
  8. import android.support.v4.app.FragmentActivity; 
  9. import android.support.v4.view.PagerAdapter; 
  10. import android.support.v4.view.ViewPager; 
  11. import android.support.v4.view.ViewPager.OnPageChangeListener; 
  12. import android.util.DisplayMetrics; 
  13. import android.view.LayoutInflater; 
  14. import android.view.View; 
  15. import android.view.animation.Animation; 
  16. import android.view.animation.TranslateAnimation; 
  17. import android.widget.ImageView; 
  18. import android.widget.TextView; 
  19. import android.widget.Toast; 
  20. /** 
  21. * Tab頁面手勢滑動切換以及動畫效果 
  22. *  
  23. * @author D.Winter 
  24. *  
  25. */ 
  26. publicclassMainActivityextendsFragmentActivity{ 
  27. // ViewPager是google SDk中自帶的一個附加包的一個類,可以用來實現螢幕間的切換。 
  28. // android-support-v4.jar 
  29. privateViewPager mPager;//頁卡內容 
  30. privateList<View> listViews;// Tab頁面列表 
  31. privateImageView cursor;// 動畫圖片 
  32. privateTextView t1, t2, t3,t4;// 頁卡頭標 
  33. privateint offset =0;// 動畫圖片偏移量 
  34. privateint currIndex =0;// 當前頁卡編號 
  35. privateint bmpW;// 動畫圖片寬度 
  36. @Override 
  37. publicvoid onCreate(Bundle savedInstanceState){ 
  38. super.onCreate(savedInstanceState); 
  39.     setContentView(R.layout.main); 
  40. InitImageView(); 
  41. InitTextView(); 
  42. InitViewPager(); 
  43. } 
  44. /** 
  45.  * 初始化頭標 
  46.  */ 
  47. privatevoidInitTextView(){ 
  48.     t1 =(TextView) findViewById(R.id.text1); 
  49.     t2 =(TextView) findViewById(R.id.text2); 
  50.     t3 =(TextView) findViewById(R.id.text3); 
  51.     t4 =(TextView) findViewById(R.id.text4); 
  52.     t1.setOnClickListener(newMyOnClickListener(0)); 
  53.     t2.setOnClickListener(newMyOnClickListener(1)); 
  54.     t3.setOnClickListener(newMyOnClickListener(2)); 
  55.     t4.setOnClickListener(newMyOnClickListener(3)); 
  56. } 
  57. /** 
  58.  * 初始化ViewPager 
  59.  */ 
  60. privatevoidInitViewPager(){ 
  61.     mPager =(ViewPager) findViewById(R.id.vPager); 
  62.     listViews =newArrayList<View>(); 
  63. LayoutInflater mInflater = getLayoutInflater(); 
  64.     listViews.add(mInflater.inflate(R.layout.lay1,null)); 
  65.     listViews.add(mInflater.inflate(R.layout.lay2,null)); 
  66.     listViews.add(mInflater.inflate(R.layout.lay3,null)); 
  67.     listViews.add(mInflater.inflate(R.layout.lay4,null)); 
  68.     mPager.setAdapter(newMyPagerAdapter(listViews)); 
  69.     mPager.setCurrentItem(0); 
  70.     mPager.setOnPageChangeListener(newMyOnPageChangeListener()); 
  71. } 
  72. /** 
  73.  * 初始化動畫 
  74.  */ 
  75. privatevoidInitImageView(){ 
  76.     cursor =(ImageView) findViewById(R.id.cursor); 
  77.     bmpW =BitmapFactory.decodeResource(getResources(), R.drawable.a) 
  78. .getWidth();// 獲取圖片寬度 
  79. DisplayMetrics dm =newDisplayMetrics(); 
  80.     getWindowManager().getDefaultDisplay().getMetrics(dm); 
  81. int screenW = dm.widthPixels;// 獲取解析度寬度 
  82.     offset =(screenW /4- bmpW)/3+23;// 計算偏移量 
  83. Matrix matrix =newMatrix(); 
  84.     matrix.postTranslate(offset,0); 
  85.     cursor.setImageMatrix(matrix);// 設定動畫初始位置 
  86. } 
  87. /** 
  88.  * ViewPager介面卡 
  89.  */ 
  90. publicclassMyPagerAdapterextendsPagerAdapter{ 
  91. publicList<View> mListViews; 
  92. publicMyPagerAdapter(List<View> mListViews){ 
  93. this.mListViews = mListViews; 
  94. } 
  95. @Override 
  96. publicvoid destroyItem(View arg0,int arg1,Object arg2){ 
  97. ((ViewPager) arg0).removeView(mListViews.get(arg1)); 
  98. } 
  99. @Override 
  100. publicvoid finishUpdate(View arg0){ 
  101. } 
  102. @Override 
  103. publicint getCount(){ 
  104. return mListViews.size(); 
  105. } 
  106. @Override 
  107. publicObject instantiateItem(View arg0,int arg1){ 
  108. ((ViewPager) arg0).addView(mListViews.get(arg1),0); 
  109. return mListViews.get(arg1); 
  110. } 
  111. @Override 
  112. publicboolean isViewFromObject(View arg0,Object arg1){ 
  113. return arg0 ==(arg1); 
  114. } 
  115. @Override 
  116. publicvoid restoreState(Parcelable arg0,ClassLoader arg1){ 
  117. } 
  118. @Override 
  119. publicParcelable saveState(){ 
  120. returnnull; 
  121. } 
  122. @Override 
  123. publicvoid startUpdate(View arg0){ 
  124. } 
  125. } 
  126. /** 
  127.  * 頭標點選監聽 
  128.  */ 
  129. publicclassMyOnClickListenerimplementsView.OnClickListener{ 
  130. privateint index =0; 
  131. publicMyOnClickListener(int i){ 
  132.         index = i; 
  133. } 
  134. @Override 
  135. publicvoid onClick(View v){ 
  136.         mPager.setCurrentItem(index); 
  137. } 
  138. }; 
  139. /** 
  140.  * 頁卡切換監聽 
  141.  */ 
  142. publicclassMyOnPageChangeListenerimplementsOnPageChangeListener{ 
  143. int one = offset *2+ bmpW;// 頁卡1 -> 頁卡2 偏移量 
  144. int two = one *2;// 頁卡1 -> 頁卡3 偏移量 
  145. int three = one *3;//頁卡1->頁卡4偏移量 
  146. @Override 
  147. publicvoid onPageSelected(int arg0){ 
  148. Animation animation =null; 
  149. switch(arg0){ 
  150. case0: 
  151. if(currIndex ==1){ 
  152.                 animation =newTranslateAnimation(one,0,0,0); 
  153. }elseif(currIndex ==2){ 
  154.                 animation =newTranslateAnimation(two,0,0,0); 
  155. }elseif(currIndex ==3){ 
  156.                 animation =newTranslateAnimation(three,0,0,0); 
  157. } 
  158. break; 
  159. case1: 
  160. if(currIndex ==0){ 
  161.                 animation =newTranslateAnimation(offset, one,0,0); 
  162. }elseif(currIndex ==2){ 
  163.                 animation =newTranslateAnimation(two, one,0,0); 
  164. }elseif(currIndex ==3){ 
  165.                 animation =newTranslateAnimation(three, one,0,0); 
  166. } 
  167. break; 
  168. case2: 
  169. if(currIndex ==0){ 
  170.                 animation =newTranslateAnimation(offset, two,0,0); 
  171. }elseif(currIndex ==1){ 
  172.                 animation =newTranslateAnimation(one, two,0,0); 
  173. }elseif(currIndex ==3){ 
  174.                 animation =newTranslateAnimation(three, two,0,0); 
  175. } 
  176. break; 
  177. case3: 
  178. if(currIndex ==0){ 
  179.                 animation =newTranslateAnimation(offset, three,0,0); 
  180. }elseif(currIndex ==1){ 
  181.                 animation =newTranslateAnimation(one, three,0,0); 
  182. }elseif(currIndex ==2){ 
  183.                 animation =newTranslateAnimation(two, three,0,0); 
  184. } 
  185. break; 
  186. } 
  187.         currIndex = arg0; 
  188.         animation.setFillAfter(true);// True:圖片停在動畫結束位置 
  189.         animation.setDuration(300); 
  190.         cursor.startAnimation(animation); 
  191. } 
  192. @Override 
  193. publicvoid onPageScrolled(int arg0,float arg1,int arg2){ 
  194. } 
  195. @Override 
  196. publicvoid onPageScrollStateChanged(int arg0){ 
  197. } 
  198. } 
  199. //提示框 
  200. publicvoidDisplayToast(String str){ 
  201. Toast.makeText(this, str,Toast.LENGTH_SHORT).show(); 
  202. } 
  203. }

下面是一個main.XML主佈局檔案
<?xml version="1.0" encoding="utf-8"?>
<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" >

<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="60.0dip"
android:background="#FFFFFF" >

<TextView
android:id="@+id/text1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1.0"
android:gravity="center"
android:text="第一頁"
android:textColor="#000000"
android:textSize="22.0dip" />

<TextView
android:id="@+id/text2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1.0"
android:gravity="center"
android:text="第二頁"
android:textColor="#000000"
android:textSize="22.0dip" />

<TextView
android:id="@+id/text3"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1.0"
android:gravity="center"
android:text="第三頁"
android:textColor="#000000"
android:textSize="22.0dip" />

<TextView
android:id="@+id/text4"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1.0"
android:gravity="center"
android:text="第四頁"
android:textColor="#000000"
android:textSize="22.0dip" />

</LinearLayout>

<ImageView
android:id="@+id/cursor"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:scaleType="matrix"
android:src="@drawable/a" />

<LinearLayout
android:id="@+id/linearLayout2"
android:layout_width="match_parent"
android:layout_height="wrap_content" >

<android.support.v4.view.ViewPager
android:id="@+id/vPager"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#000000"
android:flipInterval="30"
android:persistentDrawingCache="animation" >
</android.support.v4.view.ViewPager>

</LinearLayout>

</LinearLayout>

以下分別是ViewPager裡面放置的四個XML佈局。用來在Mian.XML裡面展示。
lay1.xml-----------------------
<?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:background="#158684"
android:orientation="vertical" >

<TextView
android:id="@+id/textView_00"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:textSize="35.0dip"
android:textAppearance="?android:attr/textAppearanceLarge" />

<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<LinearLayout
android:id="@+id/linearLayout_diancai"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="#FFFFFF"
android:orientation="vertical" >

<TextView
android:id="@+id/diancai_text1"
android:layout_width="fill_parent"
android:layout_height="65dp"
android:gravity="center"
android:text="@string/drinks"
android:textColor="#000000"
android:textSize="20.0dip" />

<TextView
android:id="@+id/diancai_text2"
android:layout_width="fill_parent"
android:layout_height="65dp"
android:gravity="center"
android:text="@string/coffee"
android:textColor="#000000"
android:textSize="20.0dip" />

<TextView
android:id="@+id/diancai_text3"
android:layout_width="fill_parent"
android:layout_height="65dp"
android:gravity="center"
android:text="@string/salad"
android:textColor="#000000"
android:textSize="20.0dip" />

<TextView
android:id="@+id/diancai_text4"
android:layout_width="fill_parent"
android:layout_height="65dp"
android:gravity="center"
android:text="@string/pizza"
android:textColor="#000000"
android:textSize="20.0dip" />

<TextView
android:id="@+id/diancai_text5"
android:layout_width="fill_parent"
android:layout_height="65dp"
android:gravity="center"
android:text="@string/dessert"
android:textColor="#000000"
android:textSize="20.0dip" />

<TextView
android:id="@+id/diancai_text6"
android:layout_width="fill_parent"
android:layout_height="65dp"
android:gravity="center"
android:text="@string/wine"
android:textColor="#000000"
android:textSize="20.0dip" />
</LinearLayout>

</LinearLayout>

</LinearLayout>

lay2.xml--------------------
<?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"
android:background="#FF8684" >

</LinearLayout>

lay3.xml--------------------
<?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"
android:background="#1586FF" >

</LinearLayout>

lay4.xml--------------------
<?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:background="#158684">

</LinearLayout>

全部程式碼如上。我現在想要在Activity裡面監聽lay1.xml裡面的TextView。實現點選之後彈出提示框的效果... 請問應該怎麼處理呢。