首页 > 解决方案 > Spinner 下拉列表未在 DialogFragment 上打开

问题描述

我正在尝试创建一个扩展的自定义对话框DialogFragment,当我创建一个简单的对话框时,微调器下拉菜单运行良好,并在我尝试打开它时做出反应。在DialogFragment微调器上很好地显示第一个选项,但是当我点击他时,它不会打开下拉菜单。

我决定基于对话框移动,DialogFragment因为当屏幕旋转时对话框大小不适合横向模式。

在我的应用程序中,我显示了来自 custom 的对话框ArrayAdapter,所以我没有找到在屏幕旋转时更改对话框大小的方法,所以我认为这DialogFragment是解决我的问题的合理方法。

微调器下拉菜单在简单对话框上运行良好,移动到DialogFragment.

DialogFragment 类的代码

    @Override
    public void onResume() {
        super.onResume();
        ViewGroup.LayoutParams params = getDialog().getWindow().getAttributes();
        params.width = ViewGroup.LayoutParams.MATCH_PARENT;
        params.height = ViewGroup.LayoutParams.MATCH_PARENT;
        getDialog().getWindow().setAttributes((android.view.WindowManager.LayoutParams) params);
    }

'treatmentTypes' 是 Spinner 私有成员。

'GetTypesOfTreatments' 是具有从服务器下载数据和更新微调器的功能的类。该功能运行良好,我可以在日志上看到微调器得到了它需要的所有东西。

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        Log.d(TAG, "onCreateView: In Dialog");
        String fullDate, fullTime;
        user.getUserDetails(view);
        Button btnCancel, btnOK;

        ViewGroup.LayoutParams params = getDialog().getWindow().getAttributes();
        params.width = ViewGroup.LayoutParams.MATCH_PARENT;

        int displayWidth = params.width;

        String min = currentDate.getMin() < 10 ? currentDate.getMin() + "0" : Integer.toString(currentDate.getMin());
        fullDate = currentDate.getDay() + "/" + currentDate.getMonth() + "/" + currentDate.getYear();
        fullTime = currentDate.getHour() + ":" + min;

    //...

        dialog_date = (TextView) view.findViewById(R.id.dialog_new_apt_date);
        dialog_time = (TextView) view.findViewById(R.id.dialog_new_apt_time);
        dialog_date.setText(fullDate);
        dialog_time.setText(fullTime);
        treatmentTypes = (Spinner) view.findViewById(R.id.dialog_new_apt_type_list);
        treatmentTypes.setDropDownWidth((int) (displayWidth * 0.5f));
        btnCancel = (Button) view.findViewById(R.id.dialog_new_apt_cancel);
        btnOK = (Button) view.findViewById(R.id.dialog_new_apt_ok);
        username = (TextView) view.findViewById(R.id.dialog_new_apt_edit_name);
        phoneNumber = (TextView) view.findViewById(R.id.dialog_new_apt_edit_phone);

        GetTypesOfTreatments getTypesOfTreatments = new GetTypesOfTreatments();
        getTypesOfTreatments.getListOfTypes(this.getContext(), treatmentTypes);

        btnCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });

    ....


    }

GetTypesOfTreatments 更新微调器部分:(微调器变量是我从对话框中传递的)

        SpinnerAdapter adapter = new SpinnerAdapter(context,R.layout.adapter_spinner_treatment_types,typeList);
        spinner.setAdapter(adapter);

微调适配器:

    @Override
    public View getDropDownView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        return createItemView(position,convertView,parent);
    }

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

    private View createItemView(int position, View convertView, ViewGroup parent){
        final View view = layoutInflater.inflate(resources, parent, false);

        view.setBackgroundColor(view.getResources().getColor(R.color.colorBackground,null));
        TextView description = (TextView) view.findViewById(R.id.spinner_adapter_description),
                price = (TextView) view.findViewById(R.id.spinner_adapter_price);

        description.setText(list.get(position).getDescription());
        if(list.get(position).getPrice() != 0)
            price.setText(Integer.toString(list.get(position).getPrice()));

        return view;
    }

对话框布局代码:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:theme="@android:style/Theme.Material.Dialog"
    android:layout_height="match_parent">

    <android.support.constraint.Guideline
        ... />

    <TextView
        ... />

    <EditText
        ... />

    <TextView
        ... />

    <Button
        ... />

    <android.support.constraint.Guideline
        ... />

    <Spinner
        android:id="@+id/dialog_new_apt_type_list"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginBottom="8dp"
        android:dropDownWidth="150dp"
        android:gravity="center|start"
        android:spinnerMode="dropdown"
        android:textAlignment="gravity"
        android:textDirection="locale"
        app:layout_constraintBottom_toTopOf="@+id/guideline7"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="@+id/guideline19"
        app:layout_constraintTop_toTopOf="@+id/guideline6" />

    <android.support.constraint.Guideline
        ... />

    <TextView
        ... />

    <android.support.constraint.Guideline
        ... />

    <TextView
        ... />

    <android.support.constraint.Guideline
        ... />

    <TextView
        ... />

    <EditText
        ... />

    <Button
        ... />

    <android.support.constraint.Guideline
        ... />

    <TextView
        ... />

</android.support.constraint.ConstraintLayout>

谢谢大家的帮助。

标签: javaandroidandroid-spinnerandroid-dialogfragment

解决方案


推荐阅读