我有以下自定义 ArrayAdapter -
public class ColorAttributeArrayAdapter extends ArrayAdapter<String> {
private List<String> titles;
private Context context;
private OnAttributeItemClicked listener;
private MarketApiCalls marketApiCalls;
public ColorAttributeArrayAdapter(@NonNull Context context, List<String> titles, OnAttributeItemClicked listener) {
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;
this.listener = listener;
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(BASE_PORTAL_PRODUCTION_URL)
// .baseUrl(BASE_PORTAL_STAGE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
marketApiCalls = retrofit.create(MarketApiCalls.class);
}
@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)) {
imageView.setBackgroundColor(Color.parseColor(htmlStandardColorMap.get(currentString)));
} else {
String colorURL = COLORS_API.concat(Uri.encode(currentString, "UTF-8"));
Picasso.get().load(colorURL).resize(90,90).into(imageView);
}
TextView value = listItem.findViewById(R.id.product_attribute_spinner_row_item_textview);
value.setText(currentString);
value.setOnClickListener(v -> {
if (listener != null) {
listener.onClick(currentString);
}
});
return listItem;
}
@Override
public View getDropDownView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
return getView(position, convertView, parent);
}
public interface OnAttributeItemClicked{
void onClick(String attributeValue);
}
}
以及以下动态创建 Spinners 的方法 -
@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 attributeLayoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
LinearLayout.LayoutParams spinnerParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
LinearLayout.LayoutParams textParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
//setting margins
attributeLayoutParams.setMargins(48,30,48,0);
textParams.setMargins(0,60,0,0);
linearLayout.setLayoutParams(attributeLayoutParams);
//creating the text view
TextView textView = new TextView(this);
textView.setText(entry.getKey().concat(":"));
textView.setLayoutParams(textParams);
//creating the spinner
Spinner spinner = new Spinner(this);
spinner.setLayoutParams(spinnerParams);
List<String> value = entry.getValue();
//attribute list adapter
ColorAttributeArrayAdapter adapter = new ColorAttributeArrayAdapter(this, value, attributeValue -> {
Log.d("itemClicked", attributeValue);
});
spinner.setAdapter(adapter);
//adding to the linear layout
linearLayout.addView(textView);
linearLayout.addView(spinner);
//adding linear layout to root view
productDetailsViewGroup.addView(linearLayout, startingIndex);
startingIndex++;
}
}
我面临的问题是,当单击视图时,我确实触发了该方法,但没有获得微调器的默认行为。在我的 Spinner 上使用 'setOnItemSelectedListener()' 不是一个好的解决方案,因为我需要保持在该位置的特定字符串值,并且如您所见,我无法从我的活动中获取它,因为它是动态的创建微调器。
那么我该如何解决这个问题并将我自己的行为添加到微调器项单击侦听器中呢?