旋转器和旋转器项目的不同视图?

发布于 2024-12-06 03:33:57 字数 3146 浏览 1 评论 0原文

有什么方法可以为关闭的微调器视图和微调器项目视图设置不同的视图吗?

我假设 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

冰火雁神 2024-12-13 03:33:57

如果您实现 SpinnerAdapter 会有帮助吗?
以下代码对我有用:

private class CustomSpinnerAdapter extends BaseAdapter implements SpinnerAdapter {
        String[] values;
        Context context;

        public NumberSpinnerAdapter(String[] values) {
            this.values = values;
            this.context = context;
        }

        @Override
        public int getCount() {
            return values.length;
        }

        @Override
        public Object getItem(int position) {
            return values[position];
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            TextView textView = (TextView) View.inflate(context, android.R.layout.simple_spinner_item, null);
            textView.setText(values[position]);
            return textView;
        }

        @Override
        public View getDropDownView(int position, View convertView, ViewGroup parent) {
            TextView textView = (TextView) View.inflate(context, android.R.layout.simple_spinner_dropdown_item, null);
            textView.setText(values[position]);
            return textView;
        }
    }

If you implement SpinnerAdapter does that help?
The following code works for me:

private class CustomSpinnerAdapter extends BaseAdapter implements SpinnerAdapter {
        String[] values;
        Context context;

        public NumberSpinnerAdapter(String[] values) {
            this.values = values;
            this.context = context;
        }

        @Override
        public int getCount() {
            return values.length;
        }

        @Override
        public Object getItem(int position) {
            return values[position];
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            TextView textView = (TextView) View.inflate(context, android.R.layout.simple_spinner_item, null);
            textView.setText(values[position]);
            return textView;
        }

        @Override
        public View getDropDownView(int position, View convertView, ViewGroup parent) {
            TextView textView = (TextView) View.inflate(context, android.R.layout.simple_spinner_dropdown_item, null);
            textView.setText(values[position]);
            return textView;
        }
    }
与风相奔跑 2024-12-13 03:33:57

虽然这是一个老问题,但我偶然发现试图解决同样的问题。
我使用 setDropDownViewResource :

Spinner mySpinner = (Spinner) mView.findViewById(R.id.mySpinner);
ArrayAdapter<String> myAdapter = new ArrayAdapter<String>(...);

myAdapter.setDropDownViewResource(R.layout.my_simple_spinner_dropdown_item);
mySpinner.setAdapter(adapter);

Although this is an old question, I stumbled upon trying to solve the same problem.
I use setDropDownViewResource:

Spinner mySpinner = (Spinner) mView.findViewById(R.id.mySpinner);
ArrayAdapter<String> myAdapter = new ArrayAdapter<String>(...);

myAdapter.setDropDownViewResource(R.layout.my_simple_spinner_dropdown_item);
mySpinner.setAdapter(adapter);
权谋诡计 2024-12-13 03:33:57

实际上,为展开(打开)和折叠(关闭)Spinner 获取不同的 View 只需覆盖 getViewgetDropdownView< /code> 你的适配器的方法。

getView(intposition,ViewconvertView,ViewGroupparent) 将生成所有用于显示所选项目的View,当 < code>Spinner 已折叠。

当您点击 Spinner 将其展开并显示下拉菜单时,getDropDownView(intposition, View ConvertView, ViewGroupparent) 会被调用 您的物品列表。您可以从此方法调用 getView(如果处于展开和折叠状态的项目相似),或者为下拉列表中的项目生成不同的 View

无论如何,如果您覆盖两个这些方法,那就太好了(即使您的getDropdownView只是使用相同的参数调用getView)。并且不要忘记使用您的 convertView 参数 - 它在这里是为了优化(重用现有视图而不是每次都膨胀/创建它们):

public View getView(int position, View convertView, ViewGroup parent) {
     ...
     if(convertView == null){
          convertView = View.inflate(getContext(),
                R.layout.dropdown_category_color, null);
     }
     //...do your stuff with your View

     return convertView;
}

Actually, getting different Views for expanded (opened) and collapsed (closed) Spinner is as simple as overriding getView and getDropdownView methods of your adapter.

getView(int position, View convertView, ViewGroup parent) will produce all the Views that will be used to display a selected item when the Spinner is collapsed.

getDropDownView(int position, View convertView, ViewGroup parent) gets called when you click on a Spinner to expand it and display a dropdown list of your items. You can either call getView from this method (if your items in expanded and collapsed states are similar), or produce a different View for the item in your dropdown list.

It would be nice if you override both these methods anyway (even if your getDropdownView just calls getView with the same arguments). And do not forget to use your convertView parameter - it is here for a purpose of optimisation (to reuse existing Views instead of inflating/creating them every time):

public View getView(int position, View convertView, ViewGroup parent) {
     ...
     if(convertView == null){
          convertView = View.inflate(getContext(),
                R.layout.dropdown_category_color, null);
     }
     //...do your stuff with your View

     return convertView;
}
月寒剑心 2024-12-13 03:33:57
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));
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));
别念他 2024-12-13 03:33:57
//1---------------
package testing;

public class SpinnerItemAdapterData {

    public int id ;
    public String name;

    public SpinnerItemAdapterData(int id, String name) {
        this.id = id;
        this.name = name;
    }

    public int getId(){
        return this.id;
    }

    public String getName(){
        return this.name;
    }

}
//-------------------------
//1---------------
package testing;

public class SpinnerItemAdapterData {

    public int id ;
    public String name;

    public SpinnerItemAdapterData(int id, String name) {
        this.id = id;
        this.name = name;
    }

    public int getId(){
        return this.id;
    }

    public String getName(){
        return this.name;
    }

}
//-------------------------
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文