Android viewpager detect swipe beyond the bounds(Android viewpager检测滑动超出范围)
问题描述
在我的 Android 应用程序中,我使用 viewpager 进行图像滑动.我的要求是,如果用户滑出第一页和最后一页,则活动应该完成.
In my Android application, I am using a viewpager for image swiping. My requirement is, if a user swipes out of the first and last page, the activity should finish.
我采用了这个示例一个>.但是方法 setOnSwipeOutListener
在我的活动中没有被调用.
I have taken this example. But the method setOnSwipeOutListener
is not called in my activity.
这是我的自定义视图寻呼机类:
This is my custom view pager class:
public class CustomViewPager extends ViewPager {
public CustomViewPager(Context context) {
super(context);
}
public CustomViewPager(Context context, AttributeSet attrs) {
super(context, attrs);
}
float mStartDragX;
OnSwipeOutListener mListener;
public void setOnSwipeOutListener(OnSwipeOutListener listener) {
mListener = listener;
}
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
float x = ev.getX();
switch (ev.getAction()) {
case MotionEvent.ACTION_DOWN:
mStartDragX = x;
break;
case MotionEvent.ACTION_MOVE:
if (mStartDragX < x && getCurrentItem() == 0) {
mListener.onSwipeOutAtStart();
} else if (mStartDragX > x
&& getCurrentItem() == getAdapter().getCount() - 1) {
mListener.onSwipeOutAtEnd();
}
break;
}
return super.onInterceptTouchEvent(ev);
}
public interface OnSwipeOutListener {
public void onSwipeOutAtStart();
public void onSwipeOutAtEnd();
}
}
在我的活动类下面,我在自定义 viewpager 类上调用 setOnSwipeOutListener
方法,但它没有被调用.
And below in my activity class I call setOnSwipeOutListener
method on my custom viewpager class, but it does not get called.
myPager = (CustomViewPager) findViewById(R.id.home_pannels_pager);
.......
myPager.setOnSwipeOutListener(new OnSwipeOutListener() { // the method never called
@Override
public void onSwipeOutAtStart() {
Log.e("swipe Out At Start ", "swipe out");
}
@Override
public void onSwipeOutAtEnd() {
Log.e("swipe Out At End ", "swipe end");
}
});
myPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
@Override
public void onPageSelected(int po) {
Log.e("positon", ""+po);
}
});
请帮助我检测用户是否滑动到第一页和最后一页以关闭此活动以及如何从代码中调用该方法.
Please help me to detect if the user has swiped beyond the first and last page to close this activity and how to call the method from code.
推荐答案
就我而言,onInterceptTouchEvent 不起作用.我用 onTouchEvent 改变了方法,它正在工作.原始解决方案来自 tjlian616
In my case, onInterceptTouchEvent does not work. I change the method with onTouchEvent and it is working. The original solution is from tjlian616
@Override
public boolean onTouchEvent(MotionEvent ev){
if(getCurrentItem()==getAdapter().getCount()-1){
final int action = ev.getAction();
float x = ev.getX();
switch(action & MotionEventCompat.ACTION_MASK){
case MotionEvent.ACTION_DOWN:
mStartDragX = x;
break;
case MotionEvent.ACTION_MOVE:
break;
case MotionEvent.ACTION_UP:
if (x<mStartDragX){
mListener.onSwipeOutAtEnd();
}else{
mStartDragX = 0;
}
break;
}
}else{
mStartDragX=0;
}
return super.onTouchEvent(ev);
}
这篇关于Android viewpager检测滑动超出范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Android viewpager检测滑动超出范围
基础教程推荐
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01