微调器上的自定义 ArrayAdapter 显示微调器外部自定义布局的图像

custom ArrayAdapter on spinner shows image of custom layout outside of the spinner

我有以下自定义数组适配器 -

public class ColorAttributeArrayAdapter extends ArrayAdapter<String> {

    private List<String> titles;
    private Context context;

    public ColorAttributeArrayAdapter(@NonNull Context context, List<String> titles) {
        super(context, R.layout.product_attribute_spinner_row_item,R.id.product_attribute_spinner_row_item_textview ,titles);
        this.titles = titles;
        this.context = context;
    }

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        View listItem = convertView;
        if (listItem == null) {
            listItem = LayoutInflater.from(context).inflate(R.layout.product_attribute_spinner_row_item, parent, false);
        }
        String currentString = titles.get(position);

        //Setting the image color
        ImageView imageView = listItem.findViewById(R.id.product_attribute_spinner_row_item_image_view);
        Map<String, String> htmlStandardColorMap = ColorUtil.getHtmlStandardColorMap();
        //if (htmlStandardColorMap.containsKey(currentString)) {
           //Toast.makeText(context, "Contains Color!", Toast.LENGTH_SHORT).show();
//            imageView.setBackgroundColor(Color.parseColor(htmlStandardColorMap.get(currentString)));
            imageView.setBackgroundColor(Color.parseColor("#4f34eb"));
        //}
        //TODO - if not in the map do an API call to color API.

        TextView value = listItem.findViewById(R.id.product_attribute_spinner_row_item_textview);
        value.setText(currentString);

        return listItem;
    }
}

使用以下自定义布局 -

<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="80dp">

    <TextView
        android:id="@+id/product_attribute_spinner_row_item_textview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        tools:text="Attribute item title"
        android:textSize="18sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <ImageView
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_marginStart="16dp"
        android:id="@+id/product_attribute_spinner_row_item_image_view"
        android:layout_marginLeft="16dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:src="@mipmap/ic_launcher" />
</android.support.constraint.ConstraintLayout>

所以我想要实现的是在文本视图旁边填充图像的行项目。

我得到的是以下内容 -

出于某种原因,图像不是显示在每个行项目中,而是显示在当前从微调器中选择的值之上。

为什么会这样?

编辑 -

这是我编辑的解决方案,但我看不到文字 -

@Override
    public void setProductPurchaseAttributes() {
        selectedProductAttributesMap = selectedProduct.getAttributesList();
        int startingIndex = 6;
        if (!isProductAvailable) return;
        for (Map.Entry<String, List<String>> entry : selectedProductAttributesMap.entrySet()) {
            //creating the linear layout
            LinearLayout linearLayout = new LinearLayout(this);
            linearLayout.setOrientation(LinearLayout.HORIZONTAL);
            //creating the layout params
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            LinearLayout.LayoutParams spinnerParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            params.setMargins(25,30,25,0);
            linearLayout.setLayoutParams(params);
            //creating the text view
            TextView textView = new TextView(this);
            textView.setText(entry.getKey().concat(":"));
            textView.setLayoutParams(params);
            //creating the spinner
            Spinner spinner = new Spinner(this);
            spinner.setLayoutParams(spinnerParams);
//            ArrayAdapter<String> adapter = new ArrayAdapter<>(getApplicationContext(), android.R.layout.simple_spinner_dropdown_item, entry.getValue());
            List<String> value = entry.getValue();
            value.clear();
            value.add("blanchedalmond");
            value.add("blanchedalmond");
            value.add("blanchedalmond");
            value.add("blanchedalmond");
            value.add("blanchedalmond");
            ColorAttributeArrayAdapter adapter = new ColorAttributeArrayAdapter(this, value);
            spinner.setAdapter(adapter);
//            adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            //adding to the linear layout
            linearLayout.addView(textView);
            linearLayout.addView(spinner);
            //adding linear layout to root view
            productDetailsViewGroup.addView(linearLayout, startingIndex);
            startingIndex++;
        }
    }
public class ColorAttributeArrayAdapter extends ArrayAdapter<String> {

    private List<String> titles;
    private Context context;

    public ColorAttributeArrayAdapter(@NonNull Context context, List<String> titles) {
        super(context, R.layout.product_attribute_spinner_row_item,R.id.product_attribute_spinner_row_item_textview ,titles);
//        super(context, 0,titles);
        this.titles = titles;
        this.context = context;
    }

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        View listItem = convertView;
        if (listItem == null) {
            listItem = LayoutInflater.from(context).inflate(R.layout.product_attribute_spinner_row_item, parent, false);
        }
        String currentString = titles.get(position);

