我想为我的循环视图实现整体的卷帘(而不是单个项目行)。我试过在触摸监听器自定义类上使用swipe手势,但仍然不能顺利工作。有时它可以正常工作,但大多数情况下,它只能从左向右滑动,即使我从右向左滑动。下面是我的自定义类代码。并且我已经在我的fragment类中为循环视图实现了它。很抱歉,我不能发布我的整个代码,因为我不应该这样做。有人能帮帮我吗??
import android.content.Context;
import android.graphics.Color;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import androidx.recyclerview.widget.RecyclerView;
import com.prolificinteractive.materialcalendarview.MaterialCalendarView;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
public class RelativeLayoutTouchListener implements View.OnTouchListener {
static final String logTag = "ActivitySwipeDetector";
public Context activity;
static final int MIN_DISTANCE = 100;// TODO change this runtime based on screen resolution. for 1920x1080 is to small the 100 distance
private float downX, downY, upX, upY;
MaterialCalendarView calendarView;
String slectedDate;
Calendar calendar;
ICalendarEventList list_interf_obcj;
// ISwipeFragment iSwipeFragment;
public RelativeLayoutTouchListener(Context mainActivity, MaterialCalendarView calendarViewes, String slectedDatesnew, Calendar calendares, ICalendarEventList list_interf_obcj, ISwipeFragment iSwipeFragment) {
this.activity = mainActivity;
this.slectedDate = slectedDatesnew;
this.calendarView = calendarViewes;
this.calendar = calendares;
this.list_interf_obcj = list_interf_obcj;
//this.iSwipeFragment = iSwipeFragment;
}
public void setDate(String slectedDatesnew) {
this.slectedDate = slectedDatesnew;
}
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN: {
downX = event.getX();
downY = event.getY();
return true;
}
case MotionEvent.ACTION_UP: {
upX = event.getX();
upY = event.getY();
float deltaX = downX - upX;
float deltaY = downY - upY;
// swipe horizontal?
if (Math.abs(deltaX) > MIN_DISTANCE) {
// left or right
if (deltaX < 0) {
// v.scrollBy(5,5);
// setDate(NewCalendarFragment.selectedDate);
onLeftToRightSwipe();
return true;
}
if (deltaX > 0) {
// v.scrollBy(5,5);
// setDate(NewCalendarFragment.selectedDate);
onRightToLeftSwipe();
return true;
}
} else {
Log.i(logTag, "Swipe was only " + Math.abs(deltaX) + " long horizontally, need at least " + MIN_DISTANCE);
// return false; // We don't consume the event
}
// swipe vertical?
if (Math.abs(deltaY) > MIN_DISTANCE) {
// top or down
if (deltaY < 0) {
this.onTopToBottomSwipe();
return true;
}
if (deltaY > 0) {
this.onBottomToTopSwipe();
return true;
}
} else {
Log.i(logTag, "Swipe was only " + Math.abs(deltaX) + " long vertically, need at least " + MIN_DISTANCE);
// return false; // We don't consume the event
}
return false; // no swipe horizontally and no swipe vertically
}// case MotionEvent.ACTION_UP:
}
return false;
}
public void onRightToLeftSwipe() {
Log.i(logTag, "RightToLeftSwipe!");
leftToRightAction(calendar, calendarView, activity, list_interf_obcj, slectedDate);
}
public void onLeftToRightSwipe() {
Log.i(logTag, "LeftToRightSwipe!");
rightToLeftAction(calendar, calendarView, activity, list_interf_obcj, slectedDate);
}
public void onTopToBottomSwipe() {
Log.i(logTag, "onTopToBottomSwipe!");
}
public void onBottomToTopSwipe() {
Log.i(logTag, "onBottomToTopSwipe!");
}
private void rightToLeftAction(Calendar calendarold, MaterialCalendarView calendarView, Context mainActivity, ICalendarEventList list_interf_obcj, String selectedDates) {
Calendar calendar = Calendar.getInstance();
SimpleDateFormat parser = null;
try {
parser = new SimpleDateFormat("MM/dd/yyyy", Locale.US);
calendar.setTime(parser.parse(selectedDates));
} catch (ParseException e) {
e.printStackTrace();
}
calendar.add(Calendar.DATE, 1);
Log.i(logTag, "RightSwipe!--> " + calendar.getTime().toString());
calendarView.setDateSelected(calendar.getTime(), true);
calendarView.setSelectedDate(calendar.getTime());
Log.i(logTag, "RightSwipe!--> " + calendar.getTime().toString());
SimpleDateFormat formated = new SimpleDateFormat("MM/dd/yyyy");
String onDateSelected_format = formated.format(calendar.getTime()).toString();
setDate(parser.format(calendar.getTime()));
calendarView.setFocusable(true);
calendarView.setCurrentDate(calendar);
new CalendarEventListTask(mainActivity, JsonUtil.CalendarListAPiJsonFormat(mainActivity, SessionStores.getSchoolId(mainActivity).toString(), onDateSelected_format, ""), Constants.CAl_LIST_TAG, list_interf_obcj, onDateSelected_format);
}
private void leftToRightAction(Calendar calendarold, MaterialCalendarView calendarView, Context mainActivity, ICalendarEventList list_interf_obcj, String selectedDates) {
Calendar calendar = Calendar.getInstance();
SimpleDateFormat parser = null;
try {
parser = new SimpleDateFormat("MM/dd/yyyy", Locale.US);
calendar.setTime(parser.parse(selectedDates));
} catch (ParseException e) {
e.printStackTrace();
}
calendar.add(Calendar.DATE, -1);
Log.i(logTag, "LeftSwipe!--> " + calendar.getTime().toString());
calendarView.setDateSelected(calendar.getTime(), true);
calendarView.setSelectedDate(calendar.getTime());
SimpleDateFormat formated = new SimpleDateFormat("MM/dd/yyyy");
String onDateSelected_format = formated.format(calendar.getTime()).toString();
setDate(parser.format(calendar.getTime()));
calendarView.setFocusable(true);
calendarView.setCurrentDate(calendar);
new CalendarEventListTask(mainActivity, JsonUtil.CalendarListAPiJsonFormat(mainActivity, SessionStores.getSchoolId(mainActivity).toString(), onDateSelected_format, ""), Constants.CAl_LIST_TAG, list_interf_obcj, onDateSelected_format);
}
}``` 发布于 2020-03-27 00:20:04
首先,您可以使用新的导航手势,请检查此链接:
https://developer.android.com/training/gestures/gesturenav
另一方面,你为什么不把你的recyclerView放在viewPager里面,它会处理滑动,你的视图就会滑动得很好
您可以将recyclerView放在视图或片段中,然后可以使用此工作流执行您想要的操作
https://stackoverflow.com/questions/60863485
复制相似问题