简体   繁体   English

更改RecyclerView所有项目的View可见性

[英]Change the View visibility of all items of RecyclerView

I have a RecyclerView who play the recording when certain item is clicked. 我有一个RecyclerView,当单击某些项目时,他会播放录音。 I want the behave when a user clicks on item1 that specific recording is playing and button View is changed which is working fine. 我希望当用户单击item1时正在播放特定的录音,并且更改了“视图”按钮,这可以正常工作。

But at the same time when item1 recording is playing and user click on item2 then item1 row Button come back to its original position. 但是同时播放item1录音时,用户单击item2,然后item1的行Button将返回到其原始位置。

Below Image show the View when item1(Row 1) is clicked. 在图像下方显示了单击item1(Row 1)时的视图。 (This is working fine) (这很好)

I have also test this to control the View in inBindViewHolder method.But it is not working because whenever I clicked holder object control the View of current selected row only. 我也测试了此方法以控制inBindViewHolder方法中的View。但是它不起作用,因为每当我单击Holder对象时,都仅控制当前选定行的View。

Below Code Section is placed in the ViewHolder 下面的代码部分放置在ViewHolder中

mPlayAudio.setOnClickListener(new View.OnClickListener() 
{
                @Override
                public void onClick(View view) 
                {
                    Log.d(TAG, "onClick: Present in onClick mPlayAudio");
                    if (listener != null) 
                    {
                        final int position = getAdapterPosition();
                        if (position != RecyclerView.NO_POSITION) 
                        {
                           // This section contain the code to play and stop 
                              the audio

                          // Using below line I only able to change selected 
                          // row button View not other row Button View  
                          mPlayAudio.setImageResource(R.drawable.play);  

                        }
                     }
                 }
});

I have also try this with in onBindViewHolder method but still not working. 我也在onBindViewHolder方法中尝试过此方法,但仍无法正常工作。

Below Code added in onBindViewHolder 下面的代码添加在onBindViewHolder中

holder.mPlayAudio.setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View view) { // This section contain code to play and stop audio //Using below statement I only able to change the //visibility of current selected row View not others holder.mPlayAudio.setImageResource(R.drawable.play); } }

So I finally was able to try this out on my own project. 因此,我终于能够在自己的项目中进行尝试。

Answer In BindViewHolder, after you have made an item click, save the value of the position of that item. 答案 BindViewHolder,你做一个项目点击后,保存项目的位置的值。 Then call notifyDataSetChanged inside the click event, which will refresh the adapter. 然后在click事件中调用notifyDataSetChanged,这将刷新适配器。 Now it obtain your result, inside BindViewHolder have an if statement checking if that value should be set accordingly (or invisible), otherwise display as visible. 现在,它获得您的结果,在BindViewHolder内部有一个if语句,检查该值是否应相应设置(或不可见),否则显示为可见。

Example Code 范例程式码

public class SelectorAdapter extends RecyclerView.Adapter<SelectorAdapter.ItemHolder> implements View.OnClickListener {

private List itemList;
private int selectedKey;

public SelectorAdapter(List list) {
    itemList = list;
}

@Override
public void onClick(View v) {

}


/* ViewHolder for each item */
class ItemHolder extends RecyclerView.ViewHolder {

    //title
    @BindView(R.id.selector_title)
    TextView title;

    @BindView(R.id.selector_layout)
    LinearLayout selector;

    ItemHolder(View itemView) {
        super(itemView);
        ButterKnife.bind(this, itemView);

    }
}


@Override
public ItemHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View itemView = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.item_layout_selector, parent, false);

    return new ItemHolder(itemView);
}


@Override
public void onBindViewHolder(ItemHolder holder, int position) {

    String title = (String) itemList.get(position);

    holder.title.setText(title);

    if (position != selectedKey) {
        holder.title.setBackgroundResource(R.drawable.selector);
    } else {
        holder.title.setBackgroundResource(R.drawable.selector_selected);
    }


    holder.itemView.setOnClickListener(v -> {
        Timber.e("selected item: %s", position);
        selectedKey = position;
        notifyDataSetChanged();
    });

}


@Override
public int getItemCount() {
    Timber.e("itemCount: %s", itemList.size());
    return itemList.size();
}


}

Here is my own project where when I select an item, it changes the background resource to selected, and then the rest are returned to the default state. 这是我自己的项目,当我选择一个项目时,它将背景资源更改为“选定”,然后将其余资源返回到默认状态。

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

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