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:

 

[java]  view plain  copy
  1. package com.demo;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.List;  
  5.   
  6. import android.graphics.BitmapFactory;  
  7. import android.graphics.Matrix;  
  8. import android.os.Bundle;  
  9. import android.os.Parcelable;  
  10. import android.support.v4.app.FragmentActivity;  
  11. import android.support.v4.view.PagerAdapter;  
  12. import android.support.v4.view.ViewPager;  
  13. import android.support.v4.view.ViewPager.OnPageChangeListener;  
  14. import android.util.DisplayMetrics;  
  15. import android.view.LayoutInflater;  
  16. import android.view.View;  
  17. import android.view.animation.Animation;  
  18. import android.view.animation.TranslateAnimation;  
  19. import android.widget.ImageView;  
  20. import android.widget.TextView;  
  21. import android.widget.Toast;  
  22.   
  23. /** 
  24.  * Tab页面手势滑动切换以及动画效果 
  25.  *  
  26.  * @author D.Winter 
  27.  *  
  28.  */  
  29. public class MainActivity extends FragmentActivity {  
  30.     // ViewPager是google SDk中自带的一个附加包的一个类,可以用来实现屏幕间的切换。  
  31.     // android-support-v4.jar  
  32.     private ViewPager mPager;//页卡内容  
  33.     private List<View> listViews; // Tab页面列表  
  34.     private ImageView cursor;// 动画图片  
  35.     private TextView t1, t2, t3,t4;// 页卡头标  
  36.     private int offset = 0;// 动画图片偏移量  
  37.     private int currIndex = 0;// 当前页卡编号  
  38.     private int bmpW;// 动画图片宽度  
  39.     @Override  
  40.     public void onCreate(Bundle savedInstanceState) {  
  41.         super.onCreate(savedInstanceState);  
  42.         setContentView(R.layout.main);  
  43.         InitImageView();  
  44.         InitTextView();  
  45.         InitViewPager();  
  46.           
  47.     }  
  48.   
  49.     /** 
  50.      * 初始化头标 
  51.      */  
  52.     private void InitTextView() {  
  53.         t1 = (TextView) findViewById(R.id.text1);  
  54.         t2 = (TextView) findViewById(R.id.text2);  
  55.         t3 = (TextView) findViewById(R.id.text3);  
  56.         t4 = (TextView) findViewById(R.id.text4);  
  57.           
  58.         t1.setOnClickListener(new MyOnClickListener(0));  
  59.         t2.setOnClickListener(new MyOnClickListener(1));  
  60.         t3.setOnClickListener(new MyOnClickListener(2));  
  61.         t4.setOnClickListener(new MyOnClickListener(3));  
  62.     }  
  63.   
  64.     /** 
  65.      * 初始化ViewPager 
  66.      */  
  67.     private void InitViewPager() {  
  68.         mPager = (ViewPager) findViewById(R.id.vPager);  
  69.         listViews = new ArrayList<View>();  
  70.         LayoutInflater mInflater = getLayoutInflater();  
  71.         listViews.add(mInflater.inflate(R.layout.lay1, null));  
  72.         listViews.add(mInflater.inflate(R.layout.lay2, null));  
  73.         listViews.add(mInflater.inflate(R.layout.lay3, null));  
  74.         listViews.add(mInflater.inflate(R.layout.lay4, null));  
  75.         mPager.setAdapter(new MyPagerAdapter(listViews));  
  76.         mPager.setCurrentItem(0);  
  77.         mPager.setOnPageChangeListener(new MyOnPageChangeListener());  
  78.   
  79.     }  
  80.     /** 
  81.      * 初始化动画 
  82.      */  
  83.     private void InitImageView() {  
  84.         cursor = (ImageView) findViewById(R.id.cursor);  
  85.         bmpW = BitmapFactory.decodeResource(getResources(), R.drawable.a)  
  86.                 .getWidth();// 获取图片宽度  
  87.         DisplayMetrics dm = new DisplayMetrics();  
  88.         getWindowManager().getDefaultDisplay().getMetrics(dm);  
  89.         int screenW = dm.widthPixels;// 获取分辨率宽度  
  90.         offset = (screenW / 4 - bmpW) / 3+23;// 计算偏移量  
  91.         Matrix matrix = new Matrix();  
  92.         matrix.postTranslate(offset, 0);  
  93.         cursor.setImageMatrix(matrix);// 设置动画初始位置  
  94.     }  
  95.   
  96.     /** 
  97.      * ViewPager适配器 
  98.      */  
  99.     public class MyPagerAdapter extends PagerAdapter {  
  100.         public List<View> mListViews;  
  101.   
  102.         public MyPagerAdapter(List<View> mListViews) {  
  103.             this.mListViews = mListViews;  
  104.         }  
  105.   
  106.         @Override  
  107.         public void destroyItem(View arg0, int arg1, Object arg2) {  
  108.             ((ViewPager) arg0).removeView(mListViews.get(arg1));  
  109.         }  
  110.   
  111.         @Override  
  112.         public void finishUpdate(View arg0) {  
  113.         }  
  114.   
  115.         @Override  
  116.         public int getCount() {  
  117.             return mListViews.size();  
  118.         }  
  119.   
  120.         @Override  
  121.         public Object instantiateItem(View arg0, int arg1) {  
  122.             ((ViewPager) arg0).addView(mListViews.get(arg1), 0);  
  123.             return mListViews.get(arg1);  
  124.         }  
  125.   
  126.         @Override  
  127.         public boolean isViewFromObject(View arg0, Object arg1) {  
  128.             return arg0 == (arg1);  
  129.         }  
  130.   
  131.         @Override  
  132.         public void restoreState(Parcelable arg0, ClassLoader arg1) {  
  133.         }  
  134.   
  135.         @Override  
  136.         public Parcelable saveState() {  
  137.             return null;  
  138.         }  
  139.   
  140.         @Override  
  141.         public void startUpdate(View arg0) {  
  142.         }  
  143.     }  
  144.   
  145.     /** 
  146.      * 头标点击监听 
  147.      */  
  148.     public class MyOnClickListener implements View.OnClickListener {  
  149.         private int index = 0;  
  150.   
  151.         public MyOnClickListener(int i) {  
  152.             index = i;  
  153.         }  
  154.   
  155.         @Override  
  156.         public void onClick(View v) {  
  157.             mPager.setCurrentItem(index);  
  158.         }  
  159.     };  
  160.   
  161.     /** 
  162.      * 页卡切换监听 
  163.      */  
  164.     public class MyOnPageChangeListener implements OnPageChangeListener {  
  165.   
  166.             int one = offset * 2 + bmpW;// 页卡1 -> 页卡2 偏移量  
  167.             int two = one * 2;// 页卡1 -> 页卡3 偏移量  
  168.             int three = one * 3;//页卡1->页卡4偏移量  
  169.             @Override  
  170.             public void onPageSelected(int arg0) {  
  171.             Animation animation = null;  
  172.             switch (arg0) {  
  173.             case 0:  
  174.                 if (currIndex == 1) {  
  175.                     animation = new TranslateAnimation(one, 000);  
  176.                 } else if (currIndex == 2) {  
  177.                     animation = new TranslateAnimation(two, 000);  
  178.                 }else if (currIndex == 3) {  
  179.                     animation = new TranslateAnimation(three, 000);  
  180.                 }  
  181.                 break;  
  182.             case 1:  
  183.                 if (currIndex == 0) {  
  184.                     animation = new TranslateAnimation(offset, one, 00);  
  185.                 } else if (currIndex == 2) {  
  186.                     animation = new TranslateAnimation(two, one, 00);  
  187.                 }   else if (currIndex == 3) {  
  188.                     animation = new TranslateAnimation(three, one, 00);  
  189.                 }     
  190.                 break;  
  191.             case 2:  
  192.                 if (currIndex == 0) {  
  193.                     animation = new TranslateAnimation(offset, two, 00);  
  194.                 } else if (currIndex == 1) {  
  195.                     animation = new TranslateAnimation(one, two, 00);  
  196.                 } else if (currIndex == 3) {  
  197.                     animation = new TranslateAnimation(three, two, 00);  
  198.                 }         
  199.                 break;  
  200.             case 3:  
  201.                 if (currIndex == 0) {  
  202.                     animation = new TranslateAnimation(offset, three, 00);  
  203.                 } else if (currIndex == 1) {  
  204.                     animation = new TranslateAnimation(one, three, 00);  
  205.                 } else if (currIndex == 2) {  
  206.                     animation = new TranslateAnimation(two, three, 00);  
  207.                 }         
  208.                 break;  
  209.                   
  210.             }  
  211.               
  212.             currIndex = arg0;  
  213.             animation.setFillAfter(true);// True:图片停在动画结束位置  
  214.             animation.setDuration(300);  
  215.             cursor.startAnimation(animation);  
  216.             }  
  217.             @Override  
  218.             public void onPageScrolled(int arg0, float arg1, int arg2) {  
  219.             }  
  220.             @Override  
  221.             public void onPageScrollStateChanged(int arg0) {  
  222.             }  
  223.     }  
  224.       
  225.     //提示框  
  226.     public void DisplayToast(String str) {  
  227.         Toast.makeText(this, str, Toast.LENGTH_SHORT).show();  
  228.     }  
  229.   
  230.       
  231. }  
