Android ViewPager放入多个XML如何监听其的控件

来源:互联网 发布:淘宝 卖东西 编辑:程序博客网 时间:2024/06/13 02:06


新版极客头条上线,每天一大波干货     任玉刚:Android开发者的职场规划     从零练就iOS高手实战班震撼来袭     新型数据库利弊谈    
 
分类: android 项目进阶 2038人阅读 评论(5) 收藏 举报

我在一个Activity里面加入了ViewPager。  ViewPager里面放了两个XML。XML里面有几个TextView控件。我想在这个Activity里面加入ViewPager中XML里面的控件监听,并且响应点击TextView之后弹出提示框的事件。但是却一直苦于无法通过findById()方法绑定该TextView控件。因为普通情况下一个Activity只能通过setContentView(R.layout.XXXX)绑定显示一个XML,只能对那一个XML里面的控件进行操作。而我放在ViewPager里面的XML中的控件是不能直接拿出来做操作的。跪求各位高手指出一条明路.......<源码奉上,求各位高手帮忙解决一下,谢谢了!>
唯一一个Activity:

 

[java] view plaincopy
  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 plaincopy
  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; 
  }


 

版权声明:本文为博主原创文章,未经博主允许不得转载。

主题推荐
viewpagerandroidxml控件
猜你在找
Apple Watch开发入门
C语言及程序设计提高
反编译Android应用
Struts实战-使用SSH框架技术开发学籍管理系统
网络赚钱靠谱项目推荐
android view的xml属性
opencv基本图像处理添加滑动条trackbar
在代码中动态绑定TabHost内容的两种方法Android
AndroidUI更新方法五利用AsyncTask更新UI
线程中的wait和notify方法
准备好了么? 更多职位尽在 
移动-IOS开发工程师
北京爱奇艺科技有限公司
15-30K/月
我要跳槽
iOS开发工程师
上海彩亿信息技术有限公司
8-15K/月
我要跳槽
iOS
人民网股份有限公司
12-15K/月
我要跳槽
IOS工程师
路普达网络科技(北京)有限公司
10-20K/月
我要跳槽
查看评论
5楼 flying_IT 2015-06-03 15:34发表 [回复]
怎么点击没效果呢
4楼 snowycarrot 2014-03-18 23:14发表 [回复]
再提供一种像我一样的菜鸟更容易理解的方法:
listViews.add(mInflater.inflate(R.layout.lay1, null));
不要像上面合在一起写,分开写:
View v1 = mInflater.inflate(R.layout.lay1, null);
listViews.add(v1);
然后通过v1.findViewById找到对应控件,就可以添加事件了

看了fragment之后偶然灵感,与君共享!
3楼 晨曦软海 2014-01-17 15:09发表 [回复]
通过为 lay1.xml里面的TextView android:onclick="DisplayToast" 可以实现。 但我现在任然有一个问题: 如果再lay1.xml 文件里面加入一个ListView ,可以通过findViewById找到这个listview并且为其提供adapter,但如何监听这个ListView里面每一个item 的点击事件呢?不知楼主是否遇到过?
2楼 doudingchenlei 2014-01-11 11:14发表 [回复]
解决了,谢谢
1楼 doudingchenlei 2014-01-10 20:32发表 [回复]
你好,我遇到跟你一样的问题,然后我是菜鸟,你的解决办法我有点看不懂,那个索引是什么东西?
发表评论
  • 用 户 名:
  • Leaning_wk
  • 评论内容:
  • 插入代码
      
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
核心技术类目
全部主题 Hadoop AWS 移动游戏 Java Android iOS Swift 智能硬件 Docker OpenStack VPN Spark ERP IE10Eclipse CRM JavaScript 数据库 Ubuntu NFC WAP jQuery BI HTML5 Spring Apache .NET API HTML SDK IISFedora XML LBS Unity Splashtop UML components Windows Mobile Rails QEMU KDE Cassandra CloudStack FTCcoremail OPhone CouchBase 云计算 iOS6 Rackspace Web App SpringSide Maemo Compuware 大数据 aptech PerlTornado Ruby Hibernate ThinkPHP HBase Pure Solr Angular Cloud Foundry Redis Scala Django Bootstrap
    个人资料
     
    aiqing0119
     
    • 访问:85655次
    • 积分:1482
    • 等级: 
    • 排名:第14391名
    • 原创:37篇
    • 转载:53篇
    • 译文:19篇
    • 评论:19条
    文章分类
  • android 项目进阶(59)
  • android 基础部分(19)
  • java 语言基础(15)
  • 野史评书(4)
  • 管理运维(0)
  • C++学习精讲(6)
  • android系统移植(0)
  • 股史纵横(1)
  • 经济民生(0)
  • android开源项目(2)
  • java开源项目(0)
  • api翻译(7)
    文章存档
  • 2015年07月(1)
  • 2014年09月(1)
  • 2014年07月(2)
  • 2014年06月(2)
  • 2014年05月(33)
    展开
    阅读排行
  • android的Environment类(10253)
  • android 布局透明度设置(4931)
  • Camera.Parameters 参数(4738)
  • Android布局中ScrollView与ListView的冲突的最简单方法(listItem.measure(0, 0))(4440)
  • Calling startActivity() from outside of an Activity context requires the FLA(3847)
  • POI读取word转换html(3695)
  • 是谁给魏延的头上安上了反骨(3525)
  • 太监娶妻是干什么用的(2198)
  • 周瑜正名:实非嫉贤妒能、心胸狭隘之人(2096)
  • Android ViewPager放入多个XML如何监听其的控件(2036)
    评论排行
  • POI读取word转换html(5)
  • Android ViewPager放入多个XML如何监听其的控件(5)
  • Camera.Parameters 参数(3)
  • Android布局中ScrollView与ListView的冲突的最简单方法(listItem.measure(0, 0))(2)
  • android 布局透明度设置(1)
  • android中类 Locale的使用(1)
  • 友盟的自动更新组件(1)
  • Calling startActivity() from outside of an Activity context requires the FLA(1)
  • Android PendingIntent(0)
  • Java基础复习笔记11基本排序算法(0)
    推荐文章
      最新评论
    • Camera.Parameters 参数

      菊草叶与圆企鹅: @u012175707:我就想回复这句话,没想到有人说了

    • Android ViewPager放入多个XML如何监听其的控件

      flying_IT: 怎么点击没效果呢

    • 友盟的自动更新组件

      cocos2dx3: updateInfo.path出来是一串乱码。不会被用户吐槽么?

    • android中类 Locale的使用

      _LinDL: 试问Locale.CHINA和Locale.CHINESE的区别

    • POI读取word转换html

      qq_24719959: 请问为什么图片不能转过来

    • android获取手机通讯录

      zhj5979: 发现了一篇 和你这惊人的相似

    • android 布局透明度设置

      Kinny_Qin: 有帮助

    • Camera.Parameters 参数

      eking2000: @u012175707:94 94

    • Calling startActivity() from outside of an Activity context requires the FLA

      deng_ta: 赞

    • Camera.Parameters 参数

      阿博3世: 你真是闲的蛋疼, 直接google翻译的吧, 你自己读一下那些句子看通不通

     
    0 0
    原创粉丝点击