简体   繁体   English

如何获取Recyclerview特定Item的ViewHolder

[英]How to get ViewHolder of specific Item of Recyclerview

Is there a way to get the ViewHolder of specific Item of recyclerview base on given position only?有没有办法仅根据给定位置获取ViewHolder的特定项的ViewHolder

Something like getViewHolder(position);类似于getViewHolder(position); . .

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

This function is in my Adapter class, I'm looking for a way to get the ViewHolder of an Item so I can change the value of one of my view of my custom item eg. holder.setText("Hello World");此函数在我的 Adapter 类中,我正在寻找一种获取项目的ViewHolder的方法,以便我可以更改自定义项目的一个视图的值, eg. holder.setText("Hello World"); eg. holder.setText("Hello World"); , I'm stock, I can't find a way to do that, I tried to search but none of them help me. ,我是股票,我找不到办法做到这一点,我试图搜索但没有人帮助我。

Anyone?有人吗?

That is not how ViewHolder works.这不是 ViewHolder 的工作方式。 ViewHolder is just a holder of layout that you made. ViewHolder 只是您制作的布局的持有者。 ViewHolder is automatically re-use-able, so if you have 100 items, android will not create 100 ViewHolders, instead of just some ViewHolders that visible on the screen. ViewHolder 可以自动重用,所以如果你有 100 个项目,android 不会创建 100 个 ViewHolders,而只是一些在屏幕上可见的 ViewHolders。 If you scroll the RecyclerView, the ViewHolder of non-visible item will be re-used.如果滚动 RecyclerView,不可见项的 ViewHolder 将被重新使用。 You just need to change the data on the ViewHolder that passed automatically on method onBindViewHolder您只需要更改在方法onBindViewHolder自动传递的 ViewHolder 上的数据

Here is the example这是例子

@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();
        }
    });
}

Edit: To make it more clear, what you should do is change the specific data from your adapter, for example you can create a method in your adapter like this编辑:为了更清楚,您应该做的是更改适配器中的特定数据,例如您可以像这样在适配器中创建一个方法

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

then call notifyItemChanged(int position) of your adapter.然后调用您的适配器的notifyItemChanged(int position)

if (your_recyclerview != null) {
        val viewholder = your_recyclerview .findViewHolderForAdapterPosition(your positions)
        if (viewholder != null){
            adapter.pausePlayer(viewholder as FeedPostDetailCommentAdapter.FeedDetailViewHolder)
        }
    }
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;
    }
}

I hope it will help you.我希望它会帮助你。 For your reference you can visit this link as well.您也可以访问此链接以供参考。

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

The simple answer is yes .简单的答案是肯定的

But the price you pay for forcing ViewHolder creation is to programmatically scroll to the desired position then capture it via callback delivered from a custom scroll listener.但是,您为强制创建ViewHolder付出的代价是以编程方式滚动到所需位置,然后通过从自定义滚动侦听器传递的回调捕获它。

Should take about 10 minutes to implement the code described in my answer to a similar question .应该需要大约 10 分钟来实现我对类似问题的回答中描述的代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM