您的位置:首页 > 移动开发 > Android开发

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

2012-08-11 06:30 483 查看
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:


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();
}

}


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

回答:

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


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));


这个相当于你 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布局文件


LayoutInflater inflater = getLayoutInflater();
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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: