我能够在列表中实现拖放项目并重新排序。我需要做的是实现一个删除功能,人们可以选择并保持一个项目并将其拖到垃圾桶图标。一旦它与垃圾图标重叠,后者将其颜色从灰色变为红色。一旦用户释放该项目,将出现确认模式。
从演示中,您可以看到将列表中的前三个项目拖到垃圾桶图标时没有将垃圾桶图标变为红色,而是启动了模态。第四项确实更改了垃圾桶图标的颜色,但它立即从列表中删除而未经确认。
使用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操作系统,用户将应用程序拖动到“卸载”图标,将图标的颜色更改为红色,并在应用程序发布后删除该应用程序。
答案 0 :(得分: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)
}