[html]  view plain  copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.  xmlns:umadsdk="http://schemas.android.com/apk/res/com.LoveBus"  
  4.  android:layout_width="fill_parent"  
  5.  android:layout_height="fill_parent"  
  6.  android:orientation="vertical" >  
  7.   
  8. <LinearLayout  
  9.  android:id="@+id/linearLayout1"  
  10.  android:layout_width="fill_parent"  
  11.  android:layout_height="60.0dip"  
  12.  android:background="#FFFFFF" >  
  13.   
  14. <TextView  
  15.  android:id="@+id/text1"  
  16.  android:layout_width="fill_parent"  
  17.  android:layout_height="fill_parent"  
  18.  android:layout_weight="1.0"  
  19.  android:gravity="center"  
  20.  android:text="第一页"  
  21. android:textColor="#000000"  
  22.  android:textSize="22.0dip" />  
  23.   
  24. <TextView  
  25.  android:id="@+id/text2"  
  26.  android:layout_width="fill_parent"  
  27.  android:layout_height="fill_parent"  
  28.  android:layout_weight="1.0"  
  29.  android:gravity="center"  
  30.  android:text="第二页"  
  31. android:textColor="#000000"  
  32.  android:textSize="22.0dip" />  
  33.   
  34. <TextView  
  35.  android:id="@+id/text3"  
  36.  android:layout_width="fill_parent"  
  37.  android:layout_height="fill_parent"  
  38.  android:layout_weight="1.0"  
  39.  android:gravity="center"  
  40.  android:text="第三页"  
  41. android:textColor="#000000"  
  42.  android:textSize="22.0dip" />  
  43.   
  44. <TextView  
  45.  android:id="@+id/text4"  
  46.  android:layout_width="fill_parent"  
  47.  android:layout_height="fill_parent"  
  48.  android:layout_weight="1.0"  
  49.  android:gravity="center"  
  50.  android:text="第四页"  
  51. android:textColor="#000000"  
  52.  android:textSize="22.0dip" />  
  53.   
  54. </LinearLayout>  
  55.   
  56. <ImageView  
  57.  android:id="@+id/cursor"  
  58.  android:layout_width="fill_parent"  
  59.  android:layout_height="wrap_content"  
  60.  android:scaleType="matrix"  
  61.  android:src="@drawable/a" />  
  62.   
  63. <LinearLayout  
  64.  android:id="@+id/linearLayout2"  
  65.  android:layout_width="match_parent"  
  66.  android:layout_height="wrap_content" >  
  67.    
  68. <android.support.v4.view.ViewPager  
  69.  android:id="@+id/vPager"  
  70.  android:layout_width="match_parent"  
  71.  android:layout_height="wrap_content"  
  72.  android:background="#000000"  
  73.  android:flipInterval="30"  
  74.  android:persistentDrawingCache="animation" >  
  75.  </android.support.v4.view.ViewPager>  
  76.   
  77. </LinearLayout>  
  78.   
  79.  </LinearLayout>  
  80.   
  81. 以下分别是ViewPager里面放置的四个XML布局。用来在Mian.XML里面展示。  
  82. lay1.xml-----------------------  
  83. <?xml version="1.0" encoding="utf-8"?>  
  84. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  85.  android:layout_width="match_parent"  
  86.  android:layout_height="match_parent"  
  87.  android:background="#158684"  
  88.  android:orientation="vertical" >  
  89.   
  90. <TextView  
  91.  android:id="@+id/textView_00"  
  92.  android:layout_width="wrap_content"  
  93.  android:layout_height="wrap_content"  
  94.  android:text=""  
  95.  android:textSize="35.0dip"   
  96. android:textAppearance="?android:attr/textAppearanceLarge" />  
  97.   
  98. <LinearLayout  
  99.  android:id="@+id/linearLayout1"  
  100.  android:layout_width="match_parent"  
  101.  android:layout_height="match_parent" >  
  102.   
  103. <LinearLayout  
  104.  android:id="@+id/linearLayout_diancai"  
  105.  android:layout_width="wrap_content"  
  106.  android:layout_height="match_parent"  
  107.  android:background="#FFFFFF"  
  108.  android:orientation="vertical" >  
  109.   
  110. <TextView  
  111.  android:id="@+id/diancai_text1"  
  112.  android:layout_width="fill_parent"  
  113.  android:layout_height="65dp"  
  114.  android:gravity="center"  
  115.  android:text="@string/drinks"  
  116.  android:textColor="#000000"  
  117.  android:textSize="20.0dip" />  
  118.   
  119. <TextView  
  120.  android:id="@+id/diancai_text2"  
  121.  android:layout_width="fill_parent"  
  122.  android:layout_height="65dp"  
  123.  android:gravity="center"  
  124.  android:text="@string/coffee"  
  125.  android:textColor="#000000"  
  126.  android:textSize="20.0dip" />  
  127.   
  128. <TextView  
  129.  android:id="@+id/diancai_text3"  
  130.  android:layout_width="fill_parent"  
  131.  android:layout_height="65dp"  
  132.  android:gravity="center"  
  133.  android:text="@string/salad"  
  134.  android:textColor="#000000"  
  135.  android:textSize="20.0dip" />  
  136.   
  137. <TextView  
  138.  android:id="@+id/diancai_text4"  
  139.  android:layout_width="fill_parent"  
  140.  android:layout_height="65dp"  
  141.  android:gravity="center"  
  142.  android:text="@string/pizza"  
  143.  android:textColor="#000000"  
  144.  android:textSize="20.0dip" />  
  145.   
  146. <TextView  
  147.  android:id="@+id/diancai_text5"  
  148.  android:layout_width="fill_parent"  
  149.  android:layout_height="65dp"  
  150.  android:gravity="center"  
  151.  android:text="@string/dessert"  
  152.  android:textColor="#000000"  
  153.  android:textSize="20.0dip" />  
  154.   
  155. <TextView  
  156.  android:id="@+id/diancai_text6"  
  157.  android:layout_width="fill_parent"  
  158.  android:layout_height="65dp"  
  159.  android:gravity="center"  
  160.  android:text="@string/wine"  
  161.  android:textColor="#000000"  
  162.  android:textSize="20.0dip" />  
  163.  </LinearLayout>  
  164.   
  165. </LinearLayout>  
  166.   
  167.  </LinearLayout>  
  168.   
  169. lay2.xml--------------------  
  170. <?xml version="1.0" encoding="utf-8"?>  
  171. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  172.  android:layout_width="fill_parent"  
  173.  android:layout_height="fill_parent"  
  174.  android:orientation="vertical"  
  175.  android:background="#FF8684" >  
  176.   
  177. </LinearLayout>  
  178.   
  179. lay3.xml--------------------  
  180. <?xml version="1.0" encoding="utf-8"?>  
  181. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  182.  android:layout_width="fill_parent"  
  183.  android:layout_height="fill_parent"  
  184.  android:orientation="vertical"  
  185.  android:background="#1586FF" >  
  186.   
  187. </LinearLayout>  
  188.   
  189. lay4.xml--------------------  
  190. <?xml version="1.0" encoding="utf-8"?>  
  191. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  192.  android:layout_width="match_parent"  
  193.  android:layout_height="match_parent"  
  194.  android:orientation="vertical"  
  195.  android:background="#158684">  
  196.   
  197. </LinearLayout>  
  198.   
  199. 全部代码如上。我现在想要在Activity里面监听lay1.xml里面的TextView。实现点击之后弹出提示框的效果... 请问应该怎么处理呢。  


 

解决方法

   /**
     * 初始化数据
     */
  @Override
  public Object instantiateItem(View container, int position) {     
    image=new ImageView(IndexActivity.this);
    image.setScaleType(ScaleType.FIT_XY);
    image.setId(vp_image_id);//在这里设置id 
    image.setOnClickListener(new ViewpageOnClickListener(position)); //在这里添加时间 并把索引弄过去
    url=aAdList.optJSONObject(position).optString(K.bean.aAdItem.bUrl_s);     
            ImageLoadStackManage.getInstance().loadImage(url, image);       
            
           ((ViewPager) container).addView(image);
           return image; 
  }




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值