spinner 的使用

   private void initTitleSpinner() {
        final ArrayList<String> titleNameList = new ArrayList<String>();
        titleNameList.add(SIMPLE_VERSION_FEEDBACK);
        titleNameList.add(PROFESSIONAL_VERSION_FEEDBACK);

        final ArrayAdapter<String> titleSpinnerAdapter = new ArrayAdapter<String>(this, R.layout.title_spinner_style, titleNameList) {
            @Override
            public View getDropDownView(int position, View convertView, ViewGroup parent) {
                View view = (getLayoutInflater().inflate(R.layout.title_spinner_view, parent, false));

                TextView item = (TextView) view.findViewById(R.id.title_spinner_item);
                item.setText(getItem(position));
                if (titleSpinner.getSelectedItemPosition() == position) {
//                    view.setBackgroundColor(getResources().getColor(R.color.screenshot_spinner_selected));
                    item.setTextColor(Color.RED);
                }
                return view;
            }
        };
        titleSpinner.setAdapter(titleSpinnerAdapter);
        titleSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

            public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
                String titleName = null;
                if (titleNameList.size() > arg2) {
                    titleName = titleNameList.get(arg2);
                }
                if (titleName != null && titleName.equals(SIMPLE_VERSION_FEEDBACK)) {
                    FeedbackOverallMgr.getInstance().unSetFeedbackProfessional();
                } else if (titleName != null && titleName.equals(PROFESSIONAL_VERSION_FEEDBACK)) {
                    FeedbackOverallMgr.getInstance().setFeedbackProfessional();
                }

                FeedbackOverallMgr.addUserTrack(FeedbackOverallMgr.UserTrackState.POINT_TITLE_SPINNER_CLICK);
            }

            public void onNothingSelected(AdapterView<?> arg0) {
                // TODO Auto-generated method stub

            }
        });

        if(FeedbackOverallMgr.getInstance().isFeedbackProfessional())
        {
            if(titleNameList.size() >= 2)
            {
                titleSpinner.setSelection(1);
            }
        }
        else
        {
            if(titleNameList.size() >= 1)
            {
                titleSpinner.setSelection(0);
            }
        }
    }

其中R.layout.title_spinner_style 文件是自定義spinner樣式,該佈局文件如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView android:id="@+id/title_spinner_item"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:textColor="#FFFFFF"
        android:textSize="14dp"
        android:background="@color/screenshot_blue"
        android:paddingBottom="5dp"
        android:paddingTop="5dp"/>

</LinearLayout>

其中R.layout.title_spinner_view文件是對spinner的下拉框樣式的自定義,如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView android:id="@+id/title_spinner_item"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:textColor="#FFFFFF"
        android:textSize="14dp"
        android:background="@color/screenshot_blue"
        android:paddingBottom="5dp"
        android:paddingTop="5dp"/>

</LinearLayout>
發佈了74 篇原創文章 · 獲贊 3 · 訪問量 6萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章