gpt4 book ai didi

java - 如何以编程方式重置 recyclerview 的背景按钮(滑动菜单)?

转载 作者:行者123 更新时间:2023-12-01 16:16:11 24 4
gpt4 key购买 nike

我有一个 recyclerview,它在我的 Android 应用程序上有一个滑动菜单(背景按钮)。我引用的文章是:https://codeburst.io/android-swipe-menu-with-recyclerview-8f28a235ff28

这篇文章很有帮助,我成功地嵌入了滑动菜单功能的基本功能。
但是,在嵌入回收器 View 列表的过滤功能后,我注意到我必须找出一种方法来手动/以编程方式重置/清除滑动菜单,因为在过滤卡片 View 列表后,滑动菜单是徘徊在每个列表的背景中,但我不知道如何解决这个问题。 (如附图所示)

我尝试过的事情是:
由适配器通知DataSetChanged - 不起作用
适配器通知项目更改 - 不起作用
重置 ItemTouchHelper(将 null 并重新附加) - 不起作用
recyclerview.performClick 重置 ItemTouchHelper 的状态 - 不起作用

根据该文章,单击 recyclerview 似乎会重置 ItemTouchHelper 的状态。您能否教我如何以编程方式单击回收器 View 或任何其他可用的解决方案?

以下代码是我认为与解决此问题相关的文章中的一小部分源代码...

private void setTouchUpListener(final Canvas c, 
final RecyclerView recyclerView,
final RecyclerView.ViewHolder viewHolder,
final float dX, final float dY,
final int actionState, final boolean isCurrentlyActive) {
recyclerView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_UP) {
SwipeController.super.onChildDraw(c, recyclerView, viewHolder, 0F, dY, actionState, isCurrentlyActive);
recyclerView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
return false;
}
});
setItemsClickable(recyclerView, true);
swipeBack = false;
buttonShowedState = ButtonsState.GONE;
}
return false;
}
});
}

After the swipe menu emerged by swiping Swipe menus remained after filtering the list

最佳答案

我发现在 recyclerview 上添加布局更改监听器可以删除滑动菜单。

@Override
public int getMovementFlags(@NotNull final RecyclerView recyclerView, @NotNull final RecyclerView.ViewHolder viewHolder) {
myRecyclerView = recyclerView;
myRecyclerView.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
@Override
public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
v.removeOnLayoutChangeListener(this);
clearSwipeMenu();
}
});
}

...

@Override
public void onChildDraw(@NotNull Canvas c, @NotNull RecyclerView recyclerView, @NotNull RecyclerView.ViewHolder viewHolder,
float dX, float dY, int actionState, boolean isCurrentlyActive) {
if (actionState == ACTION_STATE_SWIPE) {
if (buttonShowedState != ButtonsState.GONE) {
if (buttonShowedState == ButtonsState.LEFT_VISIBLE) dX = Math.max(dX, buttonWidth);
if (buttonShowedState == ButtonsState.RIGHT_VISIBLE) dX = Math.min(dX, -buttonWidth);
super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive);
} else {
setTouchListener(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive);
}
}

if (buttonShowedState == ButtonsState.GONE) {
super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive);
}
currentItemViewHolder = viewHolder;
}

...

public void clearSwipeMenu() {
if(currentItemViewHolder!=null){
clearView(myRecyclerView, currentItemViewHolder);
currentItemViewHolder = null;
}
}

关于java - 如何以编程方式重置 recyclerview 的背景按钮(滑动菜单)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62383554/

24 4 0