Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/batch-file/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何在微调器项目列表中有两个部分,具有不同的部分标题布局和项目布局_Android_Spinner_Dropdown - Fatal编程技术网

Android 如何在微调器项目列表中有两个部分,具有不同的部分标题布局和项目布局

Android 如何在微调器项目列表中有两个部分,具有不同的部分标题布局和项目布局,android,spinner,dropdown,Android,Spinner,Dropdown,我想显示一个下拉列表,它将显示城市列表。但我想分别展示前五大城市和其他城市的部分。 所以,我在下拉列表中的第一个项目是第一部分的标题,即“顶级城市”,接下来的五行项目将显示前5个城市。 第7行将显示标题为“其余城市”的标题 我无法找到解决办法,任何指导都会有帮助。提前感谢。您可以尝试以下方法: 创建一个模型类,比如说CustomSpinnerModel public class CustomSpinnerModel { public String id; public String name;

我想显示一个下拉列表,它将显示城市列表。但我想分别展示前五大城市和其他城市的部分。 所以,我在下拉列表中的第一个项目是第一部分的标题,即“顶级城市”,接下来的五行项目将显示前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

}
public类customSpinerAdapter扩展了ArrayAdapter{
私人语境;
私人充气机;
私人ArrayList喷丝头列表;
公共CustomSpinnerAdapter(@NonNull上下文上下文,int资源,@NonNull ArrayList spinnerList){
超级(上下文、资源、喷丝头列表);
this.context=上下文;
this.spinnerList=喷丝头列表;
充气器=充气器。从(上下文);
}
}
@凌驾
公共视图getDropDownView(int位置,@Nullable视图convertView,@NonNull视图组父级){
视图=转换视图;
//让你的模型就位
CustomSpinnerModel=spinnerList.get(位置);
字符串类型=model.type;
//根据类型为各个视图充气
开关(类型){
“顶级城市”案例:
视图=充气机。充气(R.layout.item\u cities\u标题,父项,false);
//执行其他操作,如设置文本等
打破
//其他病例及手术。
返回视图;
}
//其他@Override方法
}