如何从 android 中的 getDropDownView 方法获取微调器 id 或标签

发布于 2024-11-26 02:58:06 字数 927 浏览 1 评论 0原文

我有几个微调器,我为它们创建了自定义 ArrayAdapter,这样我就可以更改下拉菜单的外观。我想根据下拉列表所属的微调器来操纵视图。我以为我可以做类似parent.getTag() 的事情,但它返回null。

自定义数组适配器看起来像:

class BackgroundColorAdapter extends ArrayAdapter<String> {
    BackgroundColorAdapter() {
        super(SettingsActivity.this, R.layout.settings_spinner_item, R.id.item_text, textColors);
    }

    public View getDropDownView (int position, View convertView, ViewGroup parent){
        View row=super.getView(position, convertView, parent);
        if(parent.getTag().equals("background"){
            //Do custom stuff here
        }
        return(row);
    }
}

我正在设置标签:

settingsSpinner.setTag("bg_color_spinner");
settingsSpinner.setAdapter(new BackgroundColorAdapter());

我想我对视图层次结构的工作原理感到困惑,但旋转器下拉列表的父级将是旋转器似乎是合乎逻辑的。有人知道我如何在 getDropDownView 中找到下拉列表属于哪个微调器吗?

编辑:将 settingsSpinner 设为单个微调器而不是一组微调器,以使其不那么混乱

I have several spinners that I have created a custom ArrayAdapter for so I can change the drop down menu look. I want to manipulate the view depending on what spinner the dropdown belongs to. I thought I would be able to do something like parent.getTag() but it is returning null.

The custom array adapter looks like:

class BackgroundColorAdapter extends ArrayAdapter<String> {
    BackgroundColorAdapter() {
        super(SettingsActivity.this, R.layout.settings_spinner_item, R.id.item_text, textColors);
    }

    public View getDropDownView (int position, View convertView, ViewGroup parent){
        View row=super.getView(position, convertView, parent);
        if(parent.getTag().equals("background"){
            //Do custom stuff here
        }
        return(row);
    }
}

and I'm setting the tag:

settingsSpinner.setTag("bg_color_spinner");
settingsSpinner.setAdapter(new BackgroundColorAdapter());

I think I'm confused how the view hierarchy works but it seems logical that the parent of the spinner drop down would be the spinner. Anyone know how I can find out what spinner the drop down belongs to in getDropDownView?

edit: made the settingsSpinner a single spinner instead of an array of spinners to make it less confusing

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

揽月 2024-12-03 02:58:07

最终让它工作起来,这里是示例代码,用于更改下拉列表中每个项目的文本字体。

class TextSizeAdapter extends ArrayAdapter<String> {

        TextSizeAdapter() {
            super(SettingsActivity.this, R.layout.settings_spinner_item, R.id.item_text, textSizes);
        }

        public View getDropDownView (int position, View convertView, ViewGroup parent){

            View row=super.getView(position, convertView, parent);
            TextView text = (TextView)row.findViewById(R.id.item_text);
            text.setTextSize(TypedValue.COMPLEX_UNIT_PX,appState.FONTSIZES[position]);
            RadioButton radio = (RadioButton)row.findViewById(R.id.item_radio);
            if(settingsSpinners[2].getSelectedItemPosition() == position){
                radio.setChecked(true);
            }else{
                radio.setChecked(false);
            }

            return(row);
        }
    }

Eventually got this to work, here is the code for example that changes the text font for each item in the drop down.

class TextSizeAdapter extends ArrayAdapter<String> {

        TextSizeAdapter() {
            super(SettingsActivity.this, R.layout.settings_spinner_item, R.id.item_text, textSizes);
        }

        public View getDropDownView (int position, View convertView, ViewGroup parent){

            View row=super.getView(position, convertView, parent);
            TextView text = (TextView)row.findViewById(R.id.item_text);
            text.setTextSize(TypedValue.COMPLEX_UNIT_PX,appState.FONTSIZES[position]);
            RadioButton radio = (RadioButton)row.findViewById(R.id.item_radio);
            if(settingsSpinners[2].getSelectedItemPosition() == position){
                radio.setChecked(true);
            }else{
                radio.setChecked(false);
            }

            return(row);
        }
    }
木森分化 2024-12-03 02:58:07

我不熟悉 getDropDownView(),也不知道你为什么使用它。 getDropDownView() 的文档说明了有关父级的以下内容:

parent 该视图最终将附加到的父级

这听起来不像您正在寻找的“父级”...
由于 getView() 调用中的“父级”确实是一个 Spinner,因此您可以使用它来存储父级的实例变量,如下所示:

public Spinner mParent = null;

public View getView (int position, View convertView, ViewGroup parent)
{
    this.mParent = parent;
    return super.getView(position, convertView, parent);
}

public View getDropDownView(int position, View convertView, ViewGroup parent)
{    // Your code here -> but use 'mParent'
}

我没有尝试过,但也许这是获得您需要的内容的解决方法。如果您找到解决方案,请告诉我。

I'm unfamiliar with getDropDownView(), and don't know why you use it. Documentation for getDropDownView() states the following about the parent:

parent the parent that this view will eventually be attached to

This doesn't sound like the 'parent' you are looking for...
Since the 'parent' in the getView() call is indeed a Spinner, you could use that to store an instance variable of the parent like below:

public Spinner mParent = null;

public View getView (int position, View convertView, ViewGroup parent)
{
    this.mParent = parent;
    return super.getView(position, convertView, parent);
}

public View getDropDownView(int position, View convertView, ViewGroup parent)
{    // Your code here -> but use 'mParent'
}

I haven't tried, but maybe it's a workaround to get what you need. Please let me know if you found the solution.

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