最后选择的选项是旋转器选项的顶部

时间:2017-12-11 15:53:09

标签: java android android-spinner

我正在开发一款应用,我正在使用微调器。

但是,有很多选项供用户选择(18)。

有没有人知道是否可以对微调器进行编码,以便用户选择的最后一个选项将转到微调器选项的顶部?这样他/她就不需要再次滚动列表......

谢谢!

适配器:

 final ArrayAdapter<String> myAdapter5 = new ArrayAdapter<>(MainActivity.this,
            R.layout.spinner_layout, getResources().getStringArray(R.array.Hours));
    myAdapter.setDropDownViewResource(R.layout.spinner_layout);
    spinnerDowntimeHours.setAdapter(myAdapter5);

    spinnerDowntimeHours.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> adapterView, View view, int position2, long l) {


            switch (position2) {
                case 0:
                    break;
                case 1:
                    break;
                case 2:
                    break;
                case 3:
                    break;
                case 4:
                    break;
                case 5:
                    break;
                case 6:
                    break;
                default:
                    break;

            }
        }


        @Override
        public void onNothingSelected(AdapterView<?> adapterView) {
            String[] list = getResources().getStringArray(R.array.Hours);

            spinnerDowntimeHours.setSelection(Integer.parseInt(list[0]));
        }

    });

1 个答案:

答案 0 :(得分:0)

这是我尝试过的示例,设置微调器适配器

String[] stringArray = new String[20];
for (int i = 0; i < stringArray.length; i++) {
    stringArray[i] = "Item " + i;
}

ArrayAdapter<String> adapter = new ArrayAdapter<>(MainActivity.this,
    android.R.layout.simple_spinner_item, stringArray);
adapter.setDropDownViewResource(R.layout.spinner_item);
Spinner spinner = findViewById(R.id.spinner);
spinner.setAdapter(adapter);

spinner_item.xml

<?xml version="1.0" encoding="utf-8"?>
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/text1"
    android:background="@drawable/bg_select"
    android:singleLine="true"
    android:layout_width="match_parent"
    android:layout_height="100dp"
    android:ellipsize="marquee"/>

bg_select.xml

<?xml version="1.0" encoding="utf-8"?>

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_checked="true" android:state_pressed="true" android:drawable="@color/colorAccent" />
    <item android:state_pressed="true" android:drawable="@color/colorAccent"/>
    <item android:state_checked="true" android:drawable="@color/colorAccent"/>
    <item android:drawable="@color/colorPrimary"/>
</selector>