当前位置: 首页 > 知识库问答 >
问题:

刷卡后如何恢复回收器视图项

郭炳
2023-03-14

我有一个recyclerview,我用itemtouchhelper类给它添加了swipe,它工作得很好,在布局中有两个视图,前台视图在bacjground层上滑动,问题是我希望前台视图在滑动后获得上一个位置,就像在三星手机中进行呼叫的滑动效果一样。我可以在刷卡后拨打notifyitemchanged来实现这一点。但我只工作了一次,如果我再次在该项上滑动,视图不会恢复,这是我的itemtouchhelper类

public class RecyclerItemTouchHelperlog extends ItemTouchHelper.SimpleCallback {
private RecyclerItemTouchHelperListener listener;

public RecyclerItemTouchHelperlog(int dragDirs, int swipeDirs, RecyclerItemTouchHelperListener listener) {
    super(dragDirs, swipeDirs);
    this.listener = listener;
}

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

@Override
public void onSelectedChanged(RecyclerView.ViewHolder viewHolder, int actionState) {
    if (viewHolder != null) {
        final View foregroundView = ((recycleAdapterlog.MyViewHolder) viewHolder).logitm;

        getDefaultUIUtil().onSelected(foregroundView);
    }
}

@Override
public void onChildDrawOver(Canvas c, RecyclerView recyclerView,
                            RecyclerView.ViewHolder viewHolder, float dX, float dY,
                            int actionState, boolean isCurrentlyActive) {
    final View foregroundView = ((recycleAdapterlog.MyViewHolder) viewHolder).logitm;
    getDefaultUIUtil().onDrawOver(c, recyclerView, foregroundView, dX, dY,
            actionState, isCurrentlyActive);
}

@Override
public void clearView(@NonNull RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
    final View foregroundView = ((recycleAdapterlog.MyViewHolder) viewHolder).logitm;
    getDefaultUIUtil().clearView(foregroundView);
}

@Override
public int getSwipeDirs(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder) {

    View hv=((recycleAdapterlog.MyViewHolder) viewHolder).hv;
    if(hv==null)return super.getSwipeDirs(recyclerView, viewHolder);
    else if(hv.getVisibility()==View.VISIBLE)return 0;
    else return super.getSwipeDirs(recyclerView, viewHolder);


}

@Override
public void onChildDraw(Canvas c, @NonNull RecyclerView recyclerView,
                        @NonNull RecyclerView.ViewHolder viewHolder, float dX, float dY,
                        int actionState, boolean isCurrentlyActive) {
    final View foregroundView = ((recycleAdapterlog.MyViewHolder) viewHolder).logitm;
    final View swipel = ((recycleAdapterlog.MyViewHolder) viewHolder).swipelogleft;
    final View swiper = ((recycleAdapterlog.MyViewHolder) viewHolder).swipelogright;
    if(dX<0){
        swipel.setVisibility(View.VISIBLE);
        swiper.setVisibility(View.GONE);
    }
    else {
        swipel.setVisibility(View.GONE);
        swiper.setVisibility(View.VISIBLE);
    }
    if(actionState == ItemTouchHelper.ACTION_STATE_SWIPE) {
        final Float alpha =1 - Math.abs (dX) / ( viewHolder.itemView.getWidth ());
        foregroundView.setAlpha (alpha);
        foregroundView.setTranslationX (dX);
    }

    recentconfrag.swipedlog=true;

    getDefaultUIUtil().onDraw(c, recyclerView, foregroundView, dX, dY,
            actionState, isCurrentlyActive);
}

@Override
public void onSwiped(@NonNull RecyclerView.ViewHolder viewHolder, int direction) {
    listener.onSwiped(viewHolder, direction, viewHolder.getAdapterPosition());


}

@Override
public int convertToAbsoluteDirection(int flags, int layoutDirection) {
    return super.convertToAbsoluteDirection(flags, layoutDirection);
}

public interface RecyclerItemTouchHelperListener {
    void onSwiped(RecyclerView.ViewHolder viewHolder, int direction, int position);

}

}

共有1个答案

袁波
2023-03-14

我不知道,哪里出了问题,我在模拟器和三星Galaxy S4上测试了下面的解决方案。

public class RecyclerItemTouchHelper extends ItemTouchHelper.SimpleCallback {
    private RecyclerItemTouchHelperListener listener;

    public RecyclerItemTouchHelper(int dragDirs, int swipeDirs, RecyclerItemTouchHelperListener listener) {
        super(dragDirs, swipeDirs);
        this.listener = listener;
    }

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

    @Override
    public void onSelectedChanged(RecyclerView.ViewHolder viewHolder, int actionState) {
        if (viewHolder instanceof YourAdapter.ItemViewHolder) {
            final View foregroundView = ((YourAdapter.ItemViewHolder) viewHolder).getItem();
            getDefaultUIUtil().onSelected(foregroundView);
        }
    }

    @Override
    public void onChildDrawOver(@NotNull Canvas c, @NotNull RecyclerView recyclerView,
                                @NotNull RecyclerView.ViewHolder viewHolder, float dX, float dY,
                                int actionState, boolean isCurrentlyActive) {
        if (viewHolder instanceof YourAdapter.ItemViewHolder) {
            final View foregroundView = ((YourAdapter.ItemViewHolder) viewHolder).getItem();
            getDefaultUIUtil().onDrawOver(c, recyclerView, foregroundView, dX, dY,
                actionState, isCurrentlyActive);
        }
    }

    @Override
    public void clearView(@NotNull RecyclerView recyclerView, @NotNull RecyclerView.ViewHolder viewHolder) {
        if (viewHolder instanceof YourAdapter.ItemViewHolder) {
            final View foregroundView = ((YourAdapter.ItemViewHolder) viewHolder).getItem();
            getDefaultUIUtil().clearView(foregroundView);
        }
    }

    @Override
    public void onChildDraw(@NotNull Canvas c, @NotNull RecyclerView recyclerView,
                            @NotNull RecyclerView.ViewHolder viewHolder, float dX, float dY,
                            int actionState, boolean isCurrentlyActive) {
        if (viewHolder instanceof YourAdapter.ItemViewHolder) {
            final View foregroundView = ((YourAdapter.ItemViewHolder) viewHolder).getItem();
            getDefaultUIUtil().onDraw(c, recyclerView, foregroundView, dX, dY,
                actionState, isCurrentlyActive);
        }
    }

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

    public interface RecyclerItemTouchHelperListener {
        void onSwiped(RecyclerView.ViewHolder viewHolder, int direction, int position);
    }
}

然后在一个片段中使用它。

class YourFragment : Fragment(), RecyclerItemTouchHelper.RecyclerItemTouchHelperListener {

    private var adapter: YourAdapter? = null
    private lateinit var itemTouchHelperCallback: RecyclerItemTouchHelper


    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
                              savedInstanceState: Bundle?): View? {
        val view = inflater.inflate(LAYOUT, container, false)

        adapter = YourAdapter(this)

        view.recycler_view.run {
            layoutManager = LinearLayoutManager(context)
            adapter = this@YourFragment.adapter
            setHasFixedSize(true)
            // attaching the touch helper to recycler view
            itemTouchHelperCallback =
                RecyclerItemTouchHelper(0, ItemTouchHelper.LEFT, this@YourFragment)
            ItemTouchHelper(itemTouchHelperCallback).attachToRecyclerView(this)
        }

        return view
    }

    // callback when recycler view is swiped
    // item will be removed on swiped
    override fun onSwiped(viewHolder: RecyclerView.ViewHolder?, direction: Int, position: Int) {
        // if (viewHolder is YourAdapter.ItemViewHolder) {
            // remove the item from recycler view
            // This is a custom dialog with "Yes", "No" buttons.
            showDialog("Remove the item?",
                { adapter?.removeAt(position) },
                { adapter?.notifyItemChanged(position) })
        // }
    }

请注意,我使用了位置onS⃣,但viewHolder.getAdapterPosition()也是正确的。我使用了一个解决方案,用于从Android RecyclView ItemTouchHelper恢复滑动和恢复视图持有人的项目。

 类似资料:
  • 我有一个活动,在创建时,我创建了五个文件,然后读取这些文件并添加到我的recyclerview中。我还在我的recyclerview中添加了滑动功能。当一个项目被滑动时,它删除了recycler视图中该位置的单元格,同时也删除了我创建的文件。我面临的问题是,假设在我的recyclerview中有5个项目1、2、3、4和5,我滑动鼠标删除1,包含1的文件被删除,但在我的recyclerview中,当

  • 这是我的活动: 卡片xml 活动xml 这是我的自定义适配器 我已经看了很多关于如何让它工作的指南,但它仍然不起作用。有人知道发生了什么事吗?我的版本是25.0。1,并导入所有模块。但它并没有在布局中添加一张卡片。

  • 大家好我需要帮助我第一次使用android studio java。。。这是我的界面 在此输入图像描述 我想恢复列表中的EditText值时,用户点击"valider la命令"

  • 我的应用程序中有一个Recolyer视图,我从用户那里获取值并将其存储在SQLite数据库中,然后这些值将通过检索显示在Recolyer视图中。问题是回收器视图只有在我重新打开我的活动时才更新,所以我如何在不重新打开活动的情况下刷新回收器视图,我应该使用UI线程吗?图像:插入项目后 在重新打开activty后,当用户单击insert按钮时,我想在recyclearview上显示插入的项目

  • 我正在使用房间数据库填充RecyclerView。在这些设置中,用户可以备份和恢复基础sqlite数据库。备份/恢复是一种简单的复制机制,如下所示。 我将数据库恢复到位置,这完全正常。但是,如果用户退出设置活动,则不会刷新RecyclerView。 我想我必须告诉房间DB已经改变了。但我该怎么做呢?谢谢

  • 只是检查主活动. xml我正在做的是在按钮下方显示回收器视图并编辑文本...编辑文本的值被制成卡片,但问题是按钮不工作我不知道为什么请帮助 我是新的协调员的意见,所以请原谅我,如果我做一些新手的错误 AdapterEx.java 初始化回收器视图 主活动. xml 卡片视图. xml