旋转器和旋转器项目的不同视图?
有什么方法可以为关闭的微调器视图和微调器项目视图设置不同的视图吗?
我假设 ArrayAdapter 中使用的资源 ID 将用于关闭的项目视图以及项目视图,因此我扩展了 ArrayAdapter 并定义了>getView 使用了不同的资源,但是调用超级构造函数中的资源 id 似乎从未被使用过,只有 getView
中使用的资源 id 似乎被使用
在 Spinner.java 代码 它指出:
微调器适配器允许定义两种不同的视图:一种显示微调器本身中的数据,另一种在按下微调器时显示下拉列表中的数据。
但鉴于代码,这似乎不可能。
无论如何 - 我的代码:
public class CustomArrayAdapter <T> extends ArrayAdapter<T> {
int itemViewResourceId;
private LayoutInflater inflater;
ViewPopulator<T> viewPopulator;
private List<T> objects;
public CustomArrayAdapter(Context context, int textViewResourceId, int itemViewResourceId, ViewPopulator<T> viewPopulator, List<T> objects) {
super(context, textViewResourceId, objects);
inflater = LayoutInflater.from(context);
this.viewPopulator = viewPopulator;
this.itemViewResourceId = itemViewResourceId;
this.objects = objects;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView == null || convertView.getTag() == null) {
// new view - populate
convertView = inflater.inflate(itemViewResourceId, parent, false);
convertView.setTag(new Object());
}
viewPopulator.populateView(position, convertView, parent, objects.get(position));
return convertView;
}
}
public abstract class ViewPopulator<T> {
public abstract void populateView(int position, View convertView, ViewGroup parent, T item);
}
调用方式:
CustomArrayAdapter<T> typeAdapter = new CustomArrayAdapter<T>(context, R.layout.list_item, R.layout.list_item_big, new ViewPopulator<T>() {
@Override
public void populateView(int position, View convertView, ViewGroup parent, T item) {
((TextView) convertView.findViewById(R.id.list_item)).setText(position + " - " + item.getName());
}
}, itemsByType.get(type));
** 编辑 **
使用的资源 ID 是在 getView
方法中定义的 itemViewResourceId
- 向 CustomArrayAdapter
添加新方法,重写 getDropDownView
如下所示,为我提供与用于所有样式的 itemViewResourceId
相同的结果,以及 < code>textViewResourceId 根本没有被使用。但是,删除 getView
会导致使用 textViewResourceId
- 因此我认为 getDropDownView
实际上没有执行任何操作:
@Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
if(convertView == null || convertView.getTag() == null) {
// new view - populate
convertView = inflater.inflate(itemViewResourceId, parent, false);
convertView.setTag(new Object());
}
viewPopulator.populateView(position, convertView, parent, objects.get(position));
return convertView;
}
Is there any way I can set a different view for the closed spinner view and the spinner item view?
I assume that the resource id used in the ArrayAdapter
would be used for the closed item view, as well as the item views, so I extended the ArrayAdapter
and defined the getView
which uses a different resource, but the resource id in the call to the super constructor doesn't seem to be used ever, only the resource id used in getView
seems to be used
In the Spinner.java code it states:
A spinner adapter allows to define two different views: one that shows the data in the spinner itself and one that shows the data in the drop down list when the spinner is pressed.
but it doesn't seem possible given the code.
Anyway - my code:
public class CustomArrayAdapter <T> extends ArrayAdapter<T> {
int itemViewResourceId;
private LayoutInflater inflater;
ViewPopulator<T> viewPopulator;
private List<T> objects;
public CustomArrayAdapter(Context context, int textViewResourceId, int itemViewResourceId, ViewPopulator<T> viewPopulator, List<T> objects) {
super(context, textViewResourceId, objects);
inflater = LayoutInflater.from(context);
this.viewPopulator = viewPopulator;
this.itemViewResourceId = itemViewResourceId;
this.objects = objects;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView == null || convertView.getTag() == null) {
// new view - populate
convertView = inflater.inflate(itemViewResourceId, parent, false);
convertView.setTag(new Object());
}
viewPopulator.populateView(position, convertView, parent, objects.get(position));
return convertView;
}
}
public abstract class ViewPopulator<T> {
public abstract void populateView(int position, View convertView, ViewGroup parent, T item);
}
called with:
CustomArrayAdapter<T> typeAdapter = new CustomArrayAdapter<T>(context, R.layout.list_item, R.layout.list_item_big, new ViewPopulator<T>() {
@Override
public void populateView(int position, View convertView, ViewGroup parent, T item) {
((TextView) convertView.findViewById(R.id.list_item)).setText(position + " - " + item.getName());
}
}, itemsByType.get(type));
** EDIT **
The resource id used is the itemViewResourceId
defined in the getView
method -
adding a new method to CustomArrayAdapter
, overriding getDropDownView
as below give me the same results of the itemViewResourceId
being used for all the styling, and the textViewResourceId
not being used at all. However, removing the getView
results in the textViewResourceId
being used - hence I don't think that getDropDownView
actually does anything:
@Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
if(convertView == null || convertView.getTag() == null) {
// new view - populate
convertView = inflater.inflate(itemViewResourceId, parent, false);
convertView.setTag(new Object());
}
viewPopulator.populateView(position, convertView, parent, objects.get(position));
return convertView;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
如果您实现 SpinnerAdapter 会有帮助吗?
以下代码对我有用:
If you implement
SpinnerAdapter
does that help?The following code works for me:
虽然这是一个老问题,但我偶然发现试图解决同样的问题。
我使用 setDropDownViewResource :
Although this is an old question, I stumbled upon trying to solve the same problem.
I use
setDropDownViewResource
:实际上,为展开(打开)和折叠(关闭)
Spinner
获取不同的View
只需覆盖getView
和getDropdownView< /code> 你的适配器的方法。
getView(intposition,ViewconvertView,ViewGroupparent)
将生成所有用于显示所选项目的View
,当 < code>Spinner 已折叠。当您点击
Spinner
将其展开并显示下拉菜单时,getDropDownView(intposition, View ConvertView, ViewGroupparent)
会被调用 您的物品列表。您可以从此方法调用getView
(如果处于展开和折叠状态的项目相似),或者为下拉列表中的项目生成不同的View
。无论如何,如果您覆盖两个这些方法,那就太好了(即使您的
getDropdownView
只是使用相同的参数调用getView
)。并且不要忘记使用您的convertView
参数 - 它在这里是为了优化(重用现有视图而不是每次都膨胀/创建它们):Actually, getting different
View
s for expanded (opened) and collapsed (closed)Spinner
is as simple as overridinggetView
andgetDropdownView
methods of your adapter.getView(int position, View convertView, ViewGroup parent)
will produce all theView
s that will be used to display a selected item when theSpinner
is collapsed.getDropDownView(int position, View convertView, ViewGroup parent)
gets called when you click on aSpinner
to expand it and display a dropdown list of your items. You can either callgetView
from this method (if your items in expanded and collapsed states are similar), or produce a differentView
for the item in your dropdown list.It would be nice if you override both these methods anyway (even if your
getDropdownView
just callsgetView
with the same arguments). And do not forget to use yourconvertView
parameter - it is here for a purpose of optimisation (to reuse existing Views instead of inflating/creating them every time):