Android Spinner具有“下拉状态”和“关闭状态”的不同布局?

问题描述 投票:58回答:5

我的布局中有一个Android Spinner视图。我希望该微调器在关闭时只显示一个文本项,但是当用户点击它时(即打开微调器对话框)我想为每个项显示更多信息,包括图标和其他描述文本视图。现在,微调器在两种状态下都显示了相同的布局(图标,标题+描述)。

如果我将一个ArrayAdapter附加到微调器,那么我可以访问一个名为“setDropDownViewResource”的东西,但这不一定是我需要的东西,因为我的微调器数据是从一个Cursor中提取的,而不是从任何类型的数组中提取的(我现在已经有了) ,创建了我自己的适配器,扩展了BaseAdapter)。

谁可以帮助我?

android spinner android-spinner
5个回答
101
投票

您必须为Spinner创建一个自定义Adapter类,并为正常关闭视图覆盖两个方法getView(),为下拉列表视图覆盖getDropDownView()。两种方法都必须为单个元素返回View对象。

看看this tutorial它可能会帮助你入门。


13
投票

我也有问题。我没有覆盖课程,而是采用更简单的方法。

但首先,您需要了解适配器构造函数中的资源ID与setDropDownViewResource(...)中的另一个资源ID之间的区别。例如,

SimpleAdapter adapter =
    new SimpleAdapter(ab.getThemedContext(), data, R.layout.actionbar_dropdown, new String[] { "EventID", "Icon" },
        new int[] { R.id.event_id, R.id.icon });

adapter.setDropDownViewResource(R.layout.actionbar_list_item);

R.layout.actionbar_dropdown是旋转器的样式,R.layout.actionbar_list_item是每个列表项。

我在这里使用SimpleAdapter,因为如果我使用ArrayAdapter,xml只能是一个TextView。

R.layout.actionbar_list_item包含一个TextView,其id为event_id,一个ImageView,其id为icon

R.layout.actionbar_dropdown几乎与actionbar_list_item完全相同,但后者的ImageView的可见性设置为GONE。

这样,每个列表项都有一个textview和一个imageview,但是你只能在微调器上看到一个textview。


10
投票

使用由Flo链接的教程的代码,我创建了以下CustomSpinnerAdapter,以显示两个不同的字符串集,一个显示项目,一个不显示。我希望它对某人有帮助。

public class CustomSpinnerAdapter extends ArrayAdapter<String> {

Context mContext;
int mTextViewResourceId;
String[] mObjects;
String[] mShortNameObjects;

public CustomSpinnerAdapter(Context context, int textViewResourceId,
                            String[] objects, String[] shortNameObjects) {
    super(context, textViewResourceId, objects);
    mContext = context;
    mTextViewResourceId = textViewResourceId;
    mObjects = objects;
    mShortNameObjects = shortNameObjects;
}

@Override
public View getDropDownView(int position, View convertView,
                            ViewGroup parent) {
    LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    TextView row = (TextView) inflater.inflate(mTextViewResourceId, parent, false);
    row.setText(mObjects[position]);

    return row;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    return getCustomView(position, convertView, parent);
}

public View getCustomView(int position, View convertView, ViewGroup parent) {
    LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    TextView row = (TextView) inflater.inflate(mTextViewResourceId, parent, false);
    row.setText(mShortNameObjects[position]);

    return row;
}
}

片段内的用法:

CustomSpinnerAdapter mSpinnerAdapter = new CustomSpinnerAdapter(getActivity(), R.layout.spinner_item, getResources().getStringArray(R.array.action_filter), getResources().getStringArray(R.array.action_filter_short_names));

最后,微调项目的布局:

spinner_item.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:textSize="18dip"
    android:gravity="left"
    android:textColor="@color/navdraw_list_item_background_default"
    android:padding="5dip" />

8
投票

仅使用替代布局设置下拉视图资源:

ArrayAdapter<String> genderAdapter = new ArrayAdapter<>(this, R.layout.adapter_spinner_white, Constants.GENDER);
genderAdapter.setDropDownViewResource(R.layout.adapter_spinner_white_dropdown);
view.setAdapter(genderAdapter);

对我来说,它只是一个带有额外填充的布局,因为我的旋转器背景是圆形的可绘制的并且需要这个额外的空间。


1
投票

在获取对spinner的引用后,只需调用setUpSpinner()方法

//这里是setUpSpinner方法

private void setupSpinner() {

    // Create adapter for spinner. The list options are from the String array it will use
    // the spinner will use the default layout
    ArrayAdapter spinnerAdapter = ArrayAdapter.createFromResource(this,
            R.array.array_dropdown_options, android.R.layout.simple_spinner_item);

    // Specify dropdown layout style - simple list view with 1 item per line
    spinnerAdapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);

    // Apply the adapter to the spinner
    spinner.setAdapter(spinnerAdapter);
   // spinner is referenced spinner by finViewById.

    spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            String selection = (String) parent.getItemAtPosition(position);
            if (!TextUtils.isEmpty(selection)) {
                if (selection.equals(getString(R.string.item_a))) {
                    // your code for selected item whose id equals to id to R.string.item_a
                } else if (selection.equals(getString(R.string.item_b))) {
                    // your code
                } else {
                    // your code
                }
            }
        }

        // Because AdapterView is an abstract class, onNothingSelected must be defined
        @Override
        public void onNothingSelected(AdapterView<?> parent) {
            // code for nothing selected in dropdown
        }
    });
}

© www.soinside.com 2019 - 2024. All rights reserved.