开发者社区> 问答> 正文

如何更改Recyclerview中其他项目中按钮的可见性?

我有项目里面的按钮的recyclerview。当我单击其中一个按钮时,我想隐藏其他项目中的按钮。我知道如何在单击按钮的同一项目中执行此操作,但是我不知道如何在不触摸它们的情况下执行其他操作。怎么做?我尝试在ViewHolder中使用布尔值,但是它不起作用。


public class ListAdapter extends RecyclerView.Adapter<ListAdapter.ListViewHolder> {

private List<Task> tasks;
private boolean visible;

public class ListViewHolder extends RecyclerView.ViewHolder {
    private TextView nameTextView, idTextView;
    private Button travelButton, travellingButton, workButton, stopButton;

    public ListViewHolder(@NonNull View itemView) {
        super(itemView);
        nameTextView = itemView.findViewById(R.id.name_text_view);
        idTextView = itemView.findViewById(R.id.id_text_view);
        travelButton = itemView.findViewById(R.id.travel_button);
        travellingButton = itemView.findViewById(R.id.travelling_button);
        workButton = itemView.findViewById(R.id.work_button);
        stopButton = itemView.findViewById(R.id.stop_button);

        if (visible == true) {
            travelButton.setVisibility(View.GONE);
        }
        else {
            travelButton.setVisibility(View.VISIBLE);
        }
    }
}

@NonNull
@Override
public ListViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_list, parent, false);
    return new ListViewHolder(view);
}

@Override
public void onBindViewHolder(@NonNull final ListAdapter.ListViewHolder holder, final int position) {
    Task task = tasks.get(position);

    holder.nameTextView.setText(task.getName());
    holder.idTextView.setText(String.valueOf(task.getId()));
    holder.travelButton.setVisibility(View.VISIBLE);
    holder.travelButton.setText(R.string.start_travel);
    holder.travellingButton.setText(R.string.travelling);

    holder.travelButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            holder.travelButton.setVisibility(View.GONE);
            holder.travellingButton.setVisibility(View.VISIBLE);
            holder.workButton.setVisibility(View.VISIBLE);
            holder.itemView.setBackgroundColor(Color.parseColor("#FFECB3"));
            visible = true;
        }
    });
    holder.workButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            holder.workButton.setText(R.string.working);
            holder.travelButton.setVisibility(View.GONE);
            holder.travellingButton.setVisibility(View.GONE);
            holder.stopButton.setVisibility(View.VISIBLE);
            holder.itemView.setBackgroundColor(Color.parseColor("#FFCDD2"));
        }
    });
    holder.stopButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            holder.travelButton.setText(R.string.start_travel);
            holder.travelButton.setVisibility(View.VISIBLE);
            holder.travellingButton.setVisibility(View.GONE);
            holder.workButton.setText(R.string.work);
            holder.workButton.setVisibility(View.GONE);
            holder.stopButton.setVisibility(View.GONE);
            holder.itemView.setBackgroundColor(Color.TRANSPARENT);
            visible = false;
        }
    });

}

@Override
public int getItemCount() {
    return tasks == null ? 0 : tasks.size();
}

public void setTasks(List<Task> tasks) {
    this.tasks = tasks;
    notifyDataSetChanged();
}
}

展开
收起
Puppet 2020-01-17 09:12:13 512 0
1 条回答
写回答
取消 提交回答
  • 将此添加到onBindViewHolder:

    
    holder.travellingButton.setVisibility(visible? View.VISIBLE : View.GONE);
    

    并更改“可见”通话后

    notifyItemRangeChanged(0, getItemCount())
    
    2020-01-17 09:12:32
    赞同 展开评论 打赏
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载