How to get ViewHolder of specific Item of Recyclerview

强颜欢笑 提交于 2019-12-11 05:57:32

问题


Is there a way to get the ViewHolder of specific Item of recyclerview base on given position only?

Something like 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");, I'm stock, I can't find a way to do that, I tried to search but none of them help me.

Anyone?


回答1:


That is not how ViewHolder works. ViewHolder is just a holder of layout that you made. 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. If you scroll the RecyclerView, the ViewHolder of non-visible item will be re-used. You just need to change the data on the ViewHolder that passed automatically on method onBindViewHolder

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.




回答2:


if (your_recyclerview != null) {
        val viewholder = your_recyclerview .findViewHolderForAdapterPosition(your positions)
        if (viewholder != null){
            adapter.pausePlayer(viewholder as FeedPostDetailCommentAdapter.FeedDetailViewHolder)
        }
    }



回答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;
    }
}

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




回答4:


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.

Should take about 10 minutes to implement the code described in my answer to a similar question.



来源:https://stackoverflow.com/questions/40602095/how-to-get-viewholder-of-specific-item-of-recyclerview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!