Android: Spinner drop-down menu with selected item in top position

I have a problem with Spinner on Android. When you select an item from the drop-down list, the offset of this drop-down list will be adjusted the next time you open it. So, for example, if I select item 100 from the drop-down list 500, the next time I open the drop-down list, item 100 will be at the top of the list. This is the behavior that I want.

It seems that the problem is that I combine selector functionality with a call setSelection(int). With the following steps, I seem to have broken the bias system on the drop-down locks.

  • Open Spinner and select the second item.
  • Open Spinner again and this time release it without selecting anything.
  • Call setSelection(int)on Spinner with a value greater than 2.
  • Open Spinner a third time. Note that the offset is the same as in step 1.

I looked at the code in Spinner and AdapterView, but I do not see any public calls that I missed. Is this a bug in Spinner or a bug in my code?

+5
source share
2 answers

I think you can solve this problem by sending List to Adapter. When an item is selected, sort your list, and then use the adapter's notifyDataSetChanged () function. When you call the setSelection (int) function, sort your list again and use the notifyDataSetChanged () function.

+1
source

public void setSelection (int position, boolean animate)? , , true, . ( ) setDropDownVerticalOffset.

: Spinner API, setSelection(7, true), , , , 4 , . showToast :

private final Handler handler = new Handler();

void showToast(CharSequence msg) {
    Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
    handler.postDelayed(new Runnable(){
        public void run() {
            Toast.makeText(Spinner1.this, "auto setting", Toast.LENGTH_SHORT).show();
            Spinner s2 = (Spinner) findViewById(R.id.spinner2);
            s2.setSelection(7, true);
        }
    }, 5000);
}

:

  • "" ( ).
  • , "",
  • 5 postDelayed "" ( )
  • , .
+1

All Articles