先给大家说下实现思路
主要有是两个监听:
一是addOnPageChangeListener();二是setOnTouchListener();
addOnPageChangeListener()主要是为了获取position(滑动到了第几页)
setOnTouchListener()主要是判断在最后一页中是否向左滑动了,然后进入主页
在没给大家分享代码之前,先给大家展示下效果图:
主要功能代码
addOnPageChangeListener();viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {@Overridepublic void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {}@Overridepublic void onPageSelected(int position) {currentItem = position;//获取位置,即第几页Log.i("Guide","监听改变"+position);}@Overridepublic void onPageScrollStateChanged(int state) {}}); setOnTouchListener();viewPager.setOnTouchListener(new View.OnTouchListener() {float startX;float startY;//没有用到float endX;float endY;//没有用到@Overridepublic boolean 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);int width=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;}return false;}});
以下是全部代码
GuideActivitypackage 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;public class GuideActivity extends AppCompatActivity {private static final String LOG = "GuideActivity";int touchCount;int currentItem;List<Integer> imageIDList;List<ImageView> imageViews;ViewPager viewPager;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_guide);//初始化引导数据initGuideData();//初始化引导页initGuideView();//初始化分页控件iniView();}/*** 初始化引导页数据*/private void initGuideData() {imageIDList = new ArrayList();imageIDList.add(R.mipmap.apk_img1);imageIDList.add(R.mipmap.apk_img2);imageIDList.add(R.mipmap.apk_img3);}/*** 初始化引导页*/private void initGuideView() {imageViews = new ArrayList<>();for (int i = 0; i < imageIDList.size(); i++) {imageViews.add(new ImageView(this));}}/*** 初始化分页控件*/private void iniView() {viewPager = (ViewPager) findViewById(R.id.guide_pager);viewPager.setAdapter(new GuideAdapter());viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {@Overridepublic void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {}@Overridepublic void onPageSelected(int position) {currentItem = position;Log.i("Guide","监听改变"+position);}@Overridepublic void onPageScrollStateChanged(int state) {}});viewPager.setOnTouchListener(new View.OnTouchListener() {float startX;float startY;float endX;float endY;@Overridepublic boolean 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);int width=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;}return false;}});}private void goToMainActivity() {Intent intent=new Intent(this,MainActivity.class);startActivity(intent);finish();}/*** Viewpager适配器*/private class GuideAdapter extends PagerAdapter {@Overridepublic int getCount() {return imageViews.size();}/*** 判断当前分页是不是view* 由于ViewPager里面的分页可以填入Fragment** @param view* @param object* @return*/@Overridepublic boolean isViewFromObject(View view, Object object) {return view == object;}/*** 清理内存* 从第一页滑动到第二页,此时第一页的内存应该释放** @param container* @param position* @param object*/@Overridepublic void destroyItem(ViewGroup container, int position, Object object) {container.removeView(imageViews.get(position));//释放滑动过后的前一页}/*** 得到---->暂时是没有用的** @param object* @return*/@Overridepublic int getItemPosition(Object object) {return super.getItemPosition(object);}/*** 初始化分页** @param container* @param position* @return*/@Overridepublic Object 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;}}}
以下为动画资源代码
slide_in_left.xml
<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android"><translateandroid:duration="1000"android:fromXDelta="0%p"android:toXDelta="-100%"/></set> slide_in_right.xml<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android"><translateandroid:duration="1000"android:fromXDelta="100%p"android:toXDelta="0"/></set>
以上所述是小编给大家介绍的Android viewpager在最后一页滑动之后跳转到主页面的实例代码,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对武林网网站的支持!
新闻热点
疑难解答