在对话框模式下覆盖Spinner的下拉列表样式

时间:2013-02-18 00:47:27

标签: android android-widget

我需要在对话框模式(android:spinnerMode =“dialog”)中重写Spinner id时的下拉微调器项列表。我需要这个来定义我自己的列表分隔符。

我在应用程序主题中找到了dropDownListViewStyle项,其中包含分隔项。它的工作原理,但仅适用于android:spinnerMode =“dropdown”。

我可以对“对话”模式产生相同的效果吗?

1 个答案:

答案 0 :(得分:0)

试试这个:

<Spinner
    android:id="@+id/spinnerAddToList"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:layout_marginTop="10dp"
    android:theme="@style/ThemeOverlay.AppCompat.Light"
    android:spinnerMode="dialog"/>

下拉列表spinner_item_line_drop.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:paddingLeft="3dp"
    android:paddingRight="3dp">

    <TextView
        android:id="@+id/text_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_vertical"
        android:textSize="18sp" />

    <LinearLayout
        android:id="@+id/separator"
        android:layout_height="1dp"
        android:layout_width="match_parent"
        android:background="@color/colorPrimary"
        android:orientation="vertical"
        android:layout_marginTop="10dp"
        android:layout_marginBottom="10dp"/>

</LinearLayout>

主视图布局:spinner_item_line_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:paddingBottom="10dp"
    android:paddingLeft="3dp"
    android:paddingRight="3dp"
    android:paddingTop="10dp">

    <TextView
        android:id="@+id/text_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_vertical"
        android:textSize="18sp" />

</LinearLayout>

SpinnerAdapter:

public class SpinnerLineAdapter extends BaseAdapter {

    private List<MyListEntity> values;

    public SpinnerLineAdapter(List<MyListEntity> values)
    {
        this.values = values;
    }

    public void setValue(List<MyListEntity> list)
    {
        this.values = list;
    }

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

    @Override
    public MyListEntity getItem(int position) {
        return values.get(position);
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View itemView = convertView;
        ViewHolder viewHolder;

        Context context = parent.getContext();

        if (convertView == null) {
            itemView = LayoutInflater.from(context).inflate(R.layout.spinner_item_line_main, parent, false);

            viewHolder = new ViewHolder();
            viewHolder.textView = itemView.findViewById(R.id.text_view);

            itemView.setTag(viewHolder);

        } else {
            viewHolder = (ViewHolder) itemView.getTag();
        }

        viewHolder.textView.setText(values.get(position).getName());

        return itemView;
    }

    public View getDropDownView(int position, View convertView,ViewGroup parent) {
        View itemView = convertView;
        ViewHolder viewHolder;

        Context context = parent.getContext();

        if (convertView == null) {
            itemView = LayoutInflater.from(context).inflate(R.layout.spinner_item_line_drop, parent, false);
            viewHolder = new ViewHolder();
            viewHolder.textView = itemView.findViewById(R.id.text_view);
            itemView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) itemView.getTag();
        }

        viewHolder.textView.setText(values.get(position).getName());

        return itemView;
    }

    private static class ViewHolder {
        TextView textView;
    }
}

的活动:

SpinnerLineAdapter spinnerAddToListAdapter = new SpinnerLineAdapter(list);
                spinnerAddToList.setAdapter(spinnerAddToListAdapter);
                spinnerAddToList.setSelection(0, false);
                spinnerAddToList.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                    @Override
                    public void onItemSelected(AdapterView<?> adapterView, View view,
                                               int position, long id) {

                        MyListEntity list = spinnerAddToListAdapter.getItem(position);
                        viewModel.addList(list.id);
                        viewModel.update(myEntity);
                    }

                    @Override
                    public void onNothingSelected(AdapterView<?> adapter) {
                    }
                });

按钮单击以显示对话框:

    mButtonAdd.setOnClickListener(view -> {
        spinnerAddToList.performClick();
    });