本文介绍了对话框模式下微调器的覆盖下拉列表样式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当对话框模式下的Spinner ID(android:spinnerMode ="dialog")时,我需要覆盖下拉式Spinner项目列表.我需要用它来定义自己的列表分隔符.

I need to override List of dropdown spinner items when Spinner id in dialog mode (android:spinnerMode="dialog"). I need this to define my own list divider.

我在应用程序主题中找到了dropDownListViewStyle项目,其中包含分隔符项目.它有效,但仅适用于android:spinnerMode ="dropdown".

I have found dropDownListViewStyle item in application Theme, which contains divider item. And it works, but only for android:spinnerMode="dropdown".

在对话"模式下,我可以具有相同的效果吗?

Can I have the same effect for "dialog" mode?

推荐答案


尝试一下:


Try this:

<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

Dropdown layout 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

Main View layout: 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:

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) {
                    }
                });

单击按钮以显示对话框:

Button click to show dialog:

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

这篇关于对话框模式下微调器的覆盖下拉列表样式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 21:29