【问题标题】:How can i have a two sections in spinner item list, with different section header layout and items layout如何在微调器项目列表中有两个部分,具有不同的部分标题布局和项目布局
【发布时间】:2018-08-03 16:08:27
【问题描述】:

我想显示一个下拉列表,其中将显示城市列表。但我想显示前五个城市和其他城市的单独部分。 所以,我在下拉列表中的第一项将是第一部分的标题,即“热门城市”,接下来的五行项目将显示前 5 个城市。 然后第 7 行将显示标题为“其他城市”的标题。

我无法找到解决方案,任何指导都会有所帮助。提前致谢。

【问题讨论】:

    标签: android spinner dropdown


    【解决方案1】:

    您可以尝试以下方法:

    创建一个模型类说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
    
    }
    

    【讨论】:

      猜你喜欢
      • 2014-01-22
      • 1970-01-01
      • 1970-01-01
      • 2013-09-22
      • 1970-01-01
      • 1970-01-01
      • 2021-10-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多