转自:https://www.cnblogs.com/renhui/articles/12103367.html
一、实现思路
主要有是两个监听,
一是addOnPageChangeListener();二是setOnTouchListener();
addOnPageChangeListener()主要是为了获取position(滑动到了第几页)
setOnTouchListener()主要是判断在最后一页中是否向左滑动了,然后进入主页。
二、主要功能代码
1. addOnPageChangeListener();
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
publicvoidonPageScrolled(intposition,floatpositionOffset,int positionOffsetPixels) {
}
@Override
publicvoidonPageSelected(int position) {
currentItem = position;//获取位置,即第几页Log.i("Guide","监听改变"+position);
}
@Override
publicvoidonPageScrollStateChanged(int state) {
}
});
2. setOnTouchListener();
viewPager.setOnTouchListener(new View.OnTouchListener() {
float startX;
floatstartY;//没有用到float endX;
floatendY;//没有用到 @Override
publicboolean onTouch(View v, MotionEvent event) {
switch (event.getAction()){
case MotionEvent.ACTION_DOWN:
startX=event.getX();
startY=event.getY();
break;
case MotionEvent.ACTION_UP:
endX=event.getX();
endY=event.getY();
WindowManager windowManager= (WindowManager) getApplicationContext().getSystemService(Context.WINDOW_SERVICE);
//获取屏幕的宽度Point size =new Point();
windowManager.getDefaultDisplay().getSize(size);
intwidth=size.x;
//首先要确定的是,是否到了最后一页,然后判断是否向左滑动,并且滑动距离是否符合,我这里的判断距离是屏幕宽度的4分之一(这里可以适当控制)if(currentItem==(imageViews.size()-1)&&startX-endX>=(width/4)){
Log.i(LOG,"进入了触摸");
goToMainActivity();//进入主页overridePendingTransition(R.anim.slide_in_right,R.anim.slide_in_left);//这部分代码是切换Activity时的动画,看起来就不会很生硬 }
break;
}
returnfalse;
}
});
三、完整代码实现
GuideActivity:
package com.tc.mobileshop;import android.content.Context;import android.content.Intent;import android.graphics.Point;import android.support.v4.view.PagerAdapter;import android.support.v4.view.ViewPager;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.util.Log;import android.view.Display;import android.view.MotionEvent;import android.view.View;import android.view.ViewGroup;import android.view.WindowManager;import android.widget.ImageView;import com.tc.mobileshop.utils.DisplayUtils;import java.util.ArrayList;import java.util.List;publicclassGuideActivityextends AppCompatActivity {
privatestaticfinalString LOG = "GuideActivity";
int touchCount;
int currentItem;
List imageIDList;
List imageViews;
ViewPager viewPager;
@Override
protectedvoid onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_guide);
//初始化引导数据 initGuideData();
//初始化引导页 initGuideView();
//初始化分页控件 iniView();
}
/** * 初始化引导页数据
*/privatevoid initGuideData() {
imageIDList =new ArrayList();
imageIDList.add(R.mipmap.apk_img1);
imageIDList.add(R.mipmap.apk_img2);
imageIDList.add(R.mipmap.apk_img3);
}
/** * 初始化引导页
*/privatevoid initGuideView() {
imageViews =newArrayList<>();
for(inti = 0; i < imageIDList.size(); i++) {
imageViews.add(newImageView(this));
}
}
/** * 初始化分页控件
*/privatevoid iniView() {
viewPager = (ViewPager) findViewById(R.id.guide_pager);
viewPager.setAdapter(new GuideAdapter());
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
publicvoidonPageScrolled(intposition,floatpositionOffset,int positionOffsetPixels) {
}
@Override
publicvoidonPageSelected(int position) {
currentItem = position;
Log.i("Guide","监听改变"+position);
}
@Override
publicvoidonPageScrollStateChanged(int state) {
}
});
viewPager.setOnTouchListener(new View.OnTouchListener() {
float startX;
float startY;
float endX;
float endY;
@Override
publicboolean onTouch(View v, MotionEvent event) {
switch (event.getAction()){
case MotionEvent.ACTION_DOWN:
startX=event.getX();
startY=event.getY();
break;
case MotionEvent.ACTION_UP:
endX=event.getX();
endY=event.getY();
WindowManager windowManager= (WindowManager) getApplicationContext().getSystemService(Context.WINDOW_SERVICE);
//获取屏幕的宽度Point size =new Point();
windowManager.getDefaultDisplay().getSize(size);
intwidth=size.x;
//首先要确定的是,是否到了最后一页,然后判断是否向左滑动,并且滑动距离是否符合,我这里的判断距离是屏幕宽度的4分之一(这里可以适当控制)if(currentItem==(imageViews.size()-1)&&startX-endX>0&&startX-endX>=(width/4)){
Log.i(LOG,"进入了触摸");
goToMainActivity();
overridePendingTransition(R.anim.slide_in_right,R.anim.slide_in_left);
}
break;
}
returnfalse;
}
});
}
privatevoid goToMainActivity() {
Intent intent=newIntent(this,MainActivity.class);
startActivity(intent);
finish();
}
/** * Viewpager适配器
*/privateclassGuideAdapterextends PagerAdapter {
@Override
publicint getCount() {
return imageViews.size();
}
/** * 判断当前分页是不是view
* 由于ViewPager里面的分页可以填入Fragment
*
* @param view
* @param object
* @return*/ @Override
publicboolean isViewFromObject(View view, Object object) {
returnview == object;
}
/** * 清理内存
* 从第一页滑动到第二页,此时第一页的内存应该释放
*
* @param container
* @param position
* @param object
*/ @Override
publicvoiddestroyItem(ViewGroup container,int position, Object object) {
container.removeView(imageViews.get(position));//释放滑动过后的前一页 }
/** * 得到---->暂时是没有用的
*
* @param object
* @return*/ @Override
publicint getItemPosition(Object object) {
returnsuper.getItemPosition(object);
}
/** * 初始化分页
*
* @param container
* @param position
* @return*/ @Override
publicObject instantiateItem(ViewGroup container,int position) {
ImageView imageView = imageViews.get(position);
imageView.setImageResource(imageIDList.get(position));
ViewGroup.LayoutParams viewLayoutParams =new ViewGroup.LayoutParams
(
DisplayUtils.dip2px(GuideActivity.this, 170),
DisplayUtils.dip2px(GuideActivity.this, 200)
);
container.addView(imageView,viewLayoutParams);//设置图片的宽高return imageView;
}
}
}