滑动reviewerview项目以消除卡在片段中的某些时间

时间:2019-03-05 12:30:17

标签: android-recyclerview ezswipecontroller

我正在使用recycerview滑动以在列表项后面显示操作按钮,而我的recyclerview处于底部导航的片段中,并且一切正常,但是有时滑动会失效并卡住,我无法滑动我的项目,请帮助解决我的问题,我的代码是

我在主要活动中的代码

ItemTouchHelper itemTouchhelper =新的ItemTouchHelper(swipeController);         itemTouchhelper.attachToRecyclerView(movelistview);

    movelistview.addItemDecoration(new RecyclerView.ItemDecoration() {
        @Override
        public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
            swipeController.onDraw(c,"action");

        }
    });

滑动控制器类

公共类SwipeController扩展了回调{

private boolean swipeBack = false;


private ButtonsState buttonShowedState = ButtonsState.GONE;

private RectF buttonInstance = null;

private RecyclerView.ViewHolder currentItemViewHolder = null;

private SwipeControllerActions buttonsActions = null;

private static final float buttonWidth = 200;

public SwipeController(SwipeControllerActions buttonsActions) {
    this.buttonsActions = buttonsActions;
}

@Override
public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
    return makeMovementFlags(0, LEFT );
}

@Override
public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
    return false;
}

@Override
public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {


}

@Override
public int convertToAbsoluteDirection(int flags, int layoutDirection) {
    if (swipeBack) {
        swipeBack = buttonShowedState != ButtonsState.GONE;
        return 0;
    }
    return super.convertToAbsoluteDirection(flags, layoutDirection);
}

@Override
public void onChildDraw(Canvas c, RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive) {
    if (actionState == ACTION_STATE_SWIPE) {
        if (buttonShowedState != ButtonsState.GONE) {
            //if (buttonShowedState == ButtonsState.LEFT_VISIBLE) dX = Math.max(dX, buttonWidth);
            if (buttonShowedState == ButtonsState.RIGHT_VISIBLE) dX = Math.min(dX, -buttonWidth);
            super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive);
        }
        else {
            setTouchListener(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive);
        }
    }

    if (buttonShowedState == ButtonsState.GONE) {
        super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive);
    }
    currentItemViewHolder = viewHolder;
}

private void setTouchListener(final Canvas c, final RecyclerView recyclerView, final RecyclerView.ViewHolder viewHolder, final float dX, final float dY, final int actionState, final boolean isCurrentlyActive) {
    recyclerView.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            swipeBack = event.getAction() == MotionEvent.ACTION_CANCEL || event.getAction() == MotionEvent.ACTION_UP;
            if (swipeBack) {
                if (dX < -buttonWidth) buttonShowedState = ButtonsState.RIGHT_VISIBLE;
                //else if (dX > buttonWidth) buttonShowedState  = ButtonsState.LEFT_VISIBLE;

                if (buttonShowedState != ButtonsState.GONE) {
                    setTouchDownListener(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive);
                    setItemsClickable(recyclerView, false);
                }
            }
            return false;
        }
    });
}

private void setTouchDownListener(final Canvas c, final RecyclerView recyclerView, final RecyclerView.ViewHolder viewHolder, final float dX, final float dY, final int actionState, final boolean isCurrentlyActive) {
    recyclerView.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if (event.getAction() == MotionEvent.ACTION_DOWN) {
                setTouchUpListener(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive);
            }
            return false;
        }
    });
}

