viewpager在最后一页滑动之后,跳转到主页面

viewpager在最后一页滑动之后,跳转到主页面

思路

主要有是两个监听,
一是addOnPageChangeListener();二是setOnTouchListener();

addOnPageChangeListener()主要是为了获取position(滑动到了第几页)

setOnTouchListener()主要是判断在最后一页中是否向左滑动了,然后进入主页

主要功能代码

addOnPageChangeListener();

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

}

@Override
public void onPageSelected(int position) {
currentItem = position;//获取位置,即第几页
Log.i("Guide","监听改变"+position);
}

@Override
public void onPageScrollStateChanged(int state) {

}
});

setOnTouchListener();

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
viewPager.setOnTouchListener(new View.OnTouchListener() {
float startX;
float startY;//没有用到
float endX;
float endY;//没有用到
@Override
public 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;
}
});

以下是全部代码

GuideActivity

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
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;

public class GuideActivity extends AppCompatActivity {
private static final String LOG = "GuideActivity";
int touchCount;
int currentItem;
List<Integer> imageIDList;
List<ImageView> imageViews;
ViewPager viewPager;
@Override
protected 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() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

}

@Override
public void onPageSelected(int position) {
currentItem = position;
Log.i("Guide","监听改变"+position);
}

@Override
public void onPageScrollStateChanged(int state) {

}
});
viewPager.setOnTouchListener(new View.OnTouchListener() {
float startX;
float startY;
float endX;
float endY;
@Override
public 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 {

@Override
public int getCount() {
return imageViews.size();
}

/**
* 判断当前分页是不是view
* 由于ViewPager里面的分页可以填入Fragment
*
* @param view
* @param object
* @return
*/
@Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}

/**
* 清理内存
* 从第一页滑动到第二页,此时第一页的内存应该释放
*
* @param container
* @param position
* @param object
*/
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView(imageViews.get(position));//释放滑动过后的前一页
}

/**
* 得到---->暂时是没有用的
*
* @param object
* @return
*/
@Override
public int getItemPosition(Object object) {
return super.getItemPosition(object);
}

/**
* 初始化分页
*
* @param container
* @param position
* @return
*/
@Override
public 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

1
2
3
4
5
6
7
8
9
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="1000"
android:fromXDelta="0%p"
android:toXDelta="-100%"
/>

</set>

####slide_in_right.xml

1
2
3
4
5
6
7
8
9
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="1000"
android:fromXDelta="100%p"
android:toXDelta="0"
/>

</set>

以下是动画效果

这里写图片描述