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:
- package com.demo;
- import java.util.ArrayList;
- import java.util.List;
- import android.graphics.BitmapFactory;
- import android.graphics.Matrix;
- import android.os.Bundle;
- import android.os.Parcelable;
- import android.support.v4.app.FragmentActivity;
- import android.support.v4.view.PagerAdapter;
- import android.support.v4.view.ViewPager;
- import android.support.v4.view.ViewPager.OnPageChangeListener;
- import android.util.DisplayMetrics;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.animation.Animation;
- import android.view.animation.TranslateAnimation;
- import android.widget.ImageView;
- import android.widget.TextView;
- import android.widget.Toast;
- /**
- * Tab頁面手勢滑動切換以及動畫效果
- *
- * @author D.Winter
- *
- */
- publicclassMainActivityextendsFragmentActivity{
- // ViewPager是google SDk中自帶的一個附加包的一個類,可以用來實現螢幕間的切換。
- // android-support-v4.jar
- privateViewPager mPager;//頁卡內容
- privateList<View> listViews;// Tab頁面列表
- privateImageView cursor;// 動畫圖片
- privateTextView t1, t2, t3,t4;// 頁卡頭標
- privateint offset =0;// 動畫圖片偏移量
- privateint currIndex =0;// 當前頁卡編號
- privateint bmpW;// 動畫圖片寬度
- @Override
- publicvoid onCreate(Bundle savedInstanceState){
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- InitImageView();
- InitTextView();
- InitViewPager();
- }
- /**
- * 初始化頭標
- */
- privatevoidInitTextView(){
- t1 =(TextView) findViewById(R.id.text1);
- t2 =(TextView) findViewById(R.id.text2);
- t3 =(TextView) findViewById(R.id.text3);
- t4 =(TextView) findViewById(R.id.text4);
- t1.setOnClickListener(newMyOnClickListener(0));
- t2.setOnClickListener(newMyOnClickListener(1));
- t3.setOnClickListener(newMyOnClickListener(2));
- t4.setOnClickListener(newMyOnClickListener(3));
- }
- /**
- * 初始化ViewPager
- */
- privatevoidInitViewPager(){
- mPager =(ViewPager) findViewById(R.id.vPager);
- listViews =newArrayList<View>();
- LayoutInflater mInflater = getLayoutInflater();
- listViews.add(mInflater.inflate(R.layout.lay1,null));
- listViews.add(mInflater.inflate(R.layout.lay2,null));
- listViews.add(mInflater.inflate(R.layout.lay3,null));
- listViews.add(mInflater.inflate(R.layout.lay4,null));
- mPager.setAdapter(newMyPagerAdapter(listViews));
- mPager.setCurrentItem(0);
- mPager.setOnPageChangeListener(newMyOnPageChangeListener());
- }
- /**
- * 初始化動畫
- */
- privatevoidInitImageView(){
- cursor =(ImageView) findViewById(R.id.cursor);
- bmpW =BitmapFactory.decodeResource(getResources(), R.drawable.a)
- .getWidth();// 獲取圖片寬度
- DisplayMetrics dm =newDisplayMetrics();
- getWindowManager().getDefaultDisplay().getMetrics(dm);
- int screenW = dm.widthPixels;// 獲取解析度寬度
- offset =(screenW /4- bmpW)/3+23;// 計算偏移量
- Matrix matrix =newMatrix();
- matrix.postTranslate(offset,0);
- cursor.setImageMatrix(matrix);// 設定動畫初始位置
- }
- /**
- * ViewPager介面卡
- */
- publicclassMyPagerAdapterextendsPagerAdapter{
- publicList<View> mListViews;
- publicMyPagerAdapter(List<View> mListViews){
- this.mListViews = mListViews;
- }
- @Override
- publicvoid destroyItem(View arg0,int arg1,Object arg2){
- ((ViewPager) arg0).removeView(mListViews.get(arg1));
- }
- @Override
- publicvoid finishUpdate(View arg0){
- }
- @Override
- publicint getCount(){
- return mListViews.size();
- }
- @Override
- publicObject instantiateItem(View arg0,int arg1){
- ((ViewPager) arg0).addView(mListViews.get(arg1),0);
- return mListViews.get(arg1);
- }
- @Override
- publicboolean isViewFromObject(View arg0,Object arg1){
- return arg0 ==(arg1);
- }
- @Override
- publicvoid restoreState(Parcelable arg0,ClassLoader arg1){
- }
- @Override
- publicParcelable saveState(){
- returnnull;
- }
- @Override
- publicvoid startUpdate(View arg0){
- }
- }
- /**
- * 頭標點選監聽
- */
- publicclassMyOnClickListenerimplementsView.OnClickListener{
- privateint index =0;
- publicMyOnClickListener(int i){
- index = i;
- }
- @Override
- publicvoid onClick(View v){
- mPager.setCurrentItem(index);
- }
- };
- /**
- * 頁卡切換監聽
- */
- publicclassMyOnPageChangeListenerimplementsOnPageChangeListener{
- int one = offset *2+ bmpW;// 頁卡1 -> 頁卡2 偏移量
- int two = one *2;// 頁卡1 -> 頁卡3 偏移量
- int three = one *3;//頁卡1->頁卡4偏移量
- @Override
- publicvoid onPageSelected(int arg0){
- Animation animation =null;
- switch(arg0){
- case0:
- if(currIndex ==1){
- animation =newTranslateAnimation(one,0,0,0);
- }elseif(currIndex ==2){
- animation =newTranslateAnimation(two,0,0,0);
- }elseif(currIndex ==3){
- animation =newTranslateAnimation(three,0,0,0);
- }
- break;
- case1:
- if(currIndex ==0){
- animation =newTranslateAnimation(offset, one,0,0);
- }elseif(currIndex ==2){
- animation =newTranslateAnimation(two, one,0,0);
- }elseif(currIndex ==3){
- animation =newTranslateAnimation(three, one,0,0);
- }
- break;
- case2:
- if(currIndex ==0){
- animation =newTranslateAnimation(offset, two,0,0);
- }elseif(currIndex ==1){
- animation =newTranslateAnimation(one, two,0,0);
- }elseif(currIndex ==3){
- animation =newTranslateAnimation(three, two,0,0);
- }
- break;
- case3:
- if(currIndex ==0){
- animation =newTranslateAnimation(offset, three,0,0);
- }elseif(currIndex ==1){
- animation =newTranslateAnimation(one, three,0,0);
- }elseif(currIndex ==2){
- animation =newTranslateAnimation(two, three,0,0);
- }
- break;
- }
- currIndex = arg0;
- animation.setFillAfter(true);// True:圖片停在動畫結束位置
- animation.setDuration(300);
- cursor.startAnimation(animation);
- }
- @Override
- publicvoid onPageScrolled(int arg0,float arg1,int arg2){
- }
- @Override
- publicvoid onPageScrollStateChanged(int arg0){
- }
- }
- //提示框
- publicvoidDisplayToast(String str){
- Toast.makeText(this, str,Toast.LENGTH_SHORT).show();
- }
- }
下面是一個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。實現點選之後彈出提示框的效果... 請問應該怎麼處理呢。