#java #android #android-studio #android-viewpager
Вопрос:
, в то время как он должен быть центрирован. Это происходит только тогда, когда я дополнительно прокручиваю первую страницу:
Кодекс жеста:
public class mainActivitySimpleOnGestureListener extends GestureDetector.SimpleOnGestureListener {
private int mViewpageX,mViewpageY,mViewpageWidth,mViewpageHeight;
public void setViewPagePosition(int mViewpageWidth,int mViewpageHeight ){
this.mViewpageWidth = mViewpageWidth*7/10;
this.mViewpageHeight = mViewpageHeight*7/10;
this.mViewpageX = (mViewpageWidth /2)-(mViewpageWidth*7/10/2 );
this.mViewpageY = (mViewpageHeight /2)-(mViewpageHeight*7/10/2 );;
}
public static void setGestureDetectorTouchSlop(GestureDetector gestureDetector, int value) {
try {
Field f_mTouchSlopSquare = GestureDetector.class.getDeclaredField("mTouchSlopSquare");
f_mTouchSlopSquare.setAccessible(true);
f_mTouchSlopSquare.setInt(gestureDetector, value * value);
} catch (NoSuchFieldException | IllegalAccessException | NullPointerException e) {
Log.w(TAG, gestureDetector.toString(), e);
}
}
public static void setGestureDetectorTouchSlop(GestureDetectorCompat gestureDetector, int value) {
try {
Field f_mImpl = GestureDetectorCompat.class.getDeclaredField("mImpl");
f_mImpl.setAccessible(true);
Object mImpl = f_mImpl.get(gestureDetector);
if (mImpl == null) {
Log.w(TAG, f_mImpl " is null");
return;
}
Class<?> c_GDCIJellybeanMr2 = null;
Class<?> c_GDCIBase = null;
try {
c_GDCIJellybeanMr2 = Class.forName(GestureDetectorCompat.class.getName() "$GestureDetectorCompatImplJellybeanMr2");
c_GDCIBase = Class.forName(GestureDetectorCompat.class.getName() "$GestureDetectorCompatImplBase");
} catch (ClassNotFoundException ignored) {
}
if (c_GDCIJellybeanMr2 != null amp;amp; c_GDCIJellybeanMr2.isInstance(mImpl)) {
Field f_mDetector = c_GDCIJellybeanMr2.getDeclaredField("mDetector");
f_mDetector.setAccessible(true);
Object mDetector = f_mDetector.get(mImpl);
if (mDetector instanceof GestureDetector)
setGestureDetectorTouchSlop((GestureDetector) mDetector, value);
} else if (c_GDCIBase != null) {
Field f_mTouchSlopSquare = c_GDCIBase.getDeclaredField("mTouchSlopSquare");
f_mTouchSlopSquare.setAccessible(true);
f_mTouchSlopSquare.setInt(mImpl, value * value);
} else {
Log.w(TAG, "not handled: " mImpl.getClass().toString());
}
} catch (NoSuchFieldException | IllegalAccessException | NullPointerException e) {
Log.w(TAG, gestureDetector.getClass().toString(), e);
}
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
// Logger.v("onFling " "x: " e1.getX() " y: " e1.getY() "x: " e2.getX() " y: " e2.getY() );
JudgeSendEvent("onFling",e1,e2);
return super.onFling(e1, e2, velocityX, velocityY);
}
@Override
public boolean onSingleTapConfirmed(MotionEvent e) {
return super.onSingleTapConfirmed(e);
}
@Override
public boolean onDown(MotionEvent e) {
// Logger.v("onDown " "x: " e.getX() " y: " e.getY() );
JudgeSendEvent("onDown",e,null);
return super.onDown(e);
}
//Determine whether to issue an event
private void JudgeSendEvent(String action,MotionEvent e1, MotionEvent e2) {
switch (action) {
case "onFling":
if (e1.getX() < mViewpageX amp;amp; e1.getY() > mViewpageY amp;amp; e1.getY() < (mViewpageY mViewpageHeight)) {
if (e2.getX() - e1.getX() > 20) {
// Logger.v(" 向右滑动 ");
EventBus.getDefault().post(new slideEvent(MotionEvent.ACTION_MOVE, "right")); // Send a message
} else if (e1.getX() - e2.getX() > 20) {
// Logger.v(" 向左滑动 ");
EventBus.getDefault().post(new slideEvent(MotionEvent.ACTION_MOVE, "left"));
}
} else if (e1.getX() > (mViewpageX mViewpageWidth) amp;amp; e1.getY() > mViewpageY amp;amp; e1.getY() < (mViewpageY mViewpageHeight)) {
if (e2.getX() - e1.getX() > 20) {
EventBus.getDefault().post(new slideEvent(MotionEvent.ACTION_MOVE, "right"));
// Logger.v(" 向右滑动 ");
} else if (e1.getX() - e2.getX() > 20) {
// Logger.v(" 向左滑动 ");
EventBus.getDefault().post(new slideEvent(MotionEvent.ACTION_MOVE, "left"));
}
}
break;
case "onDown":
// Logger.v( " onDown " e1.getX() " " e1.getY()
// "n mViewpageX " mViewpageX " mViewpageWidth " mViewpageWidth
// "n mViewpageY " mViewpageY " mViewpageWidth " mViewpageHeight);
if (e1.getX() < mViewpageX amp;amp; e1.getY() > mViewpageY amp;amp; e1.getY() < (mViewpageY mViewpageHeight)) {
// Logger.v( " onDown left " );
EventBus.getDefault().post(new slideEvent(MotionEvent.ACTION_DOWN, "left"));
} else if (e1.getX() > (mViewpageX mViewpageWidth) amp;amp; e1.getY() > mViewpageY amp;amp; e1.getY() < (mViewpageY mViewpageHeight)) {
EventBus.getDefault().post(new slideEvent(MotionEvent.ACTION_DOWN, "right"));
//Logger.v( " onDown right " );
}
break;
}
}
}
The zoomchange
method:
private void zoomchange() {
Log.d("before","enlarge " thewidth " ---- " dm2.widthPixels);
Log.d("before","enlarge " theheight " ---- " dm2.heightPixels);
if (dm2.widthPixels - mViewPager.getWidth() < 5) {
mViewPager.setPageMargin(fragConst.page_interval);
// Logger.v("缩小 " thewidth);
llayoutviewpage.setGravity(CENTER_IN_PARENT|CENTER_HORIZONTAL|CENTER_VERTICAL);
//缩小
PropertyValuesHolder pvhX = PropertyValuesHolder.ofFloat("scaleX", 0.9f, 0.8f);
PropertyValuesHolder pvhY = PropertyValuesHolder.ofFloat("scaleY", 1f, 0.8f);
ObjectAnimator scale = ObjectAnimator.ofPropertyValuesHolder(mViewPager, pvhX, pvhY);
scale.setDuration(100);
scale.start();
RelativeLayout.LayoutParams Rlparam = new RelativeLayout.LayoutParams(dm2.widthPixels * 8 / 10, dm2.heightPixels * 8 / 10);
Rlparam.addRule(CENTER_IN_PARENT);
llayoutviewpage.setLayoutParams(Rlparam);
mainSimpleOnGestureListener.setViewPagePosition(mViewPager.getWidth(), mViewPager.getHeight());
pagebarlt.setVisibility(VISIBLE);
EventBus.getDefault().post(new zoomEvent(true));
EventBus.clearCaches();
} else {
Log.d("test","enlarge " thewidth " ---- " dm2.widthPixels);
Log.d("test","enlarge " thewidth " ---- " dm2.heightPixels);
mViewPager.setPageMargin(0);
llayoutviewpage.setGravity(CENTER_IN_PARENT|CENTER_HORIZONTAL|CENTER_VERTICAL);
//Zoom in
PropertyValuesHolder pvhX = PropertyValuesHolder.ofFloat("scaleX", 1f, 1f);
PropertyValuesHolder pvhY = PropertyValuesHolder.ofFloat("scaleY", 1f, 1f);
ObjectAnimator scale = ObjectAnimator.ofPropertyValuesHolder(mViewPager, pvhX, pvhY);
scale.setDuration(50);
scale.start();
currentIsFull = true;
RelativeLayout.LayoutParams Rlparam2 = new RelativeLayout.LayoutParams(dm2.widthPixels, dm2.heightPixels);
Rlparam2.addRule(CENTER_IN_PARENT|CENTER_HORIZONTAL|CENTER_VERTICAL);
llayoutviewpage.setLayoutParams(Rlparam2);
pagebarlt.setVisibility(INVISIBLE);
EventBus.getDefault().post(new zoomEvent(true));
}
}
Кроме того, я не могу открыть фрагмент при последующем нажатии на фрагмент zoomchange
. Я могу сделать это только с помощью кнопки, которая находится в режиме основной активности. Как я могу прикоснуться к этому виду?
Почему это происходит? Я использую жест для пролистывания фрагментов.