【发布时间】:2018-08-03 16:08:27
【问题描述】:
我想显示一个下拉列表,其中将显示城市列表。但我想显示前五个城市和其他城市的单独部分。 所以,我在下拉列表中的第一项将是第一部分的标题,即“热门城市”,接下来的五行项目将显示前 5 个城市。 然后第 7 行将显示标题为“其他城市”的标题。
我无法找到解决方案,任何指导都会有所帮助。提前致谢。
【问题讨论】:
我想显示一个下拉列表,其中将显示城市列表。但我想显示前五个城市和其他城市的单独部分。 所以,我在下拉列表中的第一项将是第一部分的标题,即“热门城市”,接下来的五行项目将显示前 5 个城市。 然后第 7 行将显示标题为“其他城市”的标题。
我无法找到解决方案,任何指导都会有所帮助。提前致谢。
【问题讨论】:
您可以尝试以下方法:
创建一个模型类说CustomSpinnerModel
public class CustomSpinnerModel {
public String id;
public String name;
public boolean isSelected;
public int type;//for view type
}
像这样创建自定义微调器适配器并覆盖getDropDownView
public class CustomSpinnerAdapter extends ArrayAdapter<CustomSpinnerModel> {
private Context context;
private LayoutInflater inflater;
private ArrayList<CustomSpinnerModel> spinnerList;
public CustomSpinnerAdapter(@NonNull Context context, int resource, @NonNull ArrayList<CustomSpinnerModel> spinnerList) {
super(context, resource, spinnerList);
this.context = context;
this.spinnerList = spinnerList;
inflater = LayoutInflater.from(context);
}
}
@Override
public View getDropDownView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
View view = convertView;
//Get your model at position
CustomSpinnerModel model = spinnerList.get(position);
String type = model.type;
//Based on type inflate the respective view
switch(type){
case "Top cities" :
view = inflater.inflate(R.layout.item_cities_header, parent, false);
//Do your other operation such as setting text etc
break;
//other cases and operation.
return view;
}
//other @Override methods
}
【讨论】: