博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ViewPager使用方法及子View的获取
阅读量:5269 次
发布时间:2019-06-14

本文共 4342 字,大约阅读时间需要 14 分钟。

具体不解释,直接上代码,嘿嘿.....

View Code
1 package com.demo.viewpager2;   2   3 import java.util.ArrayList;   4 import java.util.List;   5   6   7 import android.app.Activity;   8 import android.os.Bundle;   9 import android.os.Parcelable;  10 import android.support.v4.view.PagerAdapter;  11 import android.support.v4.view.ViewPager;  12 import android.view.View;  13 import android.view.View.OnClickListener;  14 import android.view.ViewGroup;  15 import android.widget.Button;  16 import android.widget.ListView;  17 import android.widget.TextView;  18 import android.widget.Toast;  19  20 public class ViewPager_2Activity extends Activity {
21 private List
listViews; 22 private ViewPager viewPager; 23 24 25 /** Called when the activity is first created. */ 26 @Override 27 public void onCreate(Bundle savedInstanceState) {
28 super.onCreate(savedInstanceState); 29 setContentView(R.layout.main); 30 listViews = new ArrayList
(); 31 listViews.add(View.inflate(getApplicationContext(), R.layout.layout_1, null)); 32 listViews.add(View.inflate(getApplicationContext(), R.layout.layout_2, null)); 33 listViews.add(View.inflate(getApplicationContext(), R.layout.layout_3, null)); 34 35 viewPager = (ViewPager) findViewById(R.id.v_Pager); 36 viewPager.setAdapter(new MyPagerAdapter(listViews)); 37 viewPager.setCurrentItem(0); 38 View view = listViews.get(0); 39 TextView textView = (TextView) view.findViewById(R.id.text_1); 40 textView.setText("10"); 41 Button button = (Button) view.findViewById(R.id.button_1); 42 button.setOnClickListener(new OnClickListener() {
43 44 public void onClick(View v) {
45 // TODO Auto-generated method stub 46 Toast.makeText(getApplicationContext(), "你点击了按钮", Toast.LENGTH_SHORT).show(); 47 } 48 }); 49 } 50 51 private class MyPagerAdapter extends PagerAdapter{
52 53 private List
mListView; 54 55 private MyPagerAdapter(List
list) {
56 // TODO Auto-generated method stub 57 this.mListView= list; 58 } 59 60 @Override 61 public void destroyItem(View arg0, int arg1, Object arg2) {
62 // TODO Auto-generated method stub 63 ((ViewGroup)arg0).removeView(mListView.get(arg1)); 64 } 65 66 @Override 67 public void finishUpdate(View arg0) {
68 // TODO Auto-generated method stub 69 70 } 71 72 @Override 73 public int getCount() {
74 // TODO Auto-generated method stub 75 return mListView.size(); 76 } 77 78 @Override 79 public Object instantiateItem(View arg0, int arg1) {
80 // TODO Auto-generated method stub 81 ((ViewGroup)arg0).addView(mListView.get(arg1), 0); 82 return mListView.get(arg1); 83 } 84 85 @Override 86 public boolean isViewFromObject(View arg0, Object arg1) {
87 // TODO Auto-generated method stub 88 return arg0==(arg1); 89 } 90 91 @Override 92 public void restoreState(Parcelable arg0, ClassLoader arg1) {
93 // TODO Auto-generated method stub 94 95 } 96 97 @Override 98 public Parcelable saveState() {
99 // TODO Auto-generated method stub 100 return null; 101 } 102 103 @Override 104 public void startUpdate(View arg0) {
105 // TODO Auto-generated method stub 106 107 } 108 109 } 110 }

main.xml代码

View Code
1 
2
6 7
11
16

其余三个布局:layout_1.xml,layout_2.xml,layout_3.xml

View Code
View Code
View Code

详细看截图

转载于:https://www.cnblogs.com/simpleceo/archive/2012/04/05/2433941.html

你可能感兴趣的文章
设计模式学习--Singleton
查看>>
webApp开发-ionic2+angular2
查看>>
刷身份证读出相关信息
查看>>
RCC_APB2Periph_AFIO--复用IO时钟的使用
查看>>
canvas背景动画
查看>>
Spring MVC 访问静态资源
查看>>
项目部署到服务器上
查看>>
Servlet里的学问(一)
查看>>
js/ajax跨域访问—jsonp的原理和实例(javascript和jquery)
查看>>
pyspider操作千万级库,pyspider在对接量级较大库的策略
查看>>
详解zabbix中文版安装部署
查看>>
[LeetCode][JavaScript]Reverse Nodes in k-Group
查看>>
JUnit 4 Vs TestNG比较
查看>>
iOS8 地图遇到的问题
查看>>
Asp.net mvc3 文件下载的实现
查看>>
构造函数的重载
查看>>
Switch结构
查看>>
CMakeList 编写规则 -1
查看>>
babel 7 简单指北
查看>>
49.Submission Details java solutions
查看>>