private void setTouchUpListener(final Canvas c, final RecyclerView recyclerView, final RecyclerView.ViewHolder viewHolder, final float dX, final float dY, final int actionState, final boolean isCurrentlyActive) {
    recyclerView.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if (event.getAction() == MotionEvent.ACTION_UP) {
                SwipeController.super.onChildDraw(c, recyclerView, viewHolder, 0F, dY, actionState, isCurrentlyActive);
                recyclerView.setOnTouchListener(new View.OnTouchListener() {
                    @Override
                    public boolean onTouch(View v, MotionEvent event) {
                        return false;
                    }
                });
                setItemsClickable(recyclerView, true);
                swipeBack = false;

                if (buttonsActions != null && buttonInstance != null && buttonInstance.contains(event.getX(), event.getY())) {
                    /*if (buttonShowedState == ButtonsState.LEFT_VISIBLE) {
                        buttonsActions.onLeftClicked(viewHolder.getAdapterPosition());
                    }else*/
                    if (buttonShowedState == ButtonsState.RIGHT_VISIBLE) {
                        buttonsActions.onRightClicked(viewHolder.getAdapterPosition());
                    }
                }
                buttonShowedState = ButtonsState.GONE;
                currentItemViewHolder = null;
            }
            return false;
        }
    });
}

private void setItemsClickable(RecyclerView recyclerView, boolean isClickable) {
    for (int i = 0; i < recyclerView.getChildCount(); ++i) {
        recyclerView.getChildAt(i).setClickable(isClickable);
    }
}

private void drawButtons(Canvas c, RecyclerView.ViewHolder viewHolder,String text) {
    float buttonWidthWithoutPadding = buttonWidth - 0;
    float corners = 0;

    View itemView = viewHolder.itemView;
    Paint p = new Paint();
    int position=viewHolder.getAdapterPosition();
    String button_text="Report";

    if(position>=0){
    if(MainApplication.moves_list.get(position).getMove_userId().equals(MainApplication.user_id)){
        button_text="Delete"; }
    else { button_text="Report"; }
    }

    RectF rightButton = new RectF(itemView.getRight() - buttonWidthWithoutPadding, itemView.getTop()+0, itemView.getRight(), itemView.getBottom()-0);
    p.setColor(Color.RED);
    c.drawRoundRect(rightButton, corners, corners, p);
    drawText(button_text, c, rightButton, p);

    buttonInstance = null;
        if (buttonShowedState == ButtonsState.RIGHT_VISIBLE) {
        buttonInstance = rightButton;
    }
}

private void drawText(String text, Canvas c, RectF button, Paint p) {
    float textSize = 40;
    p.setColor(Color.WHITE);
    p.setAntiAlias(true);
    p.setTextSize(textSize);

    float textWidth = p.measureText(text);
    c.drawText(text, button.centerX()-(textWidth/2), button.centerY()+(textSize/2), p);
}

public void onDraw(Canvas c,String text) {
    if (currentItemViewHolder != null) {
        drawButtons(c, currentItemViewHolder,text);
    }
}

}

2 个答案:

答案 0 :(得分:0)

我遇到了同样的问题。滑动按钮卡在UI中,位于recyclerview的后面。所以这是我找到的最简单,最好的解决方案。

如下所示在SwipeViewController中创建新功能

.ui-grid-header-cell{
height:60px;
max-height:60px;
}

,然后在要在同一回收者视图中填充数据的片段中使用此功能。 如下

setSwipeBack();

答案 1 :(得分:0)

我遇到了同样的问题,但是解决方法很简单。

这将在父容器拦截您的触摸事件时发生。任何覆盖ViewGroup.onInterceptTouchEvent(MotionEvent)的ViewGroup都可以做到这一点(例如RecyclerView或ScrollView)。

这将导致result <- rep(0, length(number)) for(i in 1:length(number)) { result[i] <- number[i] + 3 } 值略有变化时,您的motionEvent返回ACTION_CANCEL,并将触摸事件发送到父视图。

处理此问题的正确方法是,一旦您认为需要保留运动事件,就可以在父视图上调用ViewParent.requestDisallowInterceptTouchEvent(boolean)方法。

这是一个简单的示例(attemptClaimDrag()方法取自android源代码):

Y

在您的onTouchEvent()或onTouch()中:

/**
 * Tries to claim the user's drag motion, and requests disallowing any
 * ancestors from stealing events in the drag.
 */
private void attemptClaimDrag() {
    // parent = yourView.getParent();
    if (parent != null) {
        parent.requestDisallowInterceptTouchEvent(true);
    }
}