gpt4 book ai didi

Android viewflipper 不适用于抽屉

转载 作者:行者123 更新时间:2023-11-30 01:39:36 31 4
gpt4 key购买 nike

我正在尝试将 viewflipper 集成到已经有抽屉导航的 Activity 中,但 viewflipper 不工作,尽管抽屉导航正在工作。主题是滑动布局并显示数据。下面是我的代码XML:

<?xml version="1.0" encoding="utf-8"?>

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">

<RelativeLayout xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000"
tools:context="com.inabia.dailyayat.Activities.MainActivity">

<ViewFlipper
android:id="@+id/viewflipper"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_margin="6dp">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">

<TextView
android:id="@+id/txtayat"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:gravity="center"
android:shadowColor="#ffffff"
android:shadowDx="3"
android:shadowDy="-3"
android:shadowRadius="1.5"
android:textColor="#ffd700"
android:textSize="40sp"
android:textStyle="bold" />

</LinearLayout>

</ViewFlipper>

</RelativeLayout>

<FrameLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />

<ListView
android:id="@+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#d3d3d3"
android:cacheColorHint="#00000000"
android:choiceMode="singleChoice"
android:divider="#ffffff"
android:dividerHeight="1px" />

</android.support.v4.widget.DrawerLayout>

和java代码

private ViewFlipper viewFlipper;
private float lastX;
viewFlipper = (ViewFlipper) findViewById(R.id.viewflipper);

public boolean onTouchEvent(MotionEvent touchevent) {
switch (touchevent.getAction()) {

case MotionEvent.ACTION_DOWN:
lastX = touchevent.getX();
break;
case MotionEvent.ACTION_UP:
float currentX = touchevent.getX();

// Handling left to right screen swap.
if (lastX < currentX) {

// If there aren't any other children, just break.
if (viewFlipper.getDisplayedChild() == 1)
break;

// c.add(Calendar.DAY_OF_MONTH,-1);
// formattedDate = df.format(c.getTime());
// txt1.setText(formattedDate);

// Next screen comes in from left.
viewFlipper.setInAnimation(this, R.anim.slide_in_from_left);
// Current screen goes out from right.
viewFlipper.setOutAnimation(this, R.anim.slide_out_to_right);

// Display next screen.
viewFlipper.showNext();
}

// Handling right to left screen swap.
if (lastX > currentX) {

// If there is a child (to the left), kust break.
if (viewFlipper.getDisplayedChild() == 1)
break;

// c.add(Calendar.DAY_OF_MONTH,1);
// formattedDate = df.format(c.getTime());
// txt1.setText(formattedDate);
// Next screen comes in from right.
viewFlipper.setInAnimation(this, R.anim.slide_in_from_right);
// Current screen goes out from left.
viewFlipper.setOutAnimation(this, R.anim.slide_out_to_left);

// Display previous screen.
viewFlipper.showPrevious();
}
break;
}
return false;
}

最佳答案

尝试覆盖 dispatchTouchEvent 而不是 onTouchEvent 并使用 GestureDetector.SimpleOnGestureListener 检测设备屏幕上的所有触摸事件希望它对你有用.

代码:

自定义手势检测器.java

class CustomGestureDetector extends GestureDetector.SimpleOnGestureListener {
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
// Swipe left (next)
if (e1.getX() > e2.getX()) {
viewFlipper.setInAnimation(context, R.anim.left_in);
viewFlipper.setOutAnimation(context, R.anim.left_out);

viewFlipper.showNext();
}

// Swipe right (previous)
if (e1.getX() < e2.getX()) {
viewFlipper.setInAnimation(context, R.anim.right_in);
viewFlipper.setOutAnimation(context, R.anim.right_out);

viewFlipper.showPrevious();
}

return super.onFling(e1, e2, velocityX, velocityY);
}
@Override
public boolean onDoubleTap(MotionEvent e) {
Log.d("Tap", "Double tap");
return super.onDoubleTap(e);
}
@Override
public boolean onSingleTapUp(MotionEvent e) {

return false;
}
}

调度触摸事件:

 @Override
public boolean dispatchTouchEvent(MotionEvent ev) {
mGestureDetector.onTouchEvent(ev);
return super.dispatchTouchEvent(ev);
}

并且在您的 onCreate Activity 调用 GestureDetector 方法中,如下所示:

CustomGestureDetector customGestureDetector = new CustomGestureDetector();
mGestureDetector = new GestureDetector(this, customGestureDetector);

关于Android viewflipper 不适用于抽屉,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34675354/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com