我在代码中看到了我自己的自定义适配器微调器。 一切正常,只是图像从左侧附加在每个值上,而只附加到选定的值。 在下图中,这是我得到的:
这是我的代码:
public class MyAdapter extends ArrayAdapter<String> {
private Context context;
private String[] values;
public MyAdapter(Context ctx, int txtViewResourceId, String[] objects) {
super(ctx, txtViewResourceId, objects);
context = ctx;
values = objects;
}
@Override
public View getDropDownView(int position, View cnvtView, ViewGroup prnt) {
return getCustomView(position, cnvtView, prnt);
}
@Override
public View getView(int pos, View cnvtView, ViewGroup prnt) {
return getCustomView(pos, cnvtView, prnt);
}
public View getCustomView(int position, View convertView,ViewGroup parent) {
LayoutInflater inflater = LayoutInflater.from(context);
View mySpinner = inflater.inflate(R.layout.custom_spinner, parent,
false);
TextView subSpinner = (TextView) mySpinner.findViewById(R.id.sub_text_seen);
subSpinner.setText(values[position]);
ImageView right_icon = (ImageView) mySpinner.findViewById(R.id.left_pic);
return mySpinner;
}
}
这是适配器的初始化:
spinnerduration.setAdapter(new MyAdapter(this, R.layout.custom_spinner, weeks_array));
spinnerduration.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
/*TextView selectedText = (TextView) parent.getChildAt(0);
if (selectedText != null) {
selectedText.setTextColor(Color.WHITE);
}*/
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
/*TextView selectedText = (TextView) parent.getChildAt(0);
if (selectedText != null) {
selectedText.setTextColor(Color.WHITE);
}*/
}
});
答案 0 :(得分:1)
您需要迭代可见项目,如果未选中它们,则应删除图像:
selectedItem.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
如果已选中,则需要设置三角形绘图:
selectedItem.setCompoundDrawablesWithIntrinsicBounds(R.drawable.icon, 0, 0, 0);