滑动以删除在自定义DialogFragment情况下不起作用

时间:2019-03-13 14:31:05

标签: java android kotlin android-recyclerview recycler-adapter

我正在Dialogfragment中进行一些修改。

val params = dialog.window!!.attributes
            //   params.width = (getWidth() * 0.7).toInt()
            params.height = (getHeight() * 0.75).toInt()

            dialog.window!!.attributes = params as android.view.WindowManager.LayoutParams

然后我将Swipehelper附加到recyclerview中。 我的SwipeHelper类

public abstract class SwipeHelper extends ItemTouchHelper.SimpleCallback implements View.OnTouchListener {
    public  int BUTTON_WIDTH = 90;//converted later inside construcor
    private RecyclerView recyclerView;
    private List<UnderlayButton> buttons;
    private GestureDetector gestureDetector;
    private int swipedPos = -1;
    private float swipeThreshold = 0.5f;
    private Map<Integer, List<UnderlayButton>> buttonsBuffer;
    private Queue<Integer> recoverQueue;
    private Context activity;
    private int previousOrientationWidth = 0;
    private View itemView;

    private float convertForDisplay(float num) {
        return selectedTapArea(activity, (int) num);
    }

    private float selectedTapArea(Context context, int unit) {
        // TODO Auto-generated method stub
        float tap_pixel = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, unit, context
                .getResources().getDisplayMetrics());

