6

我能够在列表中实现拖放项目并重新排序。我需要做的是实现一个删除功能,可以选择并按住一个项目并将其拖到垃圾桶图标。一旦它与垃圾桶图标重叠,后者的颜色将从灰色变为红色。一旦用户释放该项目,将出现一个确认模式。 在此处输入图像描述

从演示中,您可以看到将列表中的前三个项目拖向垃圾桶图标并没有将垃圾桶图标变为红色,而是启动了模式。第四个项目确实改变了垃圾桶图标的颜色,但它在没有确认的情况下立即从列表中删除。

拖放是使用以下方法实现的ItemTouchHelperCallback

public class EditItemTouchHelperCallback extends ItemTouchHelper.Callback {

    private final RVAdapter mAdapter;
    private RecyclerView.ViewHolder curHolder;

    private int curPos;
    private boolean deletedSomething;
    private CreatePhotostoryActivity cpa;

    public EditItemTouchHelperCallback(RVAdapter adapter) {
        mAdapter = adapter;
        cpa = new CreatePhotostoryActivity();
    }

    @Override
    public boolean isLongPressDragEnabled() {
        return true;
    }

    @Override
    public boolean isItemViewSwipeEnabled() {
        return false;
    }

    @Override
    public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
        int dragFlags = ItemTouchHelper.UP | ItemTouchHelper.DOWN | ItemTouchHelper.START | ItemTouchHelper.END;
        int swipeFlags = ItemTouchHelper.START | ItemTouchHelper.END;
        return makeMovementFlags(dragFlags, swipeFlags);
    }

    @Override
    public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder,
                          RecyclerView.ViewHolder target) {
        curPos = target.getAdapterPosition();
        if(!deletedSomething){
            mAdapter.onItemMove(viewHolder.getAdapterPosition(), target.getAdapterPosition());
        }
        return true;
    }

    @Override
    public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
        mAdapter.onItemDismiss(viewHolder.getAdapterPosition());
    }

    @Override
    public void clearView(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
        CreatePhotostoryActivity.trashNavbar.setVisibility(View.GONE);
        CreatePhotostoryActivity.navbar.setVisibility(View.VISIBLE);
    }

    @Override
    public void onSelectedChanged(RecyclerView.ViewHolder viewHolder, int actionState) {

        boolean isOver = false;
        deletedSomething = false;

        CreatePhotostoryActivity.trashNavbar.setVisibility(View.VISIBLE);
        CreatePhotostoryActivity.trashNavbar.bringToFront();
        CreatePhotostoryActivity.navbar.setVisibility(View.GONE);

        if(ItemTouchHelper.ACTION_STATE_DRAG == actionState) {
            if (isViewOverlapping(viewHolder.itemView, CreatePhotostoryActivity.trashNavbar)) {
                CreatePhotostoryActivity.trashIcon.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.red500));
                //delete moment
                mAdapter.onItemDismiss(viewHolder.getAdapterPosition());
            } else {
                CreatePhotostoryActivity.trashIcon.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.grey700));
            }

            curHolder=viewHolder;
            curPos = curHolder.getAdapterPosition();
            Log.v("Selected","a viewholder "+ curPos);
        }

        if (isViewOverlapping(curHolder.itemView, CreatePhotostoryActivity.trashNavbar)) {
            isOver = true;
        }

        if(isOver) {
            deletedSomething = true;
            mAdapter.showConfirmationDialogDeleteMoment(curPos);
            Log.v("Dropped","a viewholder "+curHolder);
        }
    }

    private boolean isViewOverlapping(View firstView, View secondView) {
        int[] firstPosition = new int[2];
        int[] secondPosition = new int[2];

        firstView.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
        firstView.getLocationOnScreen(firstPosition);
        secondView.getLocationOnScreen(secondPosition);
        int r = firstView.getMeasuredHeight() + firstPosition[1];
        int l = secondPosition[1];
        return r >= l && (r != 0 && l != 0);
    }

}

如您所见,它onSelectedChanged设置了垃圾桶图标的颜色并调用该方法来启动确认模式。我不确定我是否将方法放置在正确的位置。在我的适配器中,我使用以下方法从列表中删除该项目:

@Override
public void onItemDismiss(int position) {
    moments.remove(position);
    notifyItemRemoved(position);
}

启动确认模式的方法调用上述方法。它也位于我的适配器中:

public void showConfirmationDialogDeleteMoment(final int position) {
    AlertDialog.Builder builder =  new AlertDialog.Builder(createPhotoStoryLayout.getContext(), R.style.AlertDialog);
    builder.setTitle("DO YOU WANT TO DELETE THIS MOMENT?")
            .setPositiveButton("DELETE", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    onItemDismiss(position);
                }
            })
            .setNegativeButton("NO", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    //do nothing
                }
            })
            .setIcon(0);
    AlertDialog alert = builder.create();
    alert.show();
    alert.getWindow().setLayout(WindowManager.LayoutParams.MATCH_PARENT,
            (int) (170 * Resources.getSystem().getDisplayMetrics().density));
    AlertUtils.displayDialog(createPhotoStoryLayout.getContext(), alert, R.drawable.red_500_pill, R.drawable.grey_700_pill, R.color.red500, R.color.grey700);

}

基本上,我想要的是类似于 Android 操作系统的东西,用户将应用程序拖到卸载图标,将图标的颜色更改为红色,并在应用程序发布后将其删除。

4

1 回答 1

0

对于仍在寻找解决方案的开发人员;只是一个想法,您可以尝试以下代码。我的 ItemTouchHelper.Callback 实现的一部分。我试过了,它在我的情况下工作正常。

    interface Callback{
        fun isInDeleteArea(dX: Float, dY: Float): Boolean
    }

    private var removeItem = false


    lateinit var callback: Callback

    override fun onChildDraw(
        c: Canvas,
        recyclerView: RecyclerView,
        viewHolder: RecyclerView.ViewHolder,
        dX: Float,
        dY: Float,
        actionState: Int,
        isCurrentlyActive: Boolean
    ) {
        if (isCurrentlyActive.not() && callback.isInDeleteArea(dX, dY)) {
            removeItem = true
            viewHolder.itemView.isGone = true
            return
        }

        viewHolder.itemView.translationY = dY
        viewHolder.itemView.translationX = dX
    }


    override fun clearView(recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder) {

    }

    override fun getAnimationDuration(
        recyclerView: RecyclerView,
        animationType: Int,
        animateDx: Float,
        animateDy: Float
    ): Long {
        if (removeItem) return 0
        return super.getAnimationDuration(recyclerView, animationType, animateDx, animateDy)
    }


    override fun onSwiped(viewHolder: RecyclerView.ViewHolder, direction: Int) {}

    override fun onSelectedChanged(viewHolder: RecyclerView.ViewHolder?, actionState: Int) {
        removeItem = false
        super.onSelectedChanged(viewHolder, actionState)
    }
于 2019-03-11T23:02:10.990 回答