简体   繁体   English

旋转后微调值消失

[英]Spinner values dissapear after rotation

I*ve got a simple "material spinner" implementation:我*有一个简单的“材料微调器”实现:

<com.google.android.material.textfield.TextInputLayout
    style="@style/Widget.MaterialComponents.TextInputLayout.FilledBox.ExposedDropdownMenu"
    android:layout_width="match_parent" 
    android:layout_height="wrap_content">

    <AutoCompleteTextView
        android:id="@+id/filled_exposed_dropdown"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="none"/>

</com.google.android.material.textfield.TextInputLayout>

And my fragment onCreate method which populates the list我的片段 onCreate 方法填充列表

AutoCompleteTextView editTextFilledExposedDropdown;
ArrayAdapter<String> adapter = null;

@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
    String[] type = new String[] {"Bed-sitter", "Single", "1- Bedroom", "2- Bedroom","3- Bedroom"};
    adapter =new ArrayAdapter<>( getContext(),R.layout.dropdown_menu_popup_item, type);
    editTextFilledExposedDropdown = view.findViewById(R.id.filled_exposed_dropdown);
    editTextFilledExposedDropdown.setAdapter(adapter);
}

The code works perfectly fine as long I don't choose an item and rotate the phone.只要我不选择项目并旋转手机,代码就可以正常工作。 Once I rotate the phone the onCreate method is called again but for some reason the only item which is available is the item I choose before.旋转手机后,再次调用onCreate方法,但由于某种原因,唯一可用的项目是我之前选择的项目。 The menu does not show all the other items.菜单不显示所有其他项目。

What might the cause be?原因可能是什么? I'm repopulating the arrayadapter on purpose just to make sure I work with the same objects when I rotate the phone.我故意重新填充 arrayadapter 只是为了确保我在旋转手机时使用相同的对象。

Use a custom view.使用自定义视图。 This is a bug from the materialdesign class which has not been fixed yet unfourtantly:这是 materialdesign 类中的一个错误,但尚未修复:

public class ExposedDropdownMenu extends MaterialAutoCompleteTextView {

    public ExposedDropdownMenu(@NonNull @NotNull Context context) {
        super(context);
    }
    public ExposedDropdownMenu(@NonNull @NotNull Context context, @Nullable @org.jetbrains.annotations.Nullable AttributeSet attributeSet) {
        super(context, attributeSet);
    }


    public ExposedDropdownMenu(@NonNull @NotNull Context context, @Nullable @org.jetbrains.annotations.Nullable AttributeSet attributeSet, int defStyleAttr) {
        super(context, attributeSet, defStyleAttr);
    }

    @Override
    public boolean getFreezesText() {
        return false;
    }
}

Set the adapter in onResume() method, it will work fine.在 onResume() 方法中设置适配器,它会正常工作。 I'm answering from mobile phone , so can't show you code snippet and I don't have enough reputation to comment so posting as answer.我正在通过手机回答,因此无法向您展示代码片段,而且我没有足够的声誉来发表评论,因此将其作为答案发布。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM