2017-07-18 139 views
1

下面你可以看到一些问题上Recyclerview挥笔与CardView
enter image description hereRecyclerview挥笔+ CardView触摸

工作程序正如你所看到的,我需要捉对整个Recyclerview左/右滑动和点击事件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() { 
} 

}

而且对未来我还需要在Card项目上添加contextmenu,它是否会像Click事件一样,会阻止滑动操作?

回答

0

这是我的解决方案

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适配器onCreateViewHolder方法集TAG到infl的当前位置视图。