Recyclerview 滑动 + CardView 触摸
Recyclerview Swipes + CardView Touch
您可以在下面看到工作应用程序在使用 CardView 的 Recyclerview 上滑动时出现一些问题
如您所见,我需要捕捉 LEFT/RIGHT 滑动整个 Recyclerview 和 Recyclerview Card 项目的 Click 事件。在我的 Recyclerview 中没有项目时滑动效果很好,但是当 Recyclerview 包含项目时,每次滑动都会被 Card 项目上的 Click 事件阻止。 如何正确设置,以便我可以在整个 Recyclerview 上滑动 LEFT/RIGHT 并点击卡片项目?
这是布局代码
<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" />
Activity代码
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() {
}
}
以后我还需要在 Card item 上添加 contextmenu,会不会像 Click 事件一样会阻止滑动操作?
这是我的解决方案
OnSwipeTouchListener class
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() {
}
}
在我的 activity
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 设置为膨胀视图的当前位置。
您可以在下面看到工作应用程序在使用 CardView 的 Recyclerview 上滑动时出现一些问题
如您所见,我需要捕捉 LEFT/RIGHT 滑动整个 Recyclerview 和 Recyclerview Card 项目的 Click 事件。在我的 Recyclerview 中没有项目时滑动效果很好,但是当 Recyclerview 包含项目时,每次滑动都会被 Card 项目上的 Click 事件阻止。 如何正确设置,以便我可以在整个 Recyclerview 上滑动 LEFT/RIGHT 并点击卡片项目?
这是布局代码
<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" />
Activity代码
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() {
}
}
以后我还需要在 Card item 上添加 contextmenu,会不会像 Click 事件一样会阻止滑动操作?
这是我的解决方案
OnSwipeTouchListener class
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() {
}
}
在我的 activity
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 设置为膨胀视图的当前位置。