java - 滑动回收站 View 无法正常工作

标签 java android android-recyclerview

我想对我的回收 View 作为一个整体(而不是单个项目行)实现滑动。 我尝试过在触摸监听器自定义类上使用滑动手势,但仍然无法顺利工作。 有时它工作正常,但大多数时候,即使我从右向左滑动,它也会从左向右滑动。 下面是我的自定义类代码。我已经在我的 fragment 类中为回收 View 实现了它。 抱歉,我无法发布我的整个代码,因为我不应该这样做。任何人都可以帮我吗? ?

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);     

    }

}``` 

最佳答案

首先,您可以使用新的导航手势,请查看此链接:

https://developer.android.com/training/gestures/gesturenav

另一方面,为什么不将 recyclerView 放入 viewPager 中,它将处理滑动并且您的 View 将以良好的形状滑动

您可以将 recyclerView 放入 View 或 fragment 内,然后您可以使用此工作流程执行您想要的操作

关于java - 滑动回收站 View 无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60863485/

相关文章:

android - View 中的 Xamarin Android EggsToGo(滑动手势)监听器不适用于 ListView

android - Recycler View 未在 Activity 中显示任何内容

android - 当我滚动或添加新项目时,RecyclerView 数据移动到其他项目

java - 在android中使用 Intent 与多个图像共享文本

java - java中句子中前三个字母的正则表达式

android - 在 Android 上 React Native,使用获取的 ajax 调用失败

android - 在 RecyclerView 适配器中获取 TextView 行数?

java - 如何使用 NetBeans 处理通过 Java 中的 New JFrame 创建的框架?

java - 如何将字符串值转换为数组?

android - Android 中的应用内结算。具体方法?