繁体   English   中英

刷卡回收视图无法正常工作

[英]Swipe for Recycle View doesn't work properly

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

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 里面,它会处理滑动,你的视图会以良好的形状滑动

您可以将 recyclerView 放在视图中或片段中,您可以使用此工作流执行您想要的操作

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM