itemtouchhelper::gesture working ok

This commit is contained in:
侯歌 2024-07-31 17:56:24 +08:00
parent 72db57e6a7
commit 931a8aebda
4 changed files with 31 additions and 42 deletions

View File

@ -69,7 +69,7 @@ void GestureDetector::init(Context* context) {
if(InputEventConsistencyVerifier::isInstrumentationEnabled()) if(InputEventConsistencyVerifier::isInstrumentationEnabled())
mInputEventConsistencyVerifier = new InputEventConsistencyVerifier((Object*)this, 0) ; mInputEventConsistencyVerifier = new InputEventConsistencyVerifier((Object*)this, 0) ;
mPressRunnable = [this](){ mPressRunnable = [this](){
mListener.onShowPress(*mCurrentDownEvent); if(mListener.onShowPress)mListener.onShowPress(*mCurrentDownEvent);
}; };
mLongPressRunnable=[this](){ mLongPressRunnable=[this](){
//recordGestureClassification(msg.arg1); //recordGestureClassification(msg.arg1);

View File

@ -7035,7 +7035,7 @@ void View::playSoundEffect(int soundConstant){
} }
bool View::performHapticFeedback(int feedbackConstant, int flags){ bool View::performHapticFeedback(int feedbackConstant, int flags){
if (mAttachInfo == nullptr) { if ((mAttachInfo == nullptr)||(mAttachInfo->mPerformHapticFeedback==nullptr)) {
return false; return false;
} }
//noinspection SimplifiableIfStatement //noinspection SimplifiableIfStatement

View File

@ -9,6 +9,7 @@ ItemTouchHelper::ItemTouchHelper(Callback* callback) {
mVelocityTracker = nullptr; mVelocityTracker = nullptr;
mSwipeEscapeVelocity = 120; mSwipeEscapeVelocity = 120;
mMaxSwipeVelocity = 800; mMaxSwipeVelocity = 800;
mGestureDetector = nullptr;
mScrollRunnable = [this]() { mScrollRunnable = [this]() {
if ((mSelected != nullptr) && scrollIfNecessary()) { if ((mSelected != nullptr) && scrollIfNecessary()) {
if (mSelected != nullptr) { //it might be lost during scrolling if (mSelected != nullptr) { //it might be lost during scrolling
@ -27,7 +28,7 @@ ItemTouchHelper::ItemTouchHelper(Callback* callback) {
} }
bool ItemTouchHelper::onInterceptTouchEvent(RecyclerView& recyclerView,MotionEvent& event) { bool ItemTouchHelper::onInterceptTouchEvent(RecyclerView& recyclerView,MotionEvent& event) {
//mGestureDetector.onTouchEvent(event); mGestureDetector->onTouchEvent(event);
LOGD_IF(_DEBUG,"intercept: x:%.f ,y:%.f",event.getX(),event.getY()); LOGD_IF(_DEBUG,"intercept: x:%.f ,y:%.f",event.getX(),event.getY());
const int action = event.getActionMasked(); const int action = event.getActionMasked();
if (action == MotionEvent::ACTION_DOWN) { if (action == MotionEvent::ACTION_DOWN) {
@ -69,7 +70,7 @@ bool ItemTouchHelper::onInterceptTouchEvent(RecyclerView& recyclerView,MotionEve
} }
void ItemTouchHelper::onTouchEvent(RecyclerView& recyclerView,MotionEvent& event) { void ItemTouchHelper::onTouchEvent(RecyclerView& recyclerView,MotionEvent& event) {
//mGestureDetector.onTouchEvent(event); mGestureDetector->onTouchEvent(event);
LOGD_IF(_DEBUG,"on touch: x:%d,y:%d",mInitialTouchX,mInitialTouchY); LOGD_IF(_DEBUG,"on touch: x:%d,y:%d",mInitialTouchX,mInitialTouchY);
if (mVelocityTracker != nullptr) { if (mVelocityTracker != nullptr) {
mVelocityTracker->addMovement(event); mVelocityTracker->addMovement(event);
@ -179,19 +180,18 @@ void ItemTouchHelper::destroyCallbacks() {
} }
void ItemTouchHelper::startGestureDetection() { void ItemTouchHelper::startGestureDetection() {
/*mItemTouchHelperGestureListener = new ItemTouchHelperGestureListener(); mItemTouchHelperGestureListener.onDown=std::bind(&ItemTouchHelper::onGestureDown,this,std::placeholders::_1);
mGestureDetector = new GestureDetectorCompat(mRecyclerView->getContext(), mItemTouchHelperGestureListener.onLongPress=std::bind(&ItemTouchHelper::onGestureLongPress,this,std::placeholders::_1);
mItemTouchHelperGestureListener);*/ mGestureDetector = new GestureDetector(mRecyclerView->getContext(),mItemTouchHelperGestureListener);
} }
void ItemTouchHelper::stopGestureDetection() { void ItemTouchHelper::stopGestureDetection() {
/*if (mItemTouchHelperGestureListener != null) { if (mItemTouchHelperGestureListener.onDown||mItemTouchHelperGestureListener.onLongPress) {
mItemTouchHelperGestureListener.doNotReactToLongPress(); doNotReactToLongPress();
mItemTouchHelperGestureListener = null; mItemTouchHelperGestureListener ={};
} }
if (mGestureDetector != null) { delete mGestureDetector;
mGestureDetector = null; mGestureDetector = nullptr;
}*/
} }
void ItemTouchHelper::getSelectedDxDy(float outPosition[2]) { void ItemTouchHelper::getSelectedDxDy(float outPosition[2]) {
@ -1222,42 +1222,39 @@ int ItemTouchHelper::SimpleCallback::getMovementFlags(RecyclerView& recyclerView
getSwipeDirs(recyclerView, viewHolder)); getSwipeDirs(recyclerView, viewHolder));
} }
ItemTouchHelper::ItemTouchHelperGestureListener::ItemTouchHelperGestureListener(ItemTouchHelper*hlp):mItemTouchHelper(hlp) { void ItemTouchHelper::doNotReactToLongPress() {
}
void ItemTouchHelper::ItemTouchHelperGestureListener::doNotReactToLongPress() {
mShouldReactToLongPress = false; mShouldReactToLongPress = false;
} }
bool ItemTouchHelper::ItemTouchHelperGestureListener::onDown(MotionEvent& e) { bool ItemTouchHelper::onGestureDown(MotionEvent& e) {
return true; return true;
} }
void ItemTouchHelper::ItemTouchHelperGestureListener::onLongPress(MotionEvent& e) { void ItemTouchHelper::onGestureLongPress(MotionEvent& e) {
if (!mShouldReactToLongPress) { if (!mShouldReactToLongPress) {
return; return;
} }
View* child = mItemTouchHelper->findChildView(e); View* child = findChildView(e);
if (child != nullptr) { if (child != nullptr) {
RecyclerView::ViewHolder* vh = mItemTouchHelper->mRecyclerView->getChildViewHolder(child); RecyclerView::ViewHolder* vh = mRecyclerView->getChildViewHolder(child);
if (vh != nullptr) { if (vh != nullptr) {
if (!mItemTouchHelper->mCallback->hasDragFlag(*mItemTouchHelper->mRecyclerView,*vh)) { if (!mCallback->hasDragFlag(*mRecyclerView,*vh)) {
return; return;
} }
int pointerId = e.getPointerId(0); int pointerId = e.getPointerId(0);
// Long press is deferred. // Long press is deferred.
// Check w/ active pointer id to avoid selecting after motion // Check w/ active pointer id to avoid selecting after motion
// event is canceled. // event is canceled.
if (pointerId == mItemTouchHelper->mActivePointerId) { if (pointerId == mActivePointerId) {
const int index = e.findPointerIndex(mItemTouchHelper->mActivePointerId); const int index = e.findPointerIndex(mActivePointerId);
const float x = e.getX(index); const float x = e.getX(index);
const float y = e.getY(index); const float y = e.getY(index);
mItemTouchHelper->mInitialTouchX = x; mInitialTouchX = x;
mItemTouchHelper->mInitialTouchY = y; mInitialTouchY = y;
mItemTouchHelper->mDx = mItemTouchHelper->mDy = 0.f; mDx = mDy = 0.f;
LOGD_IF(_DEBUG,"onlong press: x:%.f ,y:%.f",x,y); LOGD_IF(_DEBUG,"onlong press: x:%.f ,y:%.f",x,y);
if (mItemTouchHelper->mCallback->isLongPressDragEnabled()) { if (mCallback->isLongPressDragEnabled()) {
mItemTouchHelper->select(vh, ACTION_STATE_DRAG); select(vh, ACTION_STATE_DRAG);
} }
} }
} }

View File

@ -35,9 +35,7 @@ public:
class Callback; class Callback;
class SimpleCallback; class SimpleCallback;
class RecoverAnimation; class RecoverAnimation;
class ItemTouchHelperGestureListener;
protected: protected:
friend ItemTouchHelperGestureListener;
std::vector<View*> mPendingCleanup; std::vector<View*> mPendingCleanup;
std::vector<RecoverAnimation*> mRecoverAnimations; std::vector<RecoverAnimation*> mRecoverAnimations;
RecyclerView* mRecyclerView; RecyclerView* mRecyclerView;
@ -68,6 +66,11 @@ private:
int mOverdrawChildPosition = -1; int mOverdrawChildPosition = -1;
GestureDetector* mGestureDetector; GestureDetector* mGestureDetector;
//ItemTouchHelperGestureListener mItemTouchHelperGestureListener; //ItemTouchHelperGestureListener mItemTouchHelperGestureListener;
GestureDetector::OnGestureListener mItemTouchHelperGestureListener;
bool mShouldReactToLongPress = true;
void doNotReactToLongPress();
bool onGestureDown(MotionEvent& e);
void onGestureLongPress(MotionEvent& e);
RecyclerView::OnItemTouchListener mOnItemTouchListener; RecyclerView::OnItemTouchListener mOnItemTouchListener;
RecyclerView::OnChildAttachStateChangeListener mOnChildAttachStateChangeListener; RecyclerView::OnChildAttachStateChangeListener mOnChildAttachStateChangeListener;
@ -189,17 +192,6 @@ public:
int getMovementFlags(RecyclerView& recyclerView,RecyclerView::ViewHolder& viewHolder)override; int getMovementFlags(RecyclerView& recyclerView,RecyclerView::ViewHolder& viewHolder)override;
}; };
class ItemTouchHelper::ItemTouchHelperGestureListener/*:public GestureDetector::SimpleOnGestureListener*/{
private:
bool mShouldReactToLongPress = true;
ItemTouchHelper*mItemTouchHelper;
public:
ItemTouchHelperGestureListener(ItemTouchHelper*);
void doNotReactToLongPress();
bool onDown(MotionEvent& e);
void onLongPress(MotionEvent& e);
};
class ItemTouchHelper::RecoverAnimation{ class ItemTouchHelper::RecoverAnimation{
protected: protected:
friend ItemTouchHelper; friend ItemTouchHelper;