2017-03-13
原CSDN博客已弃用,文章会逐渐迁移过来。
一、最简单的ViewPager实现
(一)、创建可左右滑动的三个布局
view_first.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:textSize="20sp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="first"/>
</android.support.constraint.ConstraintLayout>
view_second.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:textSize="20sp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="second"/>
</android.support.constraint.ConstraintLayout>
view_thrid.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:textSize="20sp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="thrid"/>
</android.support.constraint.ConstraintLayout>
(二)、activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.administrator.viewpagerproject.MainActivity">
<android.support.v4.view.ViewPager
android:id="@+id/view_papger"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v4.view.ViewPager>
</android.support.constraint.ConstraintLayout>
(三)、MainActivity.java
package com.administrator.viewpagerproject;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private ViewPager mViewPager;
private List<View> mViews;
/**
*一组视图
*/
private int[] mLayoutIDs = {
R.layout.view_first,
R.layout.view_second,
R.layout.view_thrid};
private ImageView mImageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ViewPager mViewPager = findViewById(R.id.view_papger);
/**
* 一组视图放在列表里
*/
//初始化数据
mViews = new ArrayList<>();
for (int i = 0; i < mLayoutIDs.length; i++) {
View view = getLayoutInflater().inflate(mLayoutIDs[i], null);
mViews.add(view);
// mImageView = new ImageView(this);
// mImageView.setImageResource(R.mipmap.ic_launcher);
// mViews.add(mImageView);
}
mViewPager.setAdapter(mPagerAdapter);//给Adapter
}
//设置Adapter
PagerAdapter mPagerAdapter = new PagerAdapter() {
@Override
public int getCount() {
return mLayoutIDs.length;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
//按顺序给页
@Override
public Object instantiateItem(ViewGroup container, int position) {
View child = mViews.get(position);
container.addView(child);
return child;
}
//按顺序销毁页
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView(mViews.get(position));
}
};
}
二、使用ViewPager实现App引导页
添加一张图片用于测试,图片名为diglett.png
可鼠标右键另存为保存这张测试图片
(一)、创建可左右滑动的三个布局
view_first.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:textSize="20sp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="first"/>
</android.support.constraint.ConstraintLayout>
view_second.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:textSize="20sp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="second"/>
</android.support.constraint.ConstraintLayout>
view_thrid.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:textSize="20sp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="thrid"/>
</android.support.constraint.ConstraintLayout>
(二)、activity_main.xml
<?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">
<android.support.v4.view.ViewPager
android:id="@+id/view_papger"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v4.view.ViewPager>
<LinearLayout
android:id="@+id/dot_layout"
android:layout_width="120dp"
android:layout_height="30dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="30dp"
android:gravity="center"
android:orientation="horizontal">
</LinearLayout>
</RelativeLayout>
(三)、MainActivity.java
package com.administrator.viewpagerproject;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private ViewPager mViewPager;
private List<View> mViews;
/**
*一组视图
*/
private int[] mLayoutIDs = {
R.layout.view_first,
R.layout.view_second,
R.layout.view_thrid};
private ImageView mImageView;
private ViewGroup mDotViewGroup;
private List<ImageView> mDotViews = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mViewPager = findViewById(R.id.view_papger);
mDotViewGroup = findViewById(R.id.dot_layout);
/**
* 一组视图放在列表里
*/
//初始化数据
mViews = new ArrayList<>();
for (int i = 0; i < mLayoutIDs.length; i++) {
ImageView imageView = new ImageView(this);
imageView.setImageResource(R.mipmap.ic_launcher);
mViews.add(imageView);
ImageView dot = new ImageView(this);
dot.setImageResource(R.mipmap.ic_launcher);
dot.setMaxWidth(100);
dot.setMaxHeight(100);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(50,50);//相距50,50
layoutParams.leftMargin = 20;//每个点距离左边点20
dot.setLayoutParams(layoutParams);
dot.setEnabled(false);
mDotViewGroup.addView(dot);
mDotViews.add(dot);
}
//设置Adapter
mViewPager.setAdapter(mPagerAdapter);//给Adapter
mViewPager.setOffscreenPageLimit(4);//屏上的至少有几个
mViewPager.setCurrentItem(0);//控制最开始位置是第个布局,如:mViewPager.setCurrentItem(2);
setDotViews(0);//控制最开始位置是第几个点,如setDotViews(2);
mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
//当它被滚动的时候
}
@Override
public void onPageSelected(int position) {
//当它被选中的时候
setDotViews(position);
}
@Override
public void onPageScrollStateChanged(int state) {
//当它滚动的状态变化的时候
}
});
}
public void setDotViews(int position) {
for (int index = 0; index < mDotViews.size() ; index++) {
mDotViews.get(index).setImageResource(position == index ? R.mipmap.diglett:R.mipmap.ic_launcher);
}
}
PagerAdapter mPagerAdapter = new PagerAdapter() {
@Override
public int getCount() {
return mLayoutIDs.length;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
//按顺序给页
@Override
public Object instantiateItem(ViewGroup container, int position) {
View child = mViews.get(position);
container.addView(child);
return child;
}
//按顺序销毁页
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView(mViews.get(position));
}
};
}
简单效果图:
三、较好用的引导页效果,滑动结束后点击按钮跳转
activity_guide.xml
<?xml version="1.0" encoding="utf-8"?>
<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"
tools:context="com.example.nanchen.guidepagedemo.GuideActivity">
<android.support.v4.view.ViewPager
android:id="@+id/guide_vp"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v4.view.ViewPager>
<LinearLayout
android:id="@+id/guide_ll_point"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_alignParentBottom="true"
android:layout_marginBottom="40dp"
android:gravity="center_horizontal">
</LinearLayout>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/guide_ib_start"
android:src="@mipmap/btn_start"
android:layout_centerHorizontal="true"
android:layout_above="@+id/guide_ll_point"
android:background="@null"
android:visibility="gone"/>
</RelativeLayout>
GuidePagerAdapter.java
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;
import java.util.List;
/**
* Created by 南尘 on 2016/7/10.
*/
public class GuidePageAdapter extends PagerAdapter {
private List<View> viewList;
public GuidePageAdapter(List<View> viewList) {
this.viewList = viewList;
}
/**
* @return 返回页面的个数
*/
@Override
public int getCount() {
if (viewList != null){
return viewList.size();
}
return 0;
}
/**
* 判断对象是否生成界面
* @param view
* @param object
* @return
*/
@Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
/**
* 初始化position位置的界面
* @param container
* @param position
* @return
*/
@Override
public Object instantiateItem(ViewGroup container, int position) {
container.addView(viewList.get(position));
return viewList.get(position);
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView(viewList.get(position));
}
}
GuideActivity.java
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import java.util.ArrayList;
import java.util.List;
/**
* 实现首次启动的引导页面
*/
public class GuideActivity extends AppCompatActivity implements ViewPager.OnPageChangeListener{
private ViewPager vp;
private int []imageIdArray;//图片资源的数组
private List<View> viewList;//图片资源的集合
private ViewGroup vg;//放置圆点
//实例化原点View
private ImageView iv_point;
private ImageView []ivPointArray;
//最后一页的按钮
private ImageButton ib_start;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_guide);
ib_start = (ImageButton) findViewById(R.id.guide_ib_start);
ib_start.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(GuideActivity.this,MainActivity.class));
finish();
}
});
//加载ViewPager
initViewPager();
//加载底部圆点
initPoint();
}
/**
* 加载底部圆点
*/
private void initPoint() {
//这里实例化LinearLayout
vg = (ViewGroup) findViewById(R.id.guide_ll_point);
//根据ViewPager的item数量实例化数组
ivPointArray = new ImageView[viewList.size()];
//循环新建底部圆点ImageView,将生成的ImageView保存到数组中
int size = viewList.size();
for (int i = 0;i<size;i++){
iv_point = new ImageView(this);
iv_point.setLayoutParams(new ViewGroup.LayoutParams(20,20));
iv_point.setPadding(30,0,30,0);//left,top,right,bottom
ivPointArray[i] = iv_point;
//第一个页面需要设置为选中状态,这里采用两张不同的图片
if (i == 0){
iv_point.setBackgroundResource(R.mipmap.full_holo);
}else{
iv_point.setBackgroundResource(R.mipmap.empty_holo);
}
//将数组中的ImageView加入到ViewGroup
vg.addView(ivPointArray[i]);
}
}
/**
* 加载图片ViewPager
*/
private void initViewPager() {
vp = (ViewPager) findViewById(R.id.guide_vp);
//实例化图片资源
imageIdArray = new int[]{R.mipmap.guide1,R.mipmap.guide2,R.mipmap.guide3};
viewList = new ArrayList<>();
//获取一个Layout参数,设置为全屏
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT);
//循环创建View并加入到集合中
int len = imageIdArray.length;
for (int i = 0;i<len;i++){
//new ImageView并设置全屏和图片资源
ImageView imageView = new ImageView(this);
imageView.setLayoutParams(params);
imageView.setBackgroundResource(imageIdArray[i]);
//将ImageView加入到集合中
viewList.add(imageView);
}
//View集合初始化好后,设置Adapter
vp.setAdapter(new GuidePageAdapter(viewList));
//设置滑动监听
vp.setOnPageChangeListener(this);
}
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
/**
* 滑动后的监听
* @param position
*/
@Override
public void onPageSelected(int position) {
//循环设置当前页的标记图
int length = imageIdArray.length;
for (int i = 0;i<length;i++){
ivPointArray[position].setBackgroundResource(R.mipmap.full_holo);
if (position != i){
ivPointArray[i].setBackgroundResource(R.mipmap.empty_holo);
}
}
//判断是否是最后一页,若是则显示按钮
if (position == imageIdArray.length - 1){
ib_start.setVisibility(View.VISIBLE);
}else {
ib_start.setVisibility(View.GONE);
}
}
@Override
public void onPageScrollStateChanged(int state) {
}
}
总的来说实现流程比较简单。
1)先加载ViewPager,新建ImageView并添加到View集合中,然后设置Adapter并显示。
2)然后加载LinearLayout,放置原点图片,同样新建ImageView设置背景后放在圆点图集合中。
3)最后设置ViewPager的滑动监听事件,在滑动完成的监听OnPageSelected方法中,设置对应的圆点为选中,如果是最后一页,则显示按钮。