【问题标题】:Why is the Spinner's interface SpinnerPopup private? Why can't I add my own popup view?为什么 Spinner 的接口 SpinnerPopup 是私有的?为什么我不能添加自己的弹出视图?
【发布时间】:2013-11-27 11:14:15
【问题描述】:

我正在寻找一种回答 this question 的方法,其中 OP 试图限制微调器下拉视图中显示的项目数量。好像做不到。

Spinner 类有自己的私有接口,称为SpinnerPopup,它定义了下拉项的显示方式。这目前基于spinnerMode 允许下拉或对话框列表。

这两个选项也在 Spinner 类中作为私有类实现:DialogPopup 和 DropdownPopup。所以在我看来,自定义它以添加另一个弹出选项的唯一方法是复制 spinner source code 并创建我自己的版本。

但如果SpinnerPopup 接口是公开的,那么似乎很容易:

  1. 创建我自己的弹出窗口,实现SpinnerPopup;和
  2. 创建我自己的微调器来扩展原始微调器,在该微调器中我覆盖构造函数 Spinner(Context context, AttributeSet attrs, int defStyle, int mode) 来处理我的弹出窗口。

有没有人知道(或猜测)为什么不是这样?还是我在这里错过了一个更简单的解决方案?

谢谢!

【问题讨论】:

    标签: android android-widget android-spinner


    【解决方案1】:

    我需要将自己的视图设置为下拉列表,所以这就是我所做的:

    public class SpinnerAdapter_Tabela_Preco extends ArrayAdapter<Tabela_Preco>{ 
    
        // Your sent context
        private Context context;
        private List<Tabela_Preco> list_tabela_preco;
    
        public SpinnerAdapter_Tabela_Preco(Context context, int textViewResourceId, List<Tabela_Preco> values) {
            super(context, textViewResourceId, values);
            this.context = context;
            this.list_tabela_preco = values;
        }
    
        public int getCount(){
           return list_tabela_preco.size();
        }
    
        public Tabela_Preco getItem(int position){
           return list_tabela_preco.get(position);
        }
    
        public long getItemId(int position){
           return position;
        }
    
    
        // And the "magic" goes here
        // This is for the "passive" state of the spinner
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            Tabela_Preco t_p = getItem(position);
    
            // I created a dynamic TextView here, but you can reference your own  custom layout for each spinner item
            TextView label = new TextView(context);
            label.setTextColor(Color.BLACK);
            label.setTypeface(null, Typeface.BOLD);
    
            // Then you can get the current item using the values array (Users array) and the current position
            // You can NOW reference each method you has created in your bean object (User class)
            label.setText(t_p.toString());
    
            // And finally return your dynamic (or custom) view for each spinner item
            return label;
        }
    
        // And here is when the "chooser" is popped up
        // Normally is the same view, but you can customize it if you want
        @Override
        public View getDropDownView(int position, View convertView, ViewGroup parent) {
            Tabela_Preco t_p = getItem(position);
    
            TextView label = new TextView(context);
            label.setTextColor(Color.BLACK);
            label.setBackgroundColor(Color.WHITE);
            label.setMinimumHeight(50);
            label.setGravity(Gravity.CENTER_VERTICAL);
            label.setTextSize(18);
            label.setText(t_p.toString());
    
            return label;
        }
    }
    

    我用这个:

    final SpinnerAdapter_Tabela_Preco adapter = new    SpinnerAdapter_Tabela_Preco(Consulta_Produto.this, android.R.layout.simple_spinner_item, lista_tabela_preco);
    
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    
    spn_tabela_preco.setAdapter(adapter);
    

    希望对你有帮助。

    【讨论】:

    • 嗨,威廉,谢谢。我知道getView 和getDropDownView 但它们自定义了弹出窗口内的项目视图。我想自定义所有弹出窗口,包括 ListView 容器本身,而不仅仅是它的项目。
    • 嗨,我可以在标签上添加填充吗?
    猜你喜欢
    • 2015-04-01
    • 2017-04-05
    • 2014-09-09
    • 1970-01-01
    • 1970-01-01
    • 2017-06-26
    • 2022-08-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多