我有一个自定义微调器适配器,该适配器在下拉菜单中显示简单的img +文本。 但是,对于某些物品,应该按如下所述隐藏图像。
我的问题是,由于某种原因,当选择了应隐藏图像的项目时, setVisibility(GONE)似乎根本没有任何作用。
知道为什么吗? 当该项目不是下拉菜单中的选定项目时,图像消失得很好。 仅当我使用ConstraintLayout而不是RelativeLayout时,这种情况才会发生。
这是代码。
public class CloudListAdapter extends ArrayAdapter<CloudDisplayData> {
private List<CloudDisplayData> data;
private int resource;
private LayoutInflater inflater;
private Context context;
public CloudListAdapter(@NonNull Context context, int resource, List<CloudDisplayData> data) {
super(context, resource);
this.data = data;
this.resource = resource;
this.context = context;
inflater = LayoutInflater.from(context);
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public int getCount() {
return data.size();
}
@Nullable
@Override
public CloudDisplayData getItem(int position) {
return data.get(position);
}
@Override
public View getDropDownView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
View result;
if (convertView == null) {
result = inflater.inflate(resource, parent, false);
}else{
result=convertView;
}
ImageView img = result.findViewById(R.id.img_server);
TextView text = result.findViewById(R.id.label_server);
text.setText(data.get(position).name);
if (data.get(position).img_resource > 0) {
if (data.get(position).tintOn) {
img.setColorFilter(ContextCompat.getColor(context, R.color.white));
} else {
img.setColorFilter(null);
}
img.setVisibility(VISIBLE);
img.setImageResource(data.get(position).img_resource);
} else {
img.setVisibility(GONE);
}
text.setTextColor(ContextCompat.getColor(context, R.color.white));
return result;
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
if (convertView == null)
convertView = inflater.inflate(resource, parent, false);
TextView text = convertView.findViewById(R.id.label_server);
text.setText(data.get(position).name);
text.setTextColor(ContextCompat.getColor(context, R.color.colorPrimary));
return text;
}
}
资源就是这样:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/cl_server"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorPrimaryDark"
android:padding="10dp">
<ImageView
android:id="@+id/img_server"
android:layout_width="32dp"
android:layout_height="32dp"
android:contentDescription="@string/img_content_description"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/cloud" />
<TextView
android:id="@+id/label_server"
android:layout_width="wrap_content"
android:layout_height="32dp"
android:gravity="start|center_vertical"
android:paddingLeft="10dp"
android:text="@string/label_zotero"
android:textColor="@color/white"
android:textSize="16sp"
app:layout_constraintLeft_toRightOf="@+id/img_server"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>