ViewPager详解(一)——ViewPager的基本使用完整示例

简介: MainActivity如下: package cn.ww;import java.lang.reflect.Field;import android.

MainActivity如下:

package cn.ww;

import java.lang.reflect.Field;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.EdgeEffectCompat;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
/**
 * 
 * @author http://blog.csdn.net/lfdfhl
 * 
 * Demo描述:
 * 1 ViewPager的基本使用完整示例
 * 2 ViewPager处于最后一页时,手指从屏幕右方滑向左边时页面跳转.
 *
 */
public class MainActivity extends Activity {
	private Context mContext;
	private ViewPager mViewPager;
    private LauncherViewPagerAdapter mViewPagerAdapter;
    private ImageView[] dotImageViews;
    private PageChangeListenerImpl mPageChangeListenerImpl;
    private LinearLayout mDotsLinearLayout;
    //以下三个变量涉及到的功能:滑动到最后一页继续滑动时关闭ViewPager
    //若不需要该功能,将此三个变量和与之相关代码注释即可
    private EdgeEffectCompat leftEdge;
    private EdgeEffectCompat rightEdge;
    private boolean misScrolled = false;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		//去掉状态栏
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				             WindowManager.LayoutParams.FLAG_FULLSCREEN);
		setContentView(R.layout.activity_main);
		init();
	}
	
	private void init() {
		mContext = this;
		mViewPager = (ViewPager) findViewById(R.id.guide_viewpager);
		mDotsLinearLayout = (LinearLayout) findViewById(R.id.dotsLinearLayout);
		mViewPagerAdapter = new LauncherViewPagerAdapter(mContext);
		mPageChangeListenerImpl = new PageChangeListenerImpl();
		mViewPager.setAdapter(mViewPagerAdapter);
		mViewPager.setOnPageChangeListener(mPageChangeListenerImpl);
		try {
			Field leftEdgeField = mViewPager.getClass().getDeclaredField("mLeftEdge");
			Field rightEdgeField = mViewPager.getClass().getDeclaredField("mRightEdge");
			if (leftEdgeField != null && rightEdgeField != null) {
				leftEdgeField.setAccessible(true);
				rightEdgeField.setAccessible(true);
				leftEdge = (EdgeEffectCompat) leftEdgeField.get(mViewPager);
				rightEdge = (EdgeEffectCompat) rightEdgeField.get(mViewPager);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		initDots();
	}
	

    //初始化小圆点
    private void initDots() {
        dotImageViews = new ImageView[mViewPagerAdapter.getCount()];
        for (int i = 0; i < dotImageViews.length; i++) {
            LinearLayout layout = new LinearLayout(mContext);
            ImageView imageView = new ImageView(mContext);
            imageView.setLayoutParams(new ViewGroup.LayoutParams(20, 20));
            if (i == 0) {
                imageView.setBackgroundResource(R.drawable.guide_dot_white);
            } else {
                layout.setPadding(20, 0, 0, 0);
                imageView.setBackgroundResource(R.drawable.guide_dot_black);
            }
            dotImageViews[i] = imageView;
            layout.addView(imageView);
            mDotsLinearLayout.addView(layout);
        }
    }
    
    private class PageChangeListenerImpl implements ViewPager.OnPageChangeListener {
        @Override
        public void onPageSelected(int selected) {
            for (int i = 0; i < dotImageViews.length; i++) {
                dotImageViews[selected].setBackgroundResource(R.drawable.guide_dot_white);
                if (selected != i) {
                    dotImageViews[i].setBackgroundResource(R.drawable.guide_dot_black);
                }
            }
        }

        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {
            if (leftEdge != null && rightEdge != null) {
                leftEdge.finish();
                rightEdge.finish();
                leftEdge.setSize(0, 0);
                rightEdge.setSize(0, 0);
            }
        }

        @Override
        public void onPageScrollStateChanged(int state) {
            switch (state) {
                case ViewPager.SCROLL_STATE_DRAGGING:
                    misScrolled = false;
                    break;
                case ViewPager.SCROLL_STATE_SETTLING:
                    misScrolled = true;
                    break;
                case ViewPager.SCROLL_STATE_IDLE:
                    if (mViewPager.getCurrentItem() == mViewPager.getAdapter().getCount() - 1 && !misScrolled) {
                        //页面跳转的逻辑
                    	finish();
                    }
                    misScrolled = true;
                    break;
            }
        }

    }
    
    @Override
    protected void onDestroy() {
    	super.onDestroy();
    	if(null!=mViewPager){
    		mViewPager.removeAllViews();
            mViewPager = null;
    	}
    }
	
}

LauncherViewPagerAdapter如下:

package cn.ww;

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;

public class LauncherViewPagerAdapter extends PagerAdapter {
	private Context mContext;
	private int[] pagesArray = {R.drawable.a,R.drawable.b, R.drawable.c, R.drawable.d};

	public LauncherViewPagerAdapter(Context context) {
		this.mContext = context;
	}

	@Override
	public int getCount() {
		return pagesArray.length;
	}

	@Override
	public Object instantiateItem(View container, int position) {
		View itemView = LayoutInflater.from(mContext).inflate(R.layout.guide_pager_adapter, null);
		itemView.setFocusable(true);
		ImageView imageView = (ImageView) itemView.findViewById(R.id.imageView);
		imageView.setBackgroundResource(pagesArray[position]);
		((ViewPager) container).addView(itemView);
		return itemView;
	}


	@Override
	public boolean isViewFromObject(View arg0, Object arg1) {
		return arg0 == arg1;
	}

	@Override
	public void destroyItem(View container, int position, Object object) {
		((ViewPager) container).removeView((View) object);
	}
}

activity_main如下:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v4.view.ViewPager
        android:id="@+id/guide_viewpager"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <LinearLayout
        android:id="@+id/dotsLinearLayout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_marginBottom="100px"
        android:layout_centerHorizontal="true"
        android:orientation="horizontal">
    </LinearLayout>
    


</RelativeLayout>

guide_pager_adapter如下:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</RelativeLayout>


相关文章
|
6月前
|
Android开发 容器
35. 【Android教程】视频页面:ViewPager
35. 【Android教程】视频页面:ViewPager
63 3
|
Android开发
两种方法,教你解决 ViewPager 嵌套 ViewPager滑动冲突(一)
两种方法,教你解决 ViewPager 嵌套 ViewPager滑动冲突
|
Android开发 UED 开发者
两种方法,教你解决 ViewPager 嵌套 ViewPager滑动冲突(二)
两种方法,教你解决 ViewPager 嵌套 ViewPager滑动冲突
|
缓存 Android开发
ViewPager的简单使用
本节带来的是Android 3.0后引入的一个UI控件——ViewPager(视图滑动切换工具),实在想不到如何来称呼这个控件,他的大概功能:通过手势滑动可以完成View的切换,一般是用来做APP的引导页或者实现图片轮播,因为是3.0后引入的,如果想在低版本下使用,就需要引入v4兼容包,我们也可以看到,ViewPager在:android.support.v4.view.ViewPager目录下。下面我们就来学习一下这个控件的基本用法。
190 0
|
XML Java 数据格式
ViewPager基本用法
用法:数据ImageView+自定义适配器+ViewPager控件
113 0
fragment嵌套viewpager不显示
fragment嵌套viewpager不显示
196 0
ViewPager(通过反射修改viewpager切换速度)
(创建于2016/11/17) import java.lang.reflect.Field; import android.content.
1126 0
|
前端开发 Android开发 容器
1-VIII--ViewPager的基本使用
零、前言 [1].ViewPager顾名思义是将若干视图一页一页的展现 [2].ViewPager和Fragment郎才女貌,天造之合,在加个TabLayout简直和睦一家人 [3].
1062 0