前一段时间我添加了Swipe Gestures(从左到右,从右到左)到我的GridView。 GridView是一个日历,当你滑动时,它应该切换到下一个或上个月。为了达到这个目的,我使用了这个类:
public class OnSwipeTouchListener implements OnTouchListener {
private final GestureDetector gestureDetector;
public OnSwipeTouchListener(Context context) {
gestureDetector = new GestureDetector(context, new GestureListener());
}
public void onSwipeLeft() {
}
public void onSwipeRight() {
}
public boolean onTouch(View v, MotionEvent event) {
return gestureDetector.onTouchEvent(event);
}
private final class GestureListener extends SimpleOnGestureListener {
private static final int SWIPE_DISTANCE_THRESHOLD = 100;
private static final int SWIPE_VELOCITY_THRESHOLD = 100;
private static final int SWIPE_MIN_DISTANCE = 100;
private static final int SWIPE_MAX_OFF_PATH = 250;
private static final int SWIPE_THRESHOLD_VELOCITY = 200;
@Override
public boolean onDown(MotionEvent e) {
return true;
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2,
float velocityX, float velocityY) {
try {
float diffAbs = Math.abs(e1.getY() - e2.getY());
float diff = e1.getX() - e2.getX();
if (diffAbs > SWIPE_MAX_OFF_PATH)
return false;
// Left swipe
if (diff > SWIPE_MIN_DISTANCE
&& Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
onSwipeLeft();
// Right swipe
} else if (-diff < SWIPE_MIN_DISTANCE
&& Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
onSwipeRight();
}
} catch (Exception e) {
Log.e("OnSwipeTouchListener", e.getMessage());
}
return false;
}
}
}
这是我的布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:fab="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="@+id/prevMonth"
android:layout_width="50dp"
android:layout_height="35dp"
android:layout_marginLeft="15dp"
android:layout_marginTop="15dp"
android:src="@drawable/arrow_left"></ImageView>
<Button
android:id="@+id/currentMonth"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_marginLeft="25dp"
android:layout_marginRight="25dp"
android:layout_weight="0.6"
android:background="?android:attr/selectableItemBackground"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#000000"></Button>
<ImageView
android:id="@+id/nextMonth"
android:layout_width="50dp"
android:layout_height="35dp"
android:layout_marginRight="15dp"
android:layout_marginTop="15dp"
android:src="@drawable/arrow_right"></ImageView>
</LinearLayout>
<GridView
android:id="@+id/calendarheader"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:horizontalSpacing="0dp"
android:numColumns="7"
android:verticalSpacing="-5dp"
android:visibility="visible"></GridView>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<GridView
android:id="@+id/calendar"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:horizontalSpacing="0dp"
android:numColumns="7"
android:verticalSpacing="-5dp"
android:visibility="visible"></GridView>
<ImageView
android:id="@+id/keyYellow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="@+id/calendar"
android:background="@drawable/gelb_ellipse" />
<ImageView
android:id="@+id/keyGrey"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="@+id/keyYellow"
android:background="@drawable/grau_ellipse" />
<ImageView
android:id="@+id/keyGreen"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="@+id/keyGrey"
android:background="@drawable/gruen_ellipse" />
<ImageView
android:id="@+id/test"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/keyYellowExplanation"
android:layout_below="@+id/calendar"
android:background="@drawable/gruen_ellipse" />
<TextView
android:id="@+id/testText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/test"
android:layout_marginBottom="15dp"
android:layout_toEndOf="@+id/test"
android:layout_toRightOf="@+id/test"
android:text="Restmüll & Altpapier"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textSize="12dp"/>
<ImageView
android:id="@+id/test2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/keyGreenExplanation"
android:layout_below="@+id/keyYellowExplanation"
android:background="@drawable/gruen_ellipse" />
<TextView
android:id="@+id/testText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/test2"
android:layout_marginBottom="15dp"
android:layout_toEndOf="@+id/test2"
android:layout_toRightOf="@+id/test2"
android:text="Restmüll & Altpapier"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textSize="12dp"/>
<TextView
android:id="@+id/keyYellowExplanation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/keyYellow"
android:layout_marginBottom="15dp"
android:layout_toEndOf="@+id/keyYellow"
android:layout_toRightOf="@+id/keyYellow"
android:text="Gelber Sack & Biomüll"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textSize="12dp"/>
<TextView
android:id="@+id/keyGreenExplanation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/keyGreen"
android:layout_marginBottom="15dp"
android:layout_toEndOf="@+id/keyGrey"
android:layout_toRightOf="@+id/keyGrey"
android:text="Restmüll & Altpapier"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textSize="12dp"/>
<TextView
android:id="@+id/keyGreyExplanation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/keyGreen"
android:layout_marginTop="15dp"
android:layout_toEndOf="@+id/keyGreen"
android:layout_toRightOf="@+id/keyGreen"
android:text="Aktueller Tag"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textSize="12dp"/>
<com.github.clans.fab.FloatingActionMenu
android:id="@+id/fabMenu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginBottom="10dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
fab:menu_animationDelayPerItem="50"
fab:menu_backgroundColor="@android:color/transparent"
fab:menu_buttonSpacing="0dp"
fab:menu_colorNormal="#DA4336"
fab:menu_colorPressed="#E75043"
fab:menu_colorRipple="#99FFFFFF"
fab:menu_fab_hide_animation="@anim/my_hide_animation"
fab:menu_fab_label="your_label_here"
fab:menu_fab_show_animation="@anim/my_show_animation"
fab:menu_fab_size="normal"
fab:menu_icon="@drawable/fab_add"
fab:menu_labels_colorNormal="#333333"
fab:menu_labels_colorPressed="#444444"
fab:menu_labels_colorRipple="#66FFFFFF"
fab:menu_labels_cornerRadius="3dp"
fab:menu_labels_ellipsize="none"
fab:menu_labels_hideAnimation="@anim/fab_slide_out_to_right"
fab:menu_labels_margin="0dp"
fab:menu_labels_maxLines="-1"
fab:menu_labels_padding="8dp"
fab:menu_labels_paddingBottom="4dp"
fab:menu_labels_paddingLeft="8dp"
fab:menu_labels_paddingRight="8dp"
fab:menu_labels_paddingTop="4dp"
fab:menu_labels_position="left"
fab:menu_labels_showAnimation="@anim/fab_slide_in_from_right"
fab:menu_labels_showShadow="true"
fab:menu_labels_singleLine="false"
fab:menu_labels_style="@style/YourCustomLabelsStyle"
fab:menu_labels_textColor="#FFFFFF"
fab:menu_labels_textSize="14sp"
fab:menu_openDirection="up"
fab:menu_shadowColor="#66000000"
fab:menu_shadowRadius="4dp"
fab:menu_shadowXOffset="1dp"
fab:menu_shadowYOffset="3dp"
fab:menu_showShadow="true">
<com.github.clans.fab.FloatingActionButton
android:id="@+id/alarmForAllDates"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_insert_invitation_white_24dp"
app:fab_label="Erinnerungen komplett"
app:fab_size="mini" />
<com.github.clans.fab.FloatingActionButton
android:id="@+id/alarmForCurrentMonth"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_insert_invitation_white_24dp"
app:fab_label="Erinnerungen akt. Monat"
app:fab_size="mini" />
<com.github.clans.fab.FloatingActionButton
android:id="@+id/deleteAllAlarms"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_do_not_disturb_on_white_24dp"
app:fab_label="Erinnerungen löschen"
app:fab_size="mini" />
</com.github.clans.fab.FloatingActionMenu>
</RelativeLayout>
</FrameLayout>
</LinearLayout>
这样就可以继续工作,因为onFling方法中的e1变量始终为null。我已经阅读了一些处理相同问题的stackoverflow帖子,但我没有找到解决方案。有没有解决方案在我的GridView中实现滑动手势?
亲切的问候