gpt4 book ai didi

android - 加速回收器查看 "swipe"动画?

转载 作者:行者123 更新时间:2023-11-29 23:12:06 29 4
gpt4 key购买 nike

我将自定义 ItemTouchHelper.SimpleCallback 类添加到我的 RecyclerView 以添加“滑动时关闭”功能,用户可以向左滑动项目并获取项目从列表中删除。

我通过将 dX(来自 onChildDraw())除以 10,将 View 可以向左“滑动”的数量限制为 View 长度的 10%。但是,用户必须滑动的长度是 100%项目滑动屏幕的 10%。我想限制滑动(增加项目滑动速度?)以便用户只需滑动屏幕的 10% 即可让项目滑动 10%。见下图:

enter image description here

紫色圆圈是用户触摸。当用户滑动整个屏幕宽度时,项目滑动 10%(红色是滑动时显示项目“后面”的布局)。

如何提高滑动速度以滑动 10% 的屏幕宽度就足够了,而不是滑动整个屏幕宽度?

这是我的 ItemTouchHelper.SimpleCallback 类的代码:

public class SwipeDeleteHelper extends ItemTouchHelper.SimpleCallback {

private GameAdapter adapter;
private Drawable icon;
private final ColorDrawable background;
private static final int LIMIT_SWIPE_LENGTH = 10;


public SwipeDeleteHelper(GameAdapter adapter, Context context) {
super(0, ItemTouchHelper.LEFT);
this.adapter = adapter;
icon = ContextCompat.getDrawable(adapter.getContext(),
R.drawable.delete);
background = new ColorDrawable(context.getColor(R.color.colorAccent));
}

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

@Override
public void onSwiped(@NonNull RecyclerView.ViewHolder viewHolder, int direction) {
int position = viewHolder.getAdapterPosition();
adapter.deleteItem(position);
}

@Override
public void onChildDraw(@NotNull Canvas c, @NotNull RecyclerView recyclerView, @NotNull RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive) {
dX = dX/LIMIT_SWIPE_LENGTH;

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

View itemView = viewHolder.itemView;
int backgroundCornerOffset = 20;

int iconMargin = (itemView.getHeight() - icon.getIntrinsicHeight()) / 2;
int iconTop = itemView.getTop() + (itemView.getHeight() - icon.getIntrinsicHeight()) / 2;
int iconBottom = iconTop + icon.getIntrinsicHeight();

if(dX < 0) { //left swipe
int iconLeft = itemView.getRight() - iconMargin - icon.getIntrinsicWidth();
int iconRight = itemView.getRight() - iconMargin;
icon.setBounds(iconLeft, iconTop, iconRight, iconBottom);

background.setBounds(itemView.getRight() + ((int) dX) - backgroundCornerOffset,
itemView.getTop(), itemView.getRight(), itemView.getBottom());
} else { //unswiped
background.setBounds(0, 0, 0, 0);
}
background.draw(c);
icon.draw(c);
}

}

最佳答案

覆盖 getSwipeThreshold 以返回 0.1f:

@Override
public float getSwipeThreshold(@NonNull RecyclerView.ViewHolder viewHolder) {
return 0.1f;
}

关于android - 加速回收器查看 "swipe"动画?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55890877/

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