为什么Spinner的界面SpinnerPopup是私有的?为什么我不能添加自己的弹出视图?

时间:2013-11-27 11:14:15

标签: android android-widget android-spinner

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

Spinner类有自己的私有接口SpinnerPopup,它定义了如何显示下拉项。这当前基于spinnerMode允许下拉列表或对话列表。

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

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

  1. 创建我自己的弹出窗口,实现SpinnerPopup;和
  2. 创建我自己的微调器,扩展原始的微调器,覆盖构造函数Spinner(Context context, AttributeSet attrs, int defStyle, int mode)以处理弹出窗口。
  3. 有没有人有任何想法(或猜测)为什么不是这种情况?或者我在这里错过了一个更简单的解决方案?

    谢谢!

1 个答案:

答案 0 :(得分:0)

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

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);

我希望这可以帮到你。