        //Setting the image color
        ImageView imageView = listItem.findViewById(R.id.product_attribute_spinner_row_item_image_view);
        //if (htmlStandardColorMap.containsKey(currentString)) {
        //Toast.makeText(context, "Contains Color!", Toast.LENGTH_SHORT).show();
//            imageView.setBackgroundColor(Color.parseColor(htmlStandardColorMap.get(currentString)));
        imageView.setBackgroundColor(Color.parseColor("#4f34eb"));
        //}
        //TODO - if not in the map do an API call to color API.

        TextView value = listItem.findViewById(R.id.product_attribute_spinner_row_item_textview);
        value.setText(currentString);

        return listItem;
    }


    @Override
    public View getDropDownView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        return getView(position, convertView, parent);
    }
}
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="80dp"
    android:padding="20dp">

    <TextView
        android:id="@+id/product_attribute_spinner_row_item_textview"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        tools:text="Attribute item title"
        android:textSize="18sp"
        android:text="Attribute item title"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toEndOf="@+id/product_attribute_spinner_row_item_image_view"
        app:layout_constraintTop_toTopOf="parent" />

    <ImageView
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_marginStart="16dp"
        android:id="@+id/product_attribute_spinner_row_item_image_view"
        android:layout_marginLeft="16dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:src="@mipmap/ic_launcher" />
</android.support.constraint.ConstraintLayout>

使用这个编辑后的解决方案,我可以看到以下内容 -

我错过了什么?

而不是图像和文本视图,使用 TextView

的 Drawable-left 属性
  TextView txt1 = convertView.findViewById(R.id.product_attribute_spinner_row_item_image_view);
    Drawable img  = ContextCompat.getDrawable(parent.getContext(), R.mipmap.ic_launcher);
    img.setBounds(0,0,80,80);
    txt1.setCompoundDrawables(img, null, null, null);txt1.setCompoundDrawablePadding(5);

如果您要使用 ImageView,请使用 match-parent 属性 而不是 wrap-content 作为微调器的宽度和高度。

试试下面的布局:

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="80dp">

    <ImageView
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_marginStart="16dp"
        android:id="@+id/product_attribute_spinner_row_item_image_view"
        android:padding="10dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:src="@mipmap/ic_launcher" />

    <TextView
        android:id="@+id/product_attribute_spinner_row_item_textview"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        tools:text="Attribute item title"
        android:textSize="18sp"
        android:layout_marginLeft="5dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toEndOf="@+id/product_attribute_spinner_row_item_image_view"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

Adapter。应该覆盖 getDropDownView 以获得下拉列表中的 image

class ColorAttributeArrayAdapter extends ArrayAdapter<String> {

    private List<String> titles;
    private Context context;

    public ColorAttributeArrayAdapter(@NonNull Context context, List<String> titles) {
        super(context, R.layout.product_attribute_spinner_row_item, R.id.product_attribute_spinner_row_item_image_view, titles);
        this.titles = titles;
        this.context = context;
    }

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        View listItem = convertView;
        if (listItem == null) {
            listItem = LayoutInflater.from(context).inflate(R.layout.product_attribute_spinner_row_item, parent, false);
        }
        String currentString = titles.get(position);

        //Setting the image color
        ImageView imageView = listItem.findViewById(R.id.product_attribute_spinner_row_item_image_view);
        //if (htmlStandardColorMap.containsKey(currentString)) {
        //Toast.makeText(context, "Contains Color!", Toast.LENGTH_SHORT).show();
//            imageView.setBackgroundColor(Color.parseColor(htmlStandardColorMap.get(currentString)));
        imageView.setBackgroundColor(Color.parseColor("#4f34eb"));
        //}
        //TODO - if not in the map do an API call to color API.

        TextView value = listItem.findViewById(R.id.product_attribute_spinner_row_item_textview);
        value.setText(currentString);

        return listItem;
    }

    @Override
    public View getDropDownView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        return getView(position, convertView, parent);
    }
}

应该使用 match_parent 到 Spinner

的 `layout_width'
<androidx.appcompat.widget.AppCompatSpinner
    android:id="@+id/spinner"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>

输出: