Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
547 views
in Technique[技术] by (71.8m points)

Android limit the no of items displayed in a spinner's dropdown list

I have a spinner item bound to an array adapter which might have 0 or more items at any time. I want the spinner dropdown list to only show three items at a time, the rest of the items being scrollable. I have tried wrapping the spinner within a layout with a fixed width but the spinner drop down list still takes up the entire screen(if there are that many items in the array adapter) to display the list.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I was looking at the Spinner source code and it seems like you can't do that with a spinner.

The Spinner has its own private interface called SpinnerPopup which defines how dropdown items can be shown. This is currently based on the spinnerMode allowing for a dropdown or dialog list.

Both options are also implemented inside the Spinner class as private classes: DialogPopup and DropdownPopup. Since you can't access them, it seems to me your only options at this point are:

  1. Implement your own custom spinner based on other widgets such as in this example.
  2. Copy the code from the Spinner class which seems pretty self-contained and implement your version of a spinner with it, modifying whatever you like in it.

I'm sorry I couldn't be of more help.

Good luck!

EDIT:

If you choose option 2, I think all you need to do is add your mode implementing the SpinnerPopup interface. Then inside the constructor Spinner(Context context, AttributeSet attrs, int defStyle, int mode) add another case to the switch checking for the modes to instantiate your own popup. Doesn't seem hard.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...