我在一个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
- *
- */
- public class MainActivity extends FragmentActivity {
- // ViewPager是google SDk中自带的一个附加包的一个类,可以用来实现屏幕间的切换。
- // android-support-v4.jar
- private ViewPager mPager;//页卡内容
- private List<View> listViews; // Tab页面列表
- private ImageView cursor;// 动画图片
- private TextView t1, t2, t3,t4;// 页卡头标
- private int offset = 0;// 动画图片偏移量
- private int currIndex = 0;// 当前页卡编号
- private int bmpW;// 动画图片宽度
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- InitImageView();
- InitTextView();
- InitViewPager();
- }
- /**
- * 初始化头标
- */
- private void InitTextView() {
- 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(new MyOnClickListener(0));
- t2.setOnClickListener(new MyOnClickListener(1));
- t3.setOnClickListener(new MyOnClickListener(2));
- t4.setOnClickListener(new MyOnClickListener(3));
- }
- /**
- * 初始化ViewPager
- */
- private void InitViewPager() {
- mPager = (ViewPager) findViewById(R.id.vPager);
- listViews = new ArrayList<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(new MyPagerAdapter(listViews));
- mPager.setCurrentItem(0);
- mPager.setOnPageChangeListener(new MyOnPageChangeListener());
- }
- /**
- * 初始化动画
- */
- private void InitImageView() {
- cursor = (ImageView) findViewById(R.id.cursor);
- bmpW = BitmapFactory.decodeResource(getResources(), R.drawable.a)
- .getWidth();// 获取图片宽度
- DisplayMetrics dm = new DisplayMetrics();
- getWindowManager().getDefaultDisplay().getMetrics(dm);
- int screenW = dm.widthPixels;// 获取分辨率宽度
- offset = (screenW / 4 - bmpW) / 3+23;// 计算偏移量
- Matrix matrix = new Matrix();
- matrix.postTranslate(offset, 0);
- cursor.setImageMatrix(matrix);// 设置动画初始位置
- }
- /**
- * ViewPager适配器
- */
- public class MyPagerAdapter extends PagerAdapter {
- public List<View> mListViews;
- public MyPagerAdapter(List<View> mListViews) {
- this.mListViews = mListViews;
- }
- @Override
- public void destroyItem(View arg0, int arg1, Object arg2) {
- ((ViewPager) arg0).removeView(mListViews.get(arg1));
- }
- @Override
- public void finishUpdate(View arg0) {
- }
- @Override
- public int getCount() {
- return mListViews.size();
- }
- @Override
- public Object instantiateItem(View arg0, int arg1) {
- ((ViewPager) arg0).addView(mListViews.get(arg1), 0);
- return mListViews.get(arg1);
- }
- @Override
- public boolean isViewFromObject(View arg0, Object arg1) {
- return arg0 == (arg1);
- }
- @Override
- public void restoreState(Parcelable arg0, ClassLoader arg1) {
- }
- @Override
- public Parcelable saveState() {
- return null;
- }
- @Override
- public void startUpdate(View arg0) {
- }
- }
- /**
- * 头标点击监听
- */
- public class MyOnClickListener implements View.OnClickListener {
- private int index = 0;
- public MyOnClickListener(int i) {
- index = i;
- }
- @Override
- public void onClick(View v) {
- mPager.setCurrentItem(index);
- }
- };
- /**
- * 页卡切换监听
- */
- public class MyOnPageChangeListener implements OnPageChangeListener {
- int one = offset * 2 + bmpW;// 页卡1 -> 页卡2 偏移量
- int two = one * 2;// 页卡1 -> 页卡3 偏移量
- int three = one * 3;//页卡1->页卡4偏移量
- @Override
- public void onPageSelected(int arg0) {
- Animation animation = null;
- switch (arg0) {
- case 0:
- if (currIndex == 1) {
- animation = new TranslateAnimation(one, 0, 0, 0);
- } else if (currIndex == 2) {
- animation = new TranslateAnimation(two, 0, 0, 0);
- }else if (currIndex == 3) {
- animation = new TranslateAnimation(three, 0, 0, 0);
- }
- break;
- case 1:
- if (currIndex == 0) {
- animation = new TranslateAnimation(offset, one, 0, 0);
- } else if (currIndex == 2) {
- animation = new TranslateAnimation(two, one, 0, 0);
- } else if (currIndex == 3) {
- animation = new TranslateAnimation(three, one, 0, 0);
- }
- break;
- case 2:
- if (currIndex == 0) {
- animation = new TranslateAnimation(offset, two, 0, 0);
- } else if (currIndex == 1) {
- animation = new TranslateAnimation(one, two, 0, 0);
- } else if (currIndex == 3) {
- animation = new TranslateAnimation(three, two, 0, 0);
- }
- break;
- case 3:
- if (currIndex == 0) {
- animation = new TranslateAnimation(offset, three, 0, 0);
- } else if (currIndex == 1) {
- animation = new TranslateAnimation(one, three, 0, 0);
- } else if (currIndex == 2) {
- animation = new TranslateAnimation(two, three, 0, 0);
- }
- break;
- }
- currIndex = arg0;
- animation.setFillAfter(true);// True:图片停在动画结束位置
- animation.setDuration(300);
- cursor.startAnimation(animation);
- }
- @Override
- public void onPageScrolled(int arg0, float arg1, int arg2) {
- }
- @Override
- public void onPageScrollStateChanged(int arg0) {
- }
- }
- //提示框
- public void DisplayToast(String str) {
- Toast.makeText(this, str, Toast.LENGTH_SHORT).show();
- }
- }
- <?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。实现点击之后弹出提示框的效果... 请问应该怎么处理呢。
解决方法
/**
* 初始化数据
*/
@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;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
-
猜你在找
核心技术类目
- 个人资料
-
- 访问: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)
- 阅读排行
- (10253)
- (4931)
- (4738)
- (4440)
- (3847)
- (3695)
- (3525)
- (2198)
- (2096)
- (2036)
- 评论排行
- (5)
- (5)
- (3)
- (2)
- (1)
- (1)
- (1)
- (1)
- (0)
- (0)
- 推荐文章
- id="ad_frm_2" frameborder="0" scrolling="no" src="http://blog.csdn.net/common/ad.html?t=12&containerId=ad_commend&frmId=ad_frm_2" style="border-width: 0px; overflow: hidden; width: 194px; height: 200px;">
- 最新评论
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:οnclick="DisplayToast" 可以实现。 但我现在任然有一个问题: 如果再lay1.xml 文件里面加入一个ListView ,可以通过findViewById找到这个listview并且为其提供adapter,但如何监听这个ListView里面每一个item 的点击事件呢?不知楼主是否遇到过?
2楼 doudingchenlei 2014-01-11 11:14发表 [回复]-
-
解决了,谢谢
1楼 doudingchenlei 2014-01-10 20:32发表 [回复]-
-
你好,我遇到跟你一样的问题,然后我是菜鸟,你的解决办法我有点看不懂,那个索引是什么东西?