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

来源:互联网 发布:耳根算是网络写手吗? 编辑:程序博客网 时间:2024/06/04 19:21

http://www.dewen.org/q/3543 (原文地址)

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.  
  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.  */ 
  84. private void InitImageView() { 
  85.     cursor = (ImageView) findViewById(R.id.cursor); 
  86.     bmpW = BitmapFactory.decodeResource(getResources(), R.drawable.a) 
  87.             .getWidth();// 获取图片宽度 
  88.     DisplayMetrics dm = new DisplayMetrics(); 
  89.     getWindowManager().getDefaultDisplay().getMetrics(dm); 
  90.     int screenW = dm.widthPixels;// 获取分辨率宽度 
  91.     offset = (screenW / 4 - bmpW) / 3+23;// 计算偏移量 
  92.     Matrix matrix = new Matrix(); 
  93.     matrix.postTranslate(offset, 0); 
  94.     cursor.setImageMatrix(matrix);// 设置动画初始位置 
  95. } 
  96.  
  97. /** 
  98.  * ViewPager适配器 
  99.  */ 
  100. public class MyPagerAdapter extends PagerAdapter { 
  101.     public List<View> mListViews; 
  102.  
  103.     public MyPagerAdapter(List<View> mListViews) { 
  104.         this.mListViews = mListViews; 
  105.     } 
  106.  
  107.     @Override 
  108.     public void destroyItem(View arg0, int arg1, Object arg2) { 
  109.         ((ViewPager) arg0).removeView(mListViews.get(arg1)); 
  110.     } 
  111.  
  112.     @Override 
  113.     public void finishUpdate(View arg0) { 
  114.     } 
  115.  
  116.     @Override 
  117.     public int getCount() { 
  118.         return mListViews.size(); 
  119.     } 
  120.  
  121.     @Override 
  122.     public Object instantiateItem(View arg0, int arg1) { 
  123.         ((ViewPager) arg0).addView(mListViews.get(arg1), 0); 
  124.         return mListViews.get(arg1); 
  125.     } 
  126.  
  127.     @Override 
  128.     public boolean isViewFromObject(View arg0, Object arg1) { 
  129.         return arg0 == (arg1); 
  130.     } 
  131.  
  132.     @Override 
  133.     public void restoreState(Parcelable arg0, ClassLoader arg1) { 
  134.     } 
  135.  
  136.     @Override 
  137.     public Parcelable saveState() { 
  138.         return null; 
  139.     } 
  140.  
  141.     @Override 
  142.     public void startUpdate(View arg0) { 
  143.     } 
  144. } 
  145.  
  146. /** 
  147.  * 头标点击监听 
  148.  */ 
  149. public class MyOnClickListener implements View.OnClickListener { 
  150.     private int index = 0; 
  151.  
  152.     public MyOnClickListener(int i) { 
  153.         index = i; 
  154.     } 
  155.  
  156.     @Override 
  157.     public void onClick(View v) { 
  158.         mPager.setCurrentItem(index); 
  159.     } 
  160. }; 
  161.  
  162. /** 
  163.  * 页卡切换监听 
  164.  */ 
  165. public class MyOnPageChangeListener implements OnPageChangeListener { 
  166.  
  167.         int one = offset * 2 + bmpW;// 页卡1 -> 页卡2 偏移量 
  168.         int two = one * 2;// 页卡1 -> 页卡3 偏移量 
  169.         int three = one * 3;//页卡1->页卡4偏移量 
  170.         @Override 
  171.         public void onPageSelected(int arg0) { 
  172.         Animation animation = null; 
  173.         switch (arg0) { 
  174.         case 0: 
  175.             if (currIndex == 1) { 
  176.                 animation = new TranslateAnimation(one, 0, 0, 0); 
  177.             } else if (currIndex == 2) { 
  178.                 animation = new TranslateAnimation(two, 0, 0, 0); 
  179.             }else if (currIndex == 3) { 
  180.                 animation = new TranslateAnimation(three, 0, 0, 0); 
  181.             } 
  182.             break; 
  183.         case 1: 
  184.             if (currIndex == 0) { 
  185.                 animation = new TranslateAnimation(offset, one, 0, 0); 
  186.             } else if (currIndex == 2) { 
  187.                 animation = new TranslateAnimation(two, one, 0, 0); 
  188.             }   else if (currIndex == 3) { 
  189.                 animation = new TranslateAnimation(three, one, 0, 0); 
  190.             }    
  191.             break; 
  192.         case 2: 
  193.             if (currIndex == 0) { 
  194.                 animation = new TranslateAnimation(offset, two, 0, 0); 
  195.             } else if (currIndex == 1) { 
  196.                 animation = new TranslateAnimation(one, two, 0, 0); 
  197.             } else if (currIndex == 3) { 
  198.                 animation = new TranslateAnimation(three, two, 0, 0); 
  199.             }        
  200.             break; 
  201.         case 3: 
  202.             if (currIndex == 0) { 
  203.                 animation = new TranslateAnimation(offset, three, 0, 0); 
  204.             } else if (currIndex == 1) { 
  205.                 animation = new TranslateAnimation(one, three, 0, 0); 
  206.             } else if (currIndex == 2) { 
  207.                 animation = new TranslateAnimation(two, three, 0, 0); 
  208.             }        
  209.             break; 
  210.  
  211.         } 
  212.  
  213.         currIndex = arg0; 
  214.         animation.setFillAfter(true);// True:图片停在动画结束位置 
  215.         animation.setDuration(300); 
  216.         cursor.startAnimation(animation); 
  217.         } 
  218.         @Override 
  219.         public void onPageScrolled(int arg0, float arg1, int arg2) { 
  220.         } 
  221.         @Override 
  222.         public void onPageScrollStateChanged(int arg0) { 
  223.         } 
  224. } 
  225.  
  226. //提示框 
  227. public void DisplayToast(String str) { 
  228.     Toast.makeText(this, str, Toast.LENGTH_SHORT).show(); 
  229. } 
  230.  
  231.  
  232. }

下面是一个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。实现点击之后弹出提示框的效果... 请问应该怎么处理呢。


回答:

网友回答:你的问题完全可以解决的,不用细看你的代码了, 解决问题的点在

  
  1. listViews = new ArrayList<View>(); 
  2. LayoutInflater mInflater = getLayoutInflater(); 
  3. listViews.add(mInflater.inflate(R.layout.lay1, null)); 
  4. listViews.add(mInflater.inflate(R.layout.lay2, null)); 
  5. listViews.add(mInflater.inflate(R.layout.lay3, null)); 
  6. listViews.add(mInflater.inflate(R.layout.lay4, null));

这个相当于你 Viewpager 中的四页
mInflater.inflate(R.layout.xxx, null) 返回的是当前指定的xml的生成的View 对象, 你完全可以吧mInflater 理解为xml视图解析器。
假设每一个页都一个id为: text_view 的TextView,
你可以写成:
View one_page = mInflater.inflate(R.layout.xxx, null);
TextView one_text = (TextView) one_page.findViewById(R.id.text_view);
one_text.setOnClickListener(xxx);---------------解决办法(版主尝试了,效果良好)

同理往下继续写就行了。
或者从你的listviews 中把你保持的没一页View 取出来点findViewById(R.id.text_view),去找到对应页面的id为 text_view 的TextView,然后对应设置监听即可。
我不懂我讲明白没有。。。有不懂就来找我吧

该答案已被锁定,无法对其进行评论,编辑及投票。
()
评论 (2) •链接 • 2012-07-24
  • 0 支持
    讲得很清楚呢.... 万分感谢哈。我去试一下...... – 猫咪爱喝菊花茶2012-07-25
  • 0 支持
    O(∩_∩)O哈!,问题解决了就行 – 小包2012-07-26

使用LayoutInflater来加载其他xml布局文件

  
  1. LayoutInflater inflater = getLayoutInflater();    
  2. inflater.inflate(R.layout.tv,layout);

评论 (2) •链接 • 2012-07-20
  • 0 支持
    我在初始化ViewPager初始化的类里面用了add(mInflater.inflate(R.layout.lay1,null)); 把加载好的XML绑定到一个List<View>中。然后用了把List<View>丢到ViewPager里面以便可以用手指左右划动来控制XML在ViewPager里面的显示。 刚才试过了,即使用你的这个方法。在给XML里面的控件加监听的之后,程序还是会自动停止。------是不是ViewPager里面的XML压根就不能对其内部的控件加上监听器啊? – 猫咪爱喝菊花茶 2012-07-20
  • 0 支持
    是可以监听的。应该是你的写法有问题,希望你能看一下报错信息,定位到出错的位置,问题就好解决了。 – sC_Cs2012-07-23

原创粉丝点击