【问题标题】:How to get ViewHolder of specific Item of Recyclerview如何获取 Recyclerview 特定项目的 ViewHolder
【发布时间】:2016-11-15 04:33:56
【问题描述】:

有没有办法仅根据给定位置获取特定项目的 recyclerview 的ViewHolder

类似getViewHolder(position);

public MyViewHolder getViewHolder(int position){
    MyViewHolder holder = null;
    /**
        Find ViewHolder here...
            If Found initialize the holder..
                holder = ?
    **/
    return holder;
}

这个函数在我的适配器类中,我正在寻找一种方法来获取一个项目的ViewHolder,这样我就可以更改我的自定义项目eg. holder.setText("Hello World"); 的一个视图的值,我有货,我找不到这样做的方法,我试图搜索,但没有一个能帮助我。

有人吗?

【问题讨论】:

  • 请评论投反对票的原因。
  • 这可能会帮助您针对特定位置的查看器:stackoverflow.com/a/40240599/1263362
  • @Sayem - 谢谢你的链接,但你能解释一下它对我的问题有什么帮助吗?谢谢。
  • 拿到view holder后你想达到什么目的?
  • @HendraWD - 嗨!这是我想要实现的目标:我有一个在后台运行的倒数计时器,每个项目都有不同的倒数计时器和持续时间,When I close and reopen the application, the Texview that I want to show the duration is no longer updating,所以我猜我可以用Every seconds that count by the timer, Call the function in my adapter class that find the ViewHolder base on the given position, if it is found then set the textview with that holder of the current duration. 解决它,例如。 theFoundHolder.textviewDuration.setText("00:00:00").

标签: java android android-recyclerview


【解决方案1】:

这不是 ViewHolder 的工作方式。 ViewHolder 只是您制作的布局的持有者。 ViewHolder 是自动可重复使用的,因此如果您有 100 个项目,android 将不会创建 100 个 ViewHolder,而只会创建一些在屏幕上可见的 ViewHolder。如果滚动 RecyclerView,不可见项的 ViewHolder 将被重新使用。您只需要更改在方法onBindViewHolder 上自动传递的 ViewHolder 上的数据

这是一个例子

@Override
public void onBindViewHolder(final ViewHolder holder, int position) {
    holder.textViewTitle.setText(data.get(position).title);
    holder.textViewContent.setText(data.get(position).content);

    Context context = holder.button.getContext();    
    holder.button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(context, "you clicked the button!", Toast.LENGTH_SHORT).show();
        }
    });
}

编辑:为了更清楚,您应该做的是更改适配器中的特定数据,例如,您可以像这样在适配器中创建一个方法

public void changeItem(int position, ItemModel newItemModel){
    mItemModelList.remove(position);
    mItemModelList.add(position, newItemModel);
}

然后调用适配器的notifyItemChanged(int position)

【讨论】:

  • 在这里我只是想确保人们不会在 Android 中做坏事。因此,在您投反对票之前,您应该了解提问者的问题并制定更好的方法。
  • holder.button.setOnClickListener 在我的回答中并不是真正的最佳实践,但可用于将onClickListener 设置为View。最佳做法是在创建ViewHolder时设置一次onClickListener,然后使用getAdapterPosition()确定点击位置,这样就可以从该位置取回正确的数据了。
【解决方案2】:
if (your_recyclerview != null) {
        val viewholder = your_recyclerview .findViewHolderForAdapterPosition(your positions)
        if (viewholder != null){
            adapter.pausePlayer(viewholder as FeedPostDetailCommentAdapter.FeedDetailViewHolder)
        }
    }

【讨论】:

  • 这是我在 May recyclerview 适配器中的方法名称
【解决方案3】:
public static RecyclerView.ViewHolder getViewHolder(View view) {
    RecyclerView rv = getParentRecyclerView(view);
    View rvChild = getParentViewHolderItemView(view);

    if (rv != null && rvChild != null) {
        return rv.getChildViewHolder(rvChild);
    } else {
        return null;
    }
}

希望对你有所帮助。 您也可以访问此链接以供参考。

http://www.programcreek.com/java-api-examples/index.php?class=android.support.v7.widget.RecyclerView&method=getChildViewHolder

【讨论】:

  • 无法解析方法getParentRecyclerViewgetParentViewHolderItemView。根据您的代码,我怎么知道返回的 viewHolder 是我要找的那个?
  • 您在方法中传递视图以获得受尊重的视图持有者。
  • getParentRecyclerViewgetParentViewHolderItemView 无法解析。
  • 请尝试 findViewHolderForLayoutPosition(int position),如文档developer.android.com/reference/android/support/v7/widget/…
【解决方案4】:

简单的答案是

但您为强制创建ViewHolder 付出的代价是以编程方式滚动到所需位置,然后通过自定义滚动侦听器传递的回调捕获它。

大约需要 10 分钟来实现我对similar question 的回答中描述的代码。

【讨论】:

    猜你喜欢
    • 2016-07-18
    • 2017-08-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多