Android 限制显示在微调器下拉列表中的项目数 [英] Android limit the no of items displayed in a spinner's dropdown list

查看:41
本文介绍了Android 限制显示在微调器下拉列表中的项目数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个微调项绑定到一个数组适配器,它可能随时有 0 个或更多项.我希望微调下拉列表一次只显示三个项目,其余项目可滚动.我曾尝试将微调器包裹在具有固定宽度的布局中,但微调器下拉列表仍然占据整个屏幕(如果阵列适配器中有那么多项目)来显示列表.

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.

推荐答案

我正在查看 Spinner 源代码,但使用 Spinner 似乎无法做到这一点.

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

Spinner 有自己的私有接口,称为 SpinnerPopup,它定义了如何显示下拉项.这目前基于 spinnerMode 允许下拉或对话框列表.

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.

这两个选项也在 Spinner 类中作为私有类实现:DialogPopupDropdownPopup.由于您无法访问它们,在我看来,此时您唯一的选择是:

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. 基于其他小部件实现您自己的自定义微调器,例如本示例.
  2. 复制代码 来自 Spinner 类,它看起来非常独立,并使用它实现您的微调器版本,修改您喜欢的任何内容.
  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.

很抱歉我帮不上忙了.

祝你好运!

如果您选择选项 2,我认为您需要做的就是添加实现 SpinnerPopup 接口的模式.然后在构造函数 Spinner(Context context, AttributeSet attrs, int defStyle, int mode) 中添加另一个 caseswitch 检查模式实例化您自己的弹出窗口.看起来不难.

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.

这篇关于Android 限制显示在微调器下拉列表中的项目数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