码迷,mamicode.com
首页 > 移动开发 > 详细

Android菜鸟的成长笔记(27)——ViewPager的使用

时间:2014-05-15 06:35:24      阅读:430      评论:0      收藏:0      [点我收藏+]

标签:android   viewpager   pageradapter   

ViewPager是Android 3.0以上可以使用的API。

一、ViewPager能干什么?

1、微信5.0中连带滑动用ViewPager可以轻松实现。

bubuko.com,布布扣

2、实现类似于新浪微博的导航引导界面。

bubuko.com,布布扣

3、实现其他导航或菜单。

二、ViewPager的使用

1、添加布局

<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"/>  
</LinearLayout>
2、加载要显示的布局文件(View)

        LayoutInflater lf = getLayoutInflater().from(this);
        view1 = lf.inflate(R.layout.layout_0, null);
        view2 = lf.inflate(R.layout.layout_1, null);
        view3 = lf.inflate(R.layout.layout_2, null);
3、创建View集合

        mListViews = new ArrayList<View>();
        mListViews.add(view1);
        mListViews.add(view2);
        mListViews.add(view3);
4、创建PagerAdapter

ViewPager的适配器是PagerAdapter,要实现PagerAdapter至少要实现以下方法:

(1)instantiateItem(ViewGroup, int) //添加

(2)destroyItem(ViewGroup, int, Object)     //删除

(3)getCount()

(4)isViewFromObject(View, Object)

    private class MyPageAdapter extends PagerAdapter {
    	private List<View> mListViews;
    	
    	public MyPageAdapter(List<View> mListViews) {
			this.mListViews = mListViews;
		}
    	
    	/**
    	 * 删除页卡
    	 */
    	@Override
    	public void destroyItem(View container, int position, Object object) {
    		((ViewPager) container).removeView(mListViews.get(position));
    	}
    	
    	
    	/**
    	 * 实例化页卡
    	 */
    	@Override
    	public Object instantiateItem(View container, int position) {
    		((ViewPager)container).addView(mListViews.get(position));
    		return mListViews.get(position);
    	}
    	
    	/**
    	 * 返回页卡的数量
    	 */
		@Override
		public int getCount() {
			return mListViews.size();
		}

		@Override
		public boolean isViewFromObject(View arg0, Object arg1) {
			return arg0 == arg1; //官方提示这样写
		}
    	
    }
5、配置适配器

mViewPager.setAdapter(new MyPageAdapter(mListViews));

全部代码如下:

package com.example.viewpagetest;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;

public class MainActivity extends Activity {
	private List<View> mListViews;
	private View view1;
	private View view2;
	private View view3;
	private ViewPager mViewPager;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        mViewPager = (ViewPager) findViewById(R.id.viewpager);
        
        //加载要显示的选项卡
        LayoutInflater lf = getLayoutInflater().from(this);
        view1 = lf.inflate(R.layout.layout_0, null);
        view2 = lf.inflate(R.layout.layout_1, null);
        view3 = lf.inflate(R.layout.layout_2, null);
      
        mListViews = new ArrayList<View>();
        mListViews.add(view1);
        mListViews.add(view2);
        mListViews.add(view3);
        
        mViewPager.setAdapter(new MyPageAdapter(mListViews));
        
    }
    
    private class MyPageAdapter extends PagerAdapter {
    	private List<View> mListViews;
    	
    	public MyPageAdapter(List<View> mListViews) {
			this.mListViews = mListViews;
		}
    	
    	/**
    	 * 删除页卡
    	 */
    	@Override
    	public void destroyItem(View container, int position, Object object) {
    		((ViewPager) container).removeView(mListViews.get(position));
    	}
    	
    	
    	/**
    	 * 实例化页卡
    	 */
    	@Override
    	public Object instantiateItem(View container, int position) {
    		((ViewPager)container).addView(mListViews.get(position));
    		return mListViews.get(position);
    	}
    	
    	/**
    	 * 返回页卡的数量
    	 */
		@Override
		public int getCount() {
			return mListViews.size();
		}

		@Override
		public boolean isViewFromObject(View arg0, Object arg1) {
			return arg0 == arg1; //官方提示这样写
		}
    	
    }
}
各个页面代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="match_parent"
  android:layout_height="match_parent">
  <TextView 
    	android:layout_width="match_parent"
    	android:layout_height="match_parent"
    	android:text="1"
    	android:gravity="center"
    	android:textSize="120sp"
    	android:textColor="#33b5e5"
    />
</LinearLayout>
运行效果:

bubuko.com,布布扣








Android菜鸟的成长笔记(27)——ViewPager的使用,布布扣,bubuko.com

Android菜鸟的成长笔记(27)——ViewPager的使用

标签:android   viewpager   pageradapter   

原文地址:http://blog.csdn.net/dawanganban/article/details/25773891

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!