あなたはCardView
でRecyclerview上の気の抜けたビールでいくつかの問題と取り組んでアプリを見ることができます以下Recyclerviewスワイプ+ CardViewタッチ
あなたが見ることができるように、私は、全体のRecyclerview上RIGHT LEFT /スワイプをキャッチし、上のイベントをクリックして必要Recyclerviewカードアイテム。スワイプは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() {
}
}
と未来であります私はカード項目にcontextmenuを追加する必要があります。スワイプアクションを防止するClickイベントと同じ問題でしょうか?