        return tap_pixel;

    }

    private GestureDetector.SimpleOnGestureListener gestureListener = new GestureDetector.SimpleOnGestureListener() {
        @Override
        public boolean onSingleTapConfirmed(MotionEvent e) {
            for (UnderlayButton button : buttons) {
                if (button.onClick(e.getX(), e.getY()))
                    break;
            }

            return true;
        }
    };

    @Override
    public boolean onTouch(View view, MotionEvent e) {
        if (swipedPos < 0) return false;
        Point point = new Point((int) e.getRawX(), (int) e.getRawY());

        RecyclerView.ViewHolder swipedViewHolder = recyclerView.findViewHolderForAdapterPosition(swipedPos);
        if (swipedViewHolder != null) {
            View swipedItem = swipedViewHolder.itemView;
            Rect rect = new Rect();
            swipedItem.getGlobalVisibleRect(rect);

            if (e.getAction() == MotionEvent.ACTION_DOWN || e.getAction() == MotionEvent.ACTION_UP || e.getAction() == MotionEvent.ACTION_MOVE) {
                if (rect.top < point.y && rect.bottom > point.y) {
                    gestureDetector.onTouchEvent(e);
                } else {
                    recoverQueue.add(swipedPos);
                    swipedPos = -1;
                    recoverSwipedItem();
                }
            }
        }
        return false;
    }

    public SwipeHelper(Context activity, RecyclerView recyclerView) {
        super(0, ItemTouchHelper.LEFT);
        this.recyclerView = recyclerView;
        this.activity=activity;
        this.buttons = new ArrayList<>();
        this.gestureDetector = new GestureDetector(activity, gestureListener);
        this.activity = activity;
        this.recyclerView.setOnTouchListener(this);
        buttonsBuffer = new HashMap<>();
        recoverQueue = new LinkedList<Integer>() {
            @Override
            public boolean add(Integer o) {
                if (contains(o))
                    return false;
                else
                    return super.add(o);
            }
        };
        BUTTON_WIDTH = ((int) convertForDisplay(90));

        attachSwipe();
    }


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

    @Override
    public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
        int pos = viewHolder.getAdapterPosition();

        if (swipedPos != pos)
            recoverQueue.add(swipedPos);

        swipedPos = pos;

        if (buttonsBuffer.containsKey(swipedPos))
            buttons = buttonsBuffer.get(swipedPos);
        else
            buttons.clear();

        buttonsBuffer.clear();
        swipeThreshold = 0.5f * buttons.size() * BUTTON_WIDTH;
        recoverSwipedItem();
    }

    @Override
    public float getSwipeThreshold(RecyclerView.ViewHolder viewHolder) {
        return swipeThreshold;
    }

    @Override
    public float getSwipeEscapeVelocity(float defaultValue) {
        return defaultValue;
    }

    @Override
    public float getSwipeVelocityThreshold(float defaultValue) {
        return 5.0f * defaultValue;
    }

    @Override
    public void onChildDraw(Canvas c, RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive) {
        int pos = viewHolder.getAdapterPosition();
        if(previousOrientationWidth != 0 && previousOrientationWidth == Math.abs(dX)){
            dX = -itemView.getWidth();
        }
        float translationX = dX;
        itemView = viewHolder.itemView;


        /*if (recyclerView.getAdapter() instanceof MarkUpAdapter && pos >= 0) {   //Bug 30781 - Application crashes repeatedly on deleting markups-By Rustam on 11th July 2018
            MarkUpAdapter adapter = ((MarkUpAdapter) recyclerView.getAdapter());
            if (!PermissionManager.getInstance().canDeleteMarkup(adapter.getItemAt(pos))) {
                return;
            }
        }*/

        if (pos < 0) {
            swipedPos = pos;
            return;
        }

        if (actionState == ItemTouchHelper.ACTION_STATE_SWIPE) {
            if (dX < 0) {
                List<UnderlayButton> buffer = new ArrayList<>();

                if (!buttonsBuffer.containsKey(pos)) {
                    instantiateUnderlayButton(viewHolder, buffer);
                    buttonsBuffer.put(pos, buffer);
                } else {
                    buffer = buttonsBuffer.get(pos);
                }

                translationX = dX * buffer.size() * BUTTON_WIDTH / itemView.getWidth();
                drawButtons(c, itemView, buffer, pos, translationX);
            }
        }

        super.onChildDraw(c, recyclerView, viewHolder, translationX, dY, actionState, isCurrentlyActive);
    }

    public synchronized void recoverSwipedItem() {
        while (!recoverQueue.isEmpty()) {
            int pos = recoverQueue.poll();
            if (pos > -1) {
                recyclerView.getAdapter().notifyItemChanged(pos);
            }
        }
    }

    private void drawButtons(Canvas c, View itemView, List<UnderlayButton> buffer, int pos, float dX) {
        float right = itemView.getRight();
        float dButtonWidth = (-1) * dX / buffer.size();

        for (UnderlayButton button : buffer) {
            float left = right - dButtonWidth;
            button.onDraw(c, new RectF(left, itemView.getTop(), right, itemView.getBottom()), pos);
            right = left;
        }
    }

    ItemTouchHelper itemTouchHelper;

    public void attachSwipe() {
        itemTouchHelper = new ItemTouchHelper(this);
        itemTouchHelper.attachToRecyclerView(recyclerView);
        recyclerView.setOnTouchListener(this);
    }

    public void dettachSwipe() {
        recoverSwipedItem();
        itemTouchHelper.attachToRecyclerView(null);
        recyclerView.clearOnChildAttachStateChangeListeners();
        recyclerView.setOnTouchListener(null);
    }

    public void resetSwipePos() {
        swipedPos = -1;
    }

    public int getSwipedPos() {
        return swipedPos;
    }

    public void handleOrientationChange(){
        if(itemView != null){
            previousOrientationWidth = itemView.getWidth();
        }
    }

    public abstract void instantiateUnderlayButton(RecyclerView.ViewHolder viewHolder, List<UnderlayButton> underlayButtons);

    public static class UnderlayButton {
        private float PADDING = 5;
        private String text;
        private int imageResId;
        private int color;
        private int pos;
        private RectF clickRegion;
        private Context activity;
        private UnderlayButtonClickListener clickListener;

        public UnderlayButton(Context activity, String text, int imageResId, int color, UnderlayButtonClickListener clickListener) {
            this.text = text;
            this.imageResId = imageResId;
            this.color = color;
            this.clickListener = clickListener;
            this.activity=activity;
            PADDING = convertForDisplay(5);
        }

        private float convertForDisplay(float num) {
            return selectedTapArea(activity, (int) num);
        }

        private float selectedTapArea(Context context, int unit) {
            // TODO Auto-generated method stub
            float tap_pixel = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, unit, context
                    .getResources().getDisplayMetrics());

            return tap_pixel;

        }

        public boolean onClick(float x, float y) {
            if (clickRegion != null && clickRegion.contains(x, y)) {
                clickListener.onClick(pos);
                return true;
            }

            return false;
        }

        public void onDraw(Canvas c, RectF rect, int pos) {
            Paint p = new Paint();

            // Draw background
            p.setColor(color);
            c.drawRect(rect, p);

            // Draw Text
            p.setColor(Color.WHITE);
            p.setTextSize(convertForDisplay(12));

            Rect r = new Rect();
//            float cHeight = rect.height();
//            float cWidth = rect.width();
            p.setTextAlign(Paint.Align.LEFT);
            p.getTextBounds(text, 0, text.length(), r);

            Bitmap image = getDrawableBitmap();
            PointF textRect = getTextDisp(rect, r, image == null ? 0 : image.getHeight());
            PointF imageRect = image == null ? new PointF() : getBitmapDisp(rect, r.height(), image);

            c.drawText(text, rect.left + textRect.x, rect.top + textRect.y + PADDING / 2, p);
            if (image != null) {
                c.drawBitmap(image, rect.left + imageRect.x, rect.top + imageRect.y - PADDING / 2, new Paint());
            }
            clickRegion = rect;
            this.pos = pos;
        }

        private PointF getBitmapDisp(RectF container, float textHeight, Bitmap bitmap) {
            float width = bitmap.getWidth();
            float height = bitmap.getHeight() + textHeight;
            PointF bitmapPoint = new PointF();
            bitmapPoint.x = (container.width() - width) / 2;
            bitmapPoint.y = (container.height() - height) / 2;
            return bitmapPoint;
        }

        private PointF getTextDisp(RectF container, Rect textRect, float bitmapHeight) {
            float width = textRect.width();
            float height = textRect.height() + bitmapHeight;
            PointF textPoint = new PointF();
            textPoint.x = (container.width() - width) / 2 - textRect.left;
            textPoint.y = (container.height() + height) / 2 - textRect.bottom;
            return textPoint;
        }

        private Bitmap getDrawableBitmap() {
            if (imageResId > 0) {
                Drawable drawable = activity.getResources().getDrawable(imageResId);
                float aspectRatio = drawable.getIntrinsicHeight() / ((float) drawable.getIntrinsicWidth());
                int width = ((int)convertForDisplay(20));
                int height = (int) (width * aspectRatio);
                Bitmap resultPhoto = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_4444);
                Canvas canvas = new Canvas(resultPhoto);
                drawable.setBounds(0, 0, width, height);
                drawable.draw(canvas);

                return resultPhoto;
            } else {
                return null;
            }
        }
    }

    public interface UnderlayButtonClickListener {
        void onClick(int pos);
    }
}

现在,recyclerview代码:-

object : SwipeHelper(fragment.context, view) {
            override fun instantiateUnderlayButton(viewHolder: RecyclerView.ViewHolder, underlayButtons: MutableList<UnderlayButton>) {
                underlayButtons.add(UnderlayButton(
                        activity,
                        "Delete",
                        0,
                        ActivityCompat.getColor(activity, R.color.bg_row_background),
                        UnderlayButtonClickListener {
                            onSwiped(swipedPos)
                        }
                ))
            }
        }

我面临的问题是,侦听器没有调用delete操作,而是在单击删除按钮时调用了underlayButtons.add()

请注意,以上问题仅在下一行出现

params.height = (getHeight() * 0.75).toInt()

我无法在SwipeHelper类中找到需要更改的地方。

0 个答案:

没有答案