在工具栏中的任何事件上显示/隐藏RecyclerView中的视图 [英] Show/Hide views in RecyclerView on any event in Toolbar

查看:245
本文介绍了在工具栏中的任何事件上显示/隐藏RecyclerView中的视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在工具栏中有一个 Switch 按钮,在 RecyclerView TextViews c $ c>。

I have a Switch button in toolbar and two TextViews in RecyclerView.

我想管理 TextViews 之一的可见性> RecyclerView 基于 Switch 的状态。

I want to manage the visibility of one of the TextViews in RecyclerView based on the state of Switch.

我添加了 OnCheckedChangeListener Switch ,并将布尔值 FLAG 设置为<$ c此处 FALSE 的$ c> TRUE 。该 FLAG 值在 Adapter的 onBindViewHolder(-,-)方法中读取,然后根据 FLAG 将视图可见性设置为 VISIBLE / GONE

I have added OnCheckedChangeListener to the Switch and am setting a boolean FLAG to TRUE of FALSE here. This FLAG value is read in onBindViewHolder(-,-) method of the Adapter and I am setting the View visibility to VISIBLE/GONE based on the FLAG.

在MainActivity中:

In MainActivity:

Switch switchView;
private boolean switchFlag;

public boolean isSwitchFlag() {
    return switchFlag;
}

public void setSwitchFlag(boolean switchFlag) {
    this.switchFlag = switchFlag;
}

protected void onCreate(Bundle savedInstanceState) {
    ...

    switchView.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            setSwitchFlag(isChecked);
            adapter.notifyDataSetChanged();
            //recyclerView.refreshDrawableState()        
        }
    });

    ...
}

在适配器中:

public void onBindViewHolder(ViewHolder viewHolder, Cursor cursor) {
    if (((MainActivity) mContext).isSwitchFlag()) {
        viewHolder.textView.setVisibility(View.VISIBLE);
        ...
    }

我如何显示/隐藏视图工具栏中的任何事件在 RecyclerView 中显示?

How do I manage to show/hide views in RecyclerView on any event in Toolbar?

推荐答案

您最好有一个模型,其中包含一个文本字段和一个用于处理可见性的文件,然后将此模型的列表传递给recyclerView适配器。见下文:

You better have a model that contains a field for text and a filed for handling visibility, then pass a list of this model to the recyclerView adapter. see below:

class ListItem {
   private String text;
   private boolean isVisible;
   //...put getter and seeter methods
}

OnCheckChangeListener 您可以更改项目的可见性:

In the OnCheckChangeListener you can change visibilty of items:

switchView.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        for (ListItem item: mItems) {
            item.setVisiblity(isChecked);
        }
        adapter.notifyDataSetChanged();
    }
});

最后,在 onBindViewHolder 部分中,您可以

And finally, in onBindViewHolder section you can handle visibility of items.

public void onBindViewHolder(ViewHolder viewHolder, int position) {

    viewHolder.textView.setVisibility(mItems.get(position).isVisible() ? View.VISIBLE : View.GONE);
    viewHolder.textView.setText(mItems.get(position).getText());
    ...
}

这篇关于在工具栏中的任何事件上显示/隐藏RecyclerView中的视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