Пользовательский SpinnerAdapter не отображается должным образом, когда элемент выбран - PullRequest
0 голосов
/ 12 мая 2019

У меня есть специальный адаптер, который отображает простой текст 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>
...