【问题标题】:Recyclerview Swipes + CardView TouchRecyclerview 刷卡 + CardView 触摸
【发布时间】:2017-07-18 11:25:13
【问题描述】:

您可以在下面看到使用 CardView 在 Recyclerview 上滑动时存在一些问题的工作应用

如您所见,我需要在整个 Recyclerview 上捕获向左/向右滑动,并在 Recyclerview Card 项目上捕获 Click 事件。在我的 Recyclerview 中没有项目的情况下,滑动效果很好,但是当 Recyclerview 包含项目时,卡项目上的 Click 事件会阻止每次滑动。 如何让它正确,这样我就可以在整个 Recyclerview 上向左/向右滑动并点击卡片项目?

这是布局代码

<android.support.v4.widget.SwipeRefreshLayout
    android:id="@+id/swipe_refresh"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@id/toolbar_layout"
    android:layout_marginBottom="50dp"
    android:layout_marginTop="61dp">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/workout_list_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

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

<com.mikesu.horizontalexpcalendar.HorizontalExpCalendar
    android:id="@+id/calendar"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginTop="50dp"
    exp:center_container_expanded_height="318dp" />  

活动代码

workoutListAdapter = new WorkoutListAdapter(workoutList, this, this);
    RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
    rvActivities.setHasFixedSize(true);
    rvActivities.setItemAnimator(new DefaultItemAnimator());
    rvActivities.setAdapter(workoutListAdapter);
    rvActivities.setLayoutManager(mLayoutManager);
    rvActivities.setOnTouchListener(new OnSwipeTouchListener(this) {
        @Override
        public void onSwipeLeft() {
            DateTime dateTime = selectedDate.plusDays(1);
            calendar.scrollToDate(dateTime, true);
        }

        @Override
        public void onSwipeRight() {
            DateTime dateTime = selectedDate.minusDays(1);
            calendar.scrollToDate(dateTime, true);
        }
    });  

和 OnSwipeTouchListener 代码

public class OnSwipeTouchListener implements View.OnTouchListener {

private final GestureDetector gestureDetector;

public OnSwipeTouchListener(Context ctx) {
    gestureDetector = new GestureDetector(ctx, new GestureListener());
}

@Override
public boolean onTouch(View v, MotionEvent event) {
    return gestureDetector.onTouchEvent(event);
}

private final class GestureListener extends GestureDetector.SimpleOnGestureListener {

    private static final int SWIPE_THRESHOLD = 300;
    private static final int SWIPE_VELOCITY_THRESHOLD = 200;

    @Override
    public boolean onDown(MotionEvent e) {
        return true;
    }

    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
        boolean result = false;
        try {
            float diffY = e2.getY() - e1.getY();
            float diffX = e2.getX() - e1.getX();
            if (Math.abs(diffX) > Math.abs(diffY)) {
                if (Math.abs(diffX) > SWIPE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) {
                    if (diffX > 0) {
                        onSwipeRight();
                    } else {
                        onSwipeLeft();
                    }
                }
                result = true;
            }
        } catch (Exception exception) {
            exception.printStackTrace();
        }
        return result;
    }
}

public void onSwipeRight() {
}

public void onSwipeLeft() {
}

public void onSwipeTop() {
}

public void onSwipeBottom() {
}

}

而且在未来我还需要在卡片项目上添加上下文菜单,会不会像点击事件一样的问题,这会阻止滑动操作?

【问题讨论】:

    标签: android android-recyclerview swipe onitemclicklistener


    【解决方案1】:

    这是我的解决方案

    OnSwipeTouchListener 类

    public class OnSwipeTouchListener implements View.OnTouchListener {
    
    private final GestureDetector gestureDetector;
    
    public OnSwipeTouchListener(Context ctx) {
        gestureDetector = new GestureDetector(ctx, new GestureListener());
    }
    
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        return gestureDetector.onTouchEvent(event);
    }
    
    private final class GestureListener extends GestureDetector.SimpleOnGestureListener {
    
        private static final int SWIPE_THRESHOLD = 200;
        private static final int SWIPE_VELOCITY_THRESHOLD = 200;
    
        @Override
        public boolean onDown(MotionEvent e) {
            return true;
        }
    
        @Override
        public boolean onSingleTapUp(MotionEvent e) {
            onItemTouch(e.getX(), e.getY());
            return true;
        }
    
        @Override
        public void onLongPress(MotionEvent e) {
            onItemLongTouch(e.getX(), e.getY());
        }
    
        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
            boolean result = false;
            try {
                float diffY = e2.getY() - e1.getY();
                float diffX = e2.getX() - e1.getX();
                if (Math.abs(diffX) > Math.abs(diffY)) {
                    if (Math.abs(diffX) > SWIPE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) {
                        if (diffX > 0) {
                            onSwipeRight();
                        } else {
                            onSwipeLeft();
                        }
                    } else {
                        onItemTouch(e2.getX(), e2.getY());
                    }
                    result = true;
                }
            } catch (Exception exception) {
                exception.printStackTrace();
            }
            return result;
        }
    }
    
    public void onSwipeRight() {
    }
    
    public void onSwipeLeft() {
    }
    
    public void onSwipeTop() {
    }
    
    public void onSwipeBottom() {
    }
    
    public void onItemTouch(float x, float y) {
    }
    
    public void onItemTouch() {
    }
    
    public void onItemLongTouch(float x, float y) {
    }
    
    public void onItemLongTouch() {
    }
    

    }

    在我的活动中

    recyclerView.setOnTouchListener(new OnSwipeTouchListener(this) {
            @Override
            public void onSwipeLeft() {
                DateTime dateTime = selectedDate.plusDays(1);
                calendar.scrollToDate(dateTime, true);
            }
    
            @Override
            public void onSwipeRight() {
                DateTime dateTime = selectedDate.minusDays(1);
                calendar.scrollToDate(dateTime, true);
            }
    
            @Override
            public void onItemTouch(float x, float y) {
                View view = recyclerView.findChildViewUnder(x, y);
                if (view != null && view.getTag() != null) {
                    Intent intent = new Intent(MyActivity.this, NewActivity.class);  
                    // getting clicked item position and passing Object key to new activity
                    intent.putExtra("recyclerview_item_position", mData.get(Integer.parseInt(view.getTag().toString())).getKey());
                    startActivity(intent);
                }
            }
    
            @Override
            public void onItemLongTouch(float x, float y) {
                View view = rvActivities.findChildViewUnder(x, y);
                if (view != null && view.getTag() != null) {
                    Log.d("log", "Long Touch");
                }
            }
        });  
    

    还有一件事:在 Recyclerview Adapter 的 onCreateViewHolder 方法中,将 TAG 设置为膨胀视图的当前位置。

    【讨论】:

    • 在我执行向右\向左滑动(我移动视图)后,点击和长按停止反应并且它认为我总是执行滑动,如果我只是触摸视图它会移动一点向左\右,有没有办法在用户每次从视图中抬起手指时刷新动作?
    猜你喜欢
    • 1970-01-01
    • 2015-01-13
    • 2023-04-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-23
    • 1970-01-01
    相关资源
    最近更新 更多