【问题标题】:Extend RecyclerView and get clicked item's position扩展 RecyclerView 并获取点击项的位置
【发布时间】:2022-01-02 06:22:28
【问题描述】:

我正在尝试扩展 RecyclerView 以进行我自己的某些修改。这是我目前拥有的:

public class FeedRecyclerView extends RecyclerView {

    private int currentClickedItem = -1;

    public FeedRecyclerView(@NonNull Context context) {
        super(context);
        init();
    }

    public FeedRecyclerView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public FeedRecyclerView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    // Check if item in RecyclerView is clicked
    // If so, update "currentClickedItem" to the correct position

}

当用户点击 RecyclerView 中的某个项目时,我想更新上面代码中currentClickedItem 的值。我会用什么方法来做到这一点?

谢谢!

【问题讨论】:

  • 这可以通过使用适配器和一个接口来完成,为什么你需要一个自定义的 Recyclerview。好像走错了方向。
  • @RajenRaiyarela 我稍后将在 RecyclerView 中使用此值来跟踪 RecyclerView 中的哪些项目在滚动时在屏幕上可见,并将其与 currentClickedItem 的值进行比较。
  • 抱歉,我无法理解您究竟想要达到什么目的。也许其他人会指导/帮助你。
  • 我很感激。

标签: android android-recyclerview


【解决方案1】:

你可以试试这样的:

rv_list.addOnItemTouchListener(
            new RecyclerItemClickListener(activity, new RecyclerItemClickListener.OnItemClickListener() {
                @Override
                public void onItemClick(View v, int position) {

                    Toast.makeText(activity, "" + position, Toast.LENGTH_SHORT).show();
                }
            })
    );

这将为您获取点击项目的位置。

【讨论】:

  • 什么是RecyclerItemClickListener
猜你喜欢
  • 1970-01-01
  • 2015-04-02
  • 1970-01-01
  • 1970-01-01
  • 2018-12-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多