问题描述
我有一个约束布局可见性,它最初设置为GONE expandableLayout.setVisibility(View.GONE);
。我想使该视图在发出“ setonClickListener”时可见。然后,当用户再次单击布局时,视图将再次设置为“消失”。
public class ViewHolder extends RecyclerView.ViewHolder {
TextView mThicc;
ListView listView;
ConstraintLayout expandableLayout;
public ViewHolder(@NonNull View itemView) {
super(itemView);
mThicc = itemView.findViewById(R.id.thicc);
listView = itemView.findViewById(R.id.stocklist);
expandableLayout = itemView.findViewById(R.id.expandableLayout);
expandableLayout.setVisibility(View.GONE);
itemView.setonClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
expandableLayout.setVisibility(View.GONE);
}
});
}
}
问题是,当我单击布局时,它不会保持隐藏状态。
解决方法
尝试这个:
public class ViewHolder extends RecyclerView.ViewHolder {
TextView mThicc;
ListView listView;
ConstraintLayout expandableLayout;
public ViewHolder(@NonNull View itemView) {
super(itemView);
mThicc = itemView.findViewById(R.id.thicc);
listView = itemView.findViewById(R.id.stocklist);
expandableLayout = itemView.findViewById(R.id.expandableLayout);
expandableLayout.setVisibility(View.GONE);
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int visibility = expandableLayout.getVisibility() == View.GONE ? View.VISIBLE : View.GONE;
expandableLayout.setVisibility(visibility);
}
});
}
}
但是我不推荐这种方法,因为如果回收ViewHolder,它将失去其visibility
状态。您需要在列表项上保持可见性。
尝试进行调节,并检查单击按钮后视图是否可见或消失
if(expandableLayout.getVisibility==View.GONE){
expandableLayout.setVisibility(View.VISIBLE);
}
else {
expandableLayout.setVisibility(View.GONE);
}