【发布时间】: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