The method of ViewPage to judge the left and right sliding direction in Android programming
- 2020-10-07 18:52:24
- OfStack
This paper describes the ViewPage programming Android to determine the left and right sliding direction of the method. To share for your reference, the details are as follows:
package com.meityitian.app.views;
import android.content.Context;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.util.Log;
import com.meityitian.app.utils.Debug;
/**
* Overrides, adding a method to determine the direction of the slide
* @author zxy
*
*/
public class meityitianViewPager extends ViewPager {
private boolean left = false;
private boolean right = false;
private boolean isScrolling = false;
private int lastValue = -1;
private ChangeViewCallback changeViewCallback = null;
public meityitianViewPager(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public meityitianViewPager(Context context) {
super(context);
init();
}
/**
* init method .
*/
private void init() {
setOnPageChangeListener(listener);
}
/**
* listener ,to get move direction .
*/
public OnPageChangeListener listener = new OnPageChangeListener() {
@Override
public void onPageScrollStateChanged(int arg0) {
if (arg0 == 1) {
isScrolling = true;
} else {
isScrolling = false;
}
Debug.infoByTag("meityitianViewPager",
"meityitianViewPager onPageScrollStateChanged : arg0:"
+ arg0);
if (arg0 == 2) {
Debug.infoByTag("meityitianViewPager",
"meityitianViewPager onPageScrollStateChanged direction left ? "
+ left);
Debug.infoByTag("meityitianViewPager",
"meityitianViewPager onPageScrollStateChanged direction right ? "
+ right);
//notify ....
if(changeViewCallback!=null){
changeViewCallback.changeView(left, right);
}
right = left = false;
}
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
if (isScrolling) {
if (lastValue > arg2) {
// Decrement, slide to the right
right = true;
left = false;
} else if (lastValue < arg2) {
// Decrement, slide to the right
right = false;
left = true;
} else if (lastValue == arg2) {
right = left = false;
}
}
Log.i("meityitianViewPager",
"meityitianViewPager onPageScrolled last :arg2 ,"
+ lastValue + ":" + arg2);
lastValue = arg2;
}
@Override
public void onPageSelected(int arg0) {
if(changeViewCallback!=null){
changeViewCallback.getCurrentPageIndex(arg0);
}
}
};
/**
* To get whether to slide to the right
* @return true For the right sliding
*/
public boolean getMoveRight(){
return right;
}
/**
* Get whether to slide to the left
* @return true Slide for left
*/
public boolean getMoveLeft(){
return left;
}
/**
* Slide status change callback
* @author zxy
*
*/
public interface ChangeViewCallback{
/**
* Switch the view ? Determined by the left and right .
* @param left
* @param right
*/
public void changeView(boolean left,boolean right);
public void getCurrentPageIndex(int index);
}
/**
* set ...
* @param callback
*/
public void setChangeViewCallback(ChangeViewCallback callback){
changeViewCallback = callback;
}
}
I hope this article has been helpful in Android programming.