android 2.3.3及更高版本中微调器的下拉模式

时间:2013-07-04 13:57:34

标签: android android-spinner

HIeveryone! 我查看了关于这个主题的其他问题,没有找到一个理解的答案。所以,我再问一次。我的应用必须适用于Android 2.3.3及更高版本。我有一个微调器和适配器,用于显示数据。我想在用户点击微调器时以下拉方式显示数据列表。我真的不明白我应该怎么做到这一点。

@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    _messageSpinner = (Spinner) findViewById(R.id.message_filter_spnStatus);

    ArrayList<String> messageStatuses = new ArrayList<String>();
    messageStatuses.add("All");
    messageStatuses.add("Unread");
    messageStatuses.add("Read");

    _messageSpinner.setAdapter(new MessageStatusAdapter(messageStatuses));
}

public class MessageStatusAdapter implements SpinnerAdapter
    {
        ArrayList<String> mMessageStatuses = new ArrayList<String>();
        LayoutInflater layoutInflater = getLayoutInflater();

        public MessageStatusAdapter(ArrayList<String> messageStatuses)
        {
            mMessageStatuses = messageStatuses;
        }

        @Override
        public int getCount()
        {                       
            return mMessageStatuses.size();
        }

        @Override
        public Object getItem(int arg0)
        {
            // TODO Auto-generated method stub
            return null;
        }

        @Override
        public long getItemId(int arg0)
        {
            // TODO Auto-generated method stub
            return 0;
        }

        @Override
        public int getItemViewType(int arg0)
        {
            // TODO Auto-generated method stub
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            final TextView filterName;

            if (convertView == null) {
              filterName = (TextView) layoutInflater.inflate(R.layout.item_spinner_message_status, parent, false);
            } else {
              filterName = (TextView) convertView;
            }

            filterName.setText("StatusTest");
            return filterName;
        }

        @Override
        public int getViewTypeCount()
        {
            // TODO Auto-generated method stub
            return 0;
        }

        @Override
        public boolean hasStableIds()
        {
            // TODO Auto-generated method stub
            return false;
        }

        @Override
        public boolean isEmpty()
        {
            // TODO Auto-generated method stub
            return false;
        }

        @Override
        public void registerDataSetObserver(DataSetObserver arg0)
        {
            // TODO Auto-generated method stub

        }

        @Override
        public void unregisterDataSetObserver(DataSetObserver arg0)
        {
            // TODO Auto-generated method stub

        }

        @Override
          public View getDropDownView(int position, View convertView, ViewGroup parent) {
            final TextView filterName;
            if (convertView == null) {
              filterName = (TextView) layoutInflater.inflate(R.layout.item_spinner_message_status_dropdown, parent, false);
              //filterName.setEllipsize(TruncateAt.END);
            } else {
              filterName = (TextView) convertView;
            }

            filterName.setText("DropDownItem");
            return filterName;
          } 

    }

item_spinner_message_status:

<?xml version="1.0" encoding="utf-8"?>
<TextView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:textSize="10pt"
    android:textColor="@color/text_gray" >
</TextView>

item_spinner_message_status_dropdown:

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/tvMessageStatusDropdown"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:textSize="10pt"
    android:textColor="@color/text_gray"
    android:height="50dp"
    android:gravity="center_vertical"
    android:padding="2dp" >
</TextView>

2 个答案:

答案 0 :(得分:0)

_messageSpinner = (Spinner) findViewById(R.id.message_filter_spnStatus);
String[] messageStatuses = { "All", "Unread", "Read" };
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(context, android.R.layout.simple_spinner_item, messageStatuses);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
_messageSpinner.setAdapter(dataAdapter);

onCreate()

中试用此代码段

答案 1 :(得分:-1)

您可以覆盖微调框的public boolean performClick ()方法以显示ListPopupWindow。这将显示类似于ICS样式的微调器的下拉列表。

您可以使用MessageStatusAdapter对象的setAdapter方法设置ListPopupWindow

您应该使用相关方法来显示和关闭下拉列表,例如ListPopupWindow对象的setOnItemSelectedListenersetOnItemClickListener