我有项目里面的按钮的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();
}
}
将此添加到onBindViewHolder:
holder.travellingButton.setVisibility(visible? View.VISIBLE : View.GONE);
并更改“可见”通话后
notifyItemRangeChanged(0, getItemCount())
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。