简介
下拉框里显示所有item,但限制部分ietm不可点击
效果图
![](https://i-blog.csdnimg.cn/blog_migrate/cb91d90b6f250c25e8a9b308f2138671.jpeg)
![](https://i-blog.csdnimg.cn/blog_migrate/549f2922af371bf62170c5b206bb662d.jpeg)
功能实现
(MainActivity.java)
/**
* 初始化下拉框
*/
Spinner spinner = findViewById(R.id.spinner);
private void initSpinner() {
List<String> strList = new ArrayList<String>();
Map<String, String> map = new HashMap<>();
map.put("0", "item1");
map.put("1", "item2");
map.put("2", "item3");
map.put("3", "item4");
map.put("4", "item5");
for (int i = 0; i < 5; i++) {
MapBean bean = new MapBean();
bean.setValue(map.get(i + ""));
strList.add(bean.getValue());
}
ArrayAdapter<String> adpter = new CustomAdpter<String>(getActivity(), android.R.layout.simple_spinner_item, strList) {
@Override
public boolean isEnabled(int position) {
//此处设置第二个和第四个item为不可点击
if (position == 1 || position == 3) {
return false;
}
return true;
}
@Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
TextView textView = (TextView) super.getDropDownView(position, convertView, parent);
//设置下拉框里字体的颜色
if (position == 1 || position == 3) {
textView.setTextColor(Color.rgb(222, 222, 222));
} else {
textView.setTextColor(Color.BLACK);
}
return textView;
}
};
//绑定 Adapter到控件
WidgetUtils.setSpinnerDropDownVerticalOffset(spinner);
spinner.setAdapter(adpter);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
//显示所选的item
XToastUtils.success(map.get(i + ""));
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
}
(CustomAdpter.java)
public class CustomAdpter<T> extends ArrayAdapter<T> {
public CustomAdpter(Context ctx, int id, List<T> objects) {
super(ctx, id, objects);
}
// 其它构造函数
@Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
View view = super.getView(position, convertView, parent);
TextView text = (TextView) view.findViewById(android.R.id.text1);
text.setTextColor(Color.BLACK);
return view;
}
}