|
aiqing0119的专栏
新版极客头条上线,每天一大波干货 任玉刚:Android开发者的职场规划 从零练就iOS高手实战班震撼来袭 新型数据库利弊谈 分类: android 项目进阶2012-11-2016:30 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] viewplaincopy
- 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();
- }
-
-
- }
[html] viewplaincopy
- <?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;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
- 上一篇Android自定义View实现HTML图文环绕效果
- 下一篇listview设置
主题推荐viewpagerandroidxml控件猜你在找Apple Watch开发入门C语言及程序设计提高反编译Android应用Struts实战-使用SSH框架技术开发学籍管理系统网络赚钱靠谱项目推荐android view的xml属性opencv基本图像处理添加滑动条trackbar在代码中动态绑定TabHost内容的两种方法AndroidAndroidUI更新方法五利用AsyncTask更新UI线程中的wait和notify方法准备好了么? 跳吧 !更多职位尽在 CSDNJOB移动-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发表 [回复]你好,我遇到跟你一样的问题,然后我是菜鸟,你的解决办法我有点看不懂,那个索引是什么东西?发表评论
- 评论内容:
-
* 以上用户言论只代表其个人观点,不代表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 WindowsMobile Rails QEMU KDE Cassandra CloudStack FTCcoremail OPhone CouchBase 云计算 iOS6 Rackspace WebApp SpringSide Maemo Compuware 大数据 aptech PerlTornado Ruby Hibernate ThinkPHP HBase Pure Solr Angular CloudFoundry 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)
- CallingstartActivity() from outside of an Activity context requires the FLA(3847)
- POI读取word转换html(3695)
- 是谁给魏延的头上安上了反骨(3525)
- 太监娶妻是干什么用的(2198)
- 周瑜正名:实非嫉贤妒能、心胸狭隘之人(2096)
- AndroidViewPager放入多个XML如何监听其的控件(2036)
- POI读取word转换html(5)
- AndroidViewPager放入多个XML如何监听其的控件(5)
- Camera.Parameters参数(3)
- Android布局中ScrollView与ListView的冲突的最简单方法(listItem.measure(0,0))(2)
- android布局透明度设置(1)
- android中类Locale的使用(1)
- 友盟的自动更新组件(1)
- CallingstartActivity() from outside of an Activity context requires the FLA(1)
- AndroidPendingIntent(0)
- Java基础复习笔记11基本排序算法(0)
- Camera.Parameters参数菊草叶与圆企鹅: @u012175707:我就想回复这句话,没想到有人说了
- AndroidViewPager放入多个XML如何监听其的控件flying_IT: 怎么点击没效果呢
- 友盟的自动更新组件cocos2dx3: updateInfo.path出来是一串乱码。不会被用户吐槽么?
- android中类Locale的使用_LinDL: 试问Locale.CHINA和Locale.CHINESE的区别
- POI读取word转换htmlqq_24719959: 请问为什么图片不能转过来
- android获取手机通讯录zhj5979: 发现了一篇 和你这惊人的相似
- android布局透明度设置Kinny_Qin: 有帮助
- Camera.Parameters参数eking2000: @u012175707:94 94
- CallingstartActivity() from outside of an Activity context requires the FLAdeng_ta: 赞
- Camera.Parameters参数阿博3世: 你真是闲的蛋疼, 直接google翻译的吧, 你自己读一下那些句子看通不通
公司简介|招贤纳士|广告服务|银行汇款帐号|联系方式|版权声明|法律顾问|问题报告|合作伙伴|论坛反馈网站客服杂志客服微博客服webmaster@iyunv.com400-600-2320|北京创新乐知信息技术有限公司版权所有|江苏乐知网络技术有限公司提供商务支持京 ICP 证 070598 号|Copyright© 1999-2014, CSDN.NET, All Rights Reserved
|
|