java微调器_java – 具有空默认选定项的微调器

巴拉克的解决方案有问题.当您选择第一个项目时,Spinner将不会调用OnItemSelectedListener的onItemSelected()并刷新空内容,因为前一个位置和选择位置都为0.

首先在字符串数组的开头放一个空字符串:

String[] test = {" ", "one", "two", "three"};

第二个构建适配器,不要修改getView(),修改getDropDownView().将空视图的高度设置为1px.

public class MyArrayAdapter extends ArrayAdapter {

private static final int ITEM_HEIGHT = ViewGroup.LayoutParams.WRAP_CONTENT;

private int textViewResourceId;

public MyArrayAdapter(Context context,

int textViewResourceId,

String[] objects) {

super(context, textViewResourceId, objects);

this.textViewResourceId = textViewResourceId;

}

@Override

public View getDropDownView(int position, View convertView, @NonNull ViewGroup parent) {

TextView textView;

if (convertView == null) {

textView = (TextView) LayoutInflater.from(getContext())

.inflate(textViewResourceId, parent, false);

} else {

textView = (TextView) convertView;

}

textView.setText(getItem(position));

if (position == 0) {

ViewGroup.LayoutParams layoutParams = textView.getLayoutParams();

layoutParams.height = 1;

textView.setLayoutParams(layoutParams);

} else {

ViewGroup.LayoutParams layoutParams = textView.getLayoutParams();

layoutParams.height = ITEM_HEIGHT;

textView.setLayoutParams(layoutParams);

}

return textView;

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值