gpt4 book ai didi

android - 如何在 RecyclerView 中执行拖动和删除项目功能

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:21:46 47 4
gpt4 key购买 nike

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

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

拖放是使用 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 操作系统的东西,用户将应用程序拖到卸载图标上,将图标的颜色更改为红色,并在应用程序发布后将其删除。

最佳答案

对于仍在寻找解决方案的开发者;只是一个想法,你可以试试下面的代码。我的 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)
}

关于android - 如何在 RecyclerView 中执行拖动和删除项目功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47192381/

47 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com