实现步骤
- 编写布局文件
- 编写ViewPAger适配器类
- 初始化ViewPage,设置适配器和滑动监听
- 初始化ViewPager数据源与页面矩形指示
- 对ViewPager的页面滑动事件做处理,达到矩形指示跟随手的滑动而滑动
- 实现跳转到主页
具体代码
布局文件
<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/vp"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true">
<LinearLayout
android:id="@+id/ll_rect"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingBottom="10dp"/>
<ImageView
android:id="@+id/iv_greenRect"
android:layout_width="wrap_content"
android:layo android:src="@drawable/rect_green"/>
</RelativeLayout>
</RelativeLayout>
package com.yu.viewpager02;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;
import java.util.List;
/**
* Created by pecu on 2016/07/06.
*/
public class ViewPagewAdapter<T> extends PagerAdapter {
List<T> datas;
public ViewPagewAdapter(List<T> datas) {
this.datas = datas;
}
@Override
public int getCount() {
return datas.size();
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
container.addView((View) datas.get(position));
return datas.get(position);
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View) datas.get(position));
}
}
GuideActivity 类
- 初始化ViewPage,设置适配器和滑动监听
- 初始化ViewPager数据源与页面矩形指示
- 对ViewPager的页面滑动事件做处理,达到矩形指示跟随手的滑动而滑动
- 实现跳转到主页
package com.yu.viewpager02;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import java.util.ArrayList;
import java.util.List;
public class Guide extends Activity implements ViewPager.OnPageChangeListener {
ViewPager vp;
int images[] = new int[]{R.mipmap.guide1, R.mipmap.guide2, R.mipmap.guide3};
List<View> views;
LinearLayout ll_rect;
ImageView greenRect;
int rectMargin;
Button btn_enter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
initrect();
vp = (ViewPager) findViewById(R.id.vp);
vp.setAdapter(new ViewPagewAdapter<View>(views));
vp.addOnPageChangeListener(this);
}
private void initView() {
views = new ArrayList<View>();
for (int i = 0; i < images.length; i++) {
ImageView iv = new ImageView(Guide.this);
iv.setImageResource(images[i]);
views.add(iv);
}
btn_enter = (Button) findViewById(R.id.btn_enter);
btn_enter.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(Guide.this,MainActivity.class));
finish();
}
});
}
private void initrect() {
ll_rect = (LinearLayout) findViewById(R.id.ll_rect);
for (int i = 0; i < images.length; i++) {
ImageView iv = new ImageView(Guide.this);
iv.setImageResource(R.drawable.rect_grey);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, -2);
if (i > 0) params.leftMargin = 10;
iv.setLayoutParams(params);
final int finalI = i;
iv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
vp.setCurrentItem(finalI);
}
});
ll_rect.addView(iv);
}
greenRect = (ImageView) findViewById(R.id.iv_greenRect);
greenRect.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
greenRect.getViewTreeObserver().removeOnGlobalLayoutListener(this);
rectMargin = ll_rect.getChildAt(1).getLeft() - ll_rect.getChildAt(0).getLeft();
}
});
}
/**
* 页面滑动时调用
*
* @param position
* @param positionOffset 参数在[0,1)范围 , 表示已滑动偏移比例
* @param positionOffsetPixels 滑动偏移量
*/
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
int leftMargin = (int) (position * rectMargin + positionOffset * rectMargin);
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) greenRect.getLayoutParams();
params.leftMargin = leftMargin;
greenRect.setLayoutParams(params);
}
@Override
public void onPageSelected(int position) {
if (position == images.length - 1) btn_enter.setVisibility(View.VISIBLE);
else btn_enter.setVisibility(View.GONE);
}
@Override
public void onPageScrollStateChanged(int state) {
}
}
用于绘制矩形的shape
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<size android:height="5dp" android:width="20dp"/>
<solid android:color="#0f9"/>
</shape>
----------
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<size android:height="4dp" android:width="20dp"/>
<solid android:color="#666"/>
</shape>
效果图