Difference between setAdapter() and setDropDownViewResource() on Spinner

心已入冬 提交于 2019-12-10 22:22:43

问题


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.

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)?

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()?


回答1:


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?"

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.

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

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;
    ...
}



回答2:


For the simple_spinner_item in setAdapter(), it used to show each row of item in the spinner. For the simple_spinner_dropdown_item, it used to show all rows of items when the user click the dropdown icon in the spinner. If you want to see the difference between both of them, you can try the steps below:

  • create a customized resource layout for both simple_spinner_item and simple_spinner_dropdown_item

  • create a TextView in both layouts

  • change the font colour of the TextView

From that, you will see the difference.. thanks



来源:https://stackoverflow.com/questions/45128935/difference-between-setadapter-and-setdropdownviewresource-on-spinner

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!