viewPager的简单使用

代码下载地址:http://download.csdn.net/detail/u011324501/9412552
http://download.csdn.net/detail/u011324501/9412610
添加布局文件:main.xml</span>
 
<?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.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center" >
 
        <android.support.v4.view.PagerTitleStrip
            android:id="@+id/pagertitle"
            android:layout_width="wrap_content"
            android:layout_height="30dip"
            android:layout_gravity="top" />
    </android.support.v4.view.ViewPager> 

</LinearLayout>

添加布局文件:view1.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="#00cc66"
    android:orientation="vertical" >
    
	<Button
	    android:id="@+id/btn1"
	    android:layout_width="wrap_content"
	    android:layout_height="wrap_content"/>
	<TextView
	    android:id="@+id/text"
	    android:layout_width="wrap_content"
	    android:layout_height="wrap_content"
	    android:textSize="30sp"
	    android:textColor="#000000"
	    android:text="view1"/>
</LinearLayout>

添加view2布局文件:view2.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="#ffffcc"
    android:orientation="vertical" >
    
    <Button
	    android:id="@+id/btn2"
	    android:layout_width="wrap_content"
	    android:layout_height="wrap_content"/>
	<TextView
	    android:id="@+id/text2"
	    android:layout_width="wrap_content"
	    android:layout_height="wrap_content"
	    android:textSize="30sp"
	    android:textColor="#000000"
	    android:text=""/>
</LinearLayout>
添加布局文件:view3.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="#00ccff"
    android:orientation="vertical" >
    
	 <Button
	    android:id="@+id/btn3"
	    android:layout_width="wrap_content"
	    android:layout_height="wrap_content"/>
	<TextView
	    android:id="@+id/text3"
	    android:layout_width="wrap_content"
	    android:layout_height="wrap_content"
	    android:textSize="30sp"
	    android:textColor="#000000"
	    android:text=""/>
</LinearLayout>
添加MainActivity.java
package com.example.viewpager;

import java.util.ArrayList;

import android.os.Bundle;
import android.app.Activity;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.PagerTitleStrip;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {

	private ViewPager mViewPager;
	private PagerTitleStrip mPagerTitleStrip;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		
		mViewPager = (ViewPager)findViewById(R.id.viewpager);
		mPagerTitleStrip = (PagerTitleStrip)findViewById(R.id.pagertitle);
		//上面的标题添加的监听器
		mPagerTitleStrip.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				Toast.makeText(getApplication(), "dff", Toast.LENGTH_SHORT).show();
			}
		});
		//将分页显示的View装入数组中
		LayoutInflater mInflater = LayoutInflater.from(getApplication());
		View view1 = mInflater.inflate(R.layout.view1, null);
		View view2 = mInflater.inflate(R.layout.view2, null);
		View view3 = mInflater.inflate(R.layout.view3, null);
		
		//每个页面的Title数据
		final ArrayList<View> views = new ArrayList<View>();
		views.add(view1);
		views.add(view2);
		views.add(view3);
		
		final ArrayList<String> titles = new ArrayList<String>();
		titles.add("view1");
		titles.add("view2");
		titles.add("view3");
		
		//填充viewpager的数据适配器
		PagerAdapter mPagerAdapter = new PagerAdapter() {
			
			@Override
			public boolean isViewFromObject(View arg0, Object arg1) {
				// TODO Auto-generated method stub
				return arg0 ==arg1;
			}
			
			@Override
			public int getCount() {
				// TODO Auto-generated method stub
				return views.size();
			}

			@Override
			public void destroyItem(View container, int position, Object object) {
				// TODO Auto-generated method stub
				((ViewPager)container).removeView(views.get(position));
			}

			@Override
			public CharSequence getPageTitle(int position) {
				// TODO Auto-generated method stub
				return titles.get(position);
			}

			@Override
			public Object instantiateItem(View container, int position) {
				// TODO Auto-generated method stub
				((ViewPager)container).addView(views.get(position));
				//VIEW1
				Button btn = (Button)findViewById(R.id.btn1);
				btn.setOnClickListener(new OnClickListener() {
					
					@Override
					public void onClick(View arg0) {
						// TODO Auto-generated method stub
					   TextView text = (TextView)findViewById(R.id.text);
					   text.setText("view1");
					   //VIEW2
					   Button btn2 = (Button)findViewById(R.id.btn2);
					   btn2.setOnClickListener(new OnClickListener() {
							
							@Override
							public void onClick(View arg0) {
								// TODO Auto-generated method stub
								TextView text2 = (TextView)findViewById(R.id.text2);
								text2.setText("View2");
								//VIEW3
								Button btn2 = (Button)findViewById(R.id.btn3);
								   btn2.setOnClickListener(new OnClickListener() {
										
										@Override
										public void onClick(View arg0) {
											// TODO Auto-generated method stub
											TextView text2 = (TextView)findViewById(R.id.text3);
											text2.setText("View3");
										}
								});			
								
							}
						});			   
					}
				});
				
//				
				
				return views.get(position);
			}	
			
		};
		mViewPager.setAdapter(mPagerAdapter);
	}

}


 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值