Spinner上的setAdapter()和setDropDownViewResource()之间的区别 [英] Difference between setAdapter() and setDropDownViewResource() on Spinner

查看:89
本文介绍了Spinner上的setAdapter()和setDropDownViewResource()之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看着这个问题 android.R.layout.simple_spinner_dropdown_item和android.R.layout.simple_spinner_item 之间的区别,但找不到我的问题的答案.

I looked at this question Difference between android.R.layout.simple_spinner_dropdown_item and android.R.layout.simple_spinner_item, but couldn't find an answer to my question.

我可以看到simple_spinner_item和simple_spinner_dropdown_item之间存在差异.但是,为什么我需要同时设置它们?旋转器布局的哪一部分由setAdapter() (simple_spinner_item )中设置的布局控制,哪一部分由setDropDownViewResource() (simple_spinner_dropdown_item)中设置的布局控制?

I can see that there is a difference between simple_spinner_item and simple_spinner_dropdown_item. But why do I need to set them both? Which part of Spinner layout is controlled by layout set in setAdapter() (simple_spinner_item ) and which one by layout set in setDropDownViewResource() (simple_spinner_dropdown_item)?

现在我不明白为什么我不能做相反的事情:对setDropDownViewResource()使用simple_spinner_item,对setAdapter()使用simple_spinner_dropdown_item吗?

Right now I can't see why I can't do the opposite: use simple_spinner_item with setDropDownViewResource() and simple_spinner_dropdown_item with setAdapter()?

推荐答案

我看到了困惑.重新说明一下:为什么还要向适配器的构造函数提供android.R.layout.simple_spinner_item?如何使用?"

I see the confusion. To rephrase: "Why do I even provide android.R.layout.simple_spinner_item to the adapter's constructor? How is it used?"

提供给适配器的构造函数的布局资源用于测量微调器行项目的视图.从AbsSpinner#onMeasure()调用mAdapter.getView(),最终

The layout resource, that is provided to the adapter's constructor is used to measure spinner row item's view. From AbsSpinner#onMeasure() mAdapter.getView() is called, which eventually uses mResource (the resource id, that was passed in to the adapter from constructor) to inflate the view.

另一方面,mDropDownResource

On the other hand, the mDropDownResource is used to inflate each row of Spinner.

请注意,如果您未明确提供mDropDownResource,则默认为适配器构造函数中提供的mResource.

Note, that if you do not provide mDropDownResource explicitly, it defaults to the mResource that was provided in the constructor of adapter.

public ArrayAdapter(@NonNull Context context, @LayoutRes int resource,
        @IdRes int textViewResourceId, @NonNull List<T> objects) {
    ...
    mResource = mDropDownResource = resource;
    ...
}

这篇关于Spinner上的setAdapter()和setDropDownViewResource()之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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