Spinner 在自定义对话框中不工作 - 为什么?

Spinner not working in custom Dialog - why?

我正在尝试在自定义对话框中使用 android 微调器,但是当我点击微调器时,带有项目的列表打开,您可以点击它们并关闭视图,但是 none 的项目被选中。当我在普通片段中使用完全相同的代码时,它工作正常。

由于我在任何地方都找不到任何有用的信息,在对话框中使用微调器时是否需要了解一些信息?

自定义对话框Class:

public SelectionDialog(Activity context, ArrayAdapter<T> arrayAdapter, List<T> objects) {
    super(context);
    this.activity = context;
    this.arrayAdapter = arrayAdapter;
    this.objects = objects;
}



@SuppressLint("ResourceAsColor")
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    getWindow().requestFeature(Window.FEATURE_NO_TITLE);
    getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));

    View view = LayoutInflater.from(getContext()).inflate(R.layout.dialog_selection,null);

    setContentView(view);

    BodyMenuCard parentCard = view.findViewById(R.id.parentCard);
    parentCard.setHeader(header);

    TextView textView = parentCard.getCardBody().findViewById(R.id.bodyText);
    textView.setText(description);

    yesBtn = parentCard.getCardBody().findViewById(R.id.btn_yes);

    //spinner = parentCard.getCardBody().findViewById(R.id.employeeSpinner);
    spinner = view.findViewById(R.id.employeeSpinner);
    spinnerHandler = new EmployeeSpinnerHandler(getContext(), spinner, (List<Employee>) 
    toggleYesBtn(false);


    if (iconRessoruce != -1) parentCard.setIcon(iconRessoruce);
    if (backgroundColor != -1) parentCard.setCircleColor(backgroundColor);

    if (successfullCallback != null) yesBtn.setOnClickListener((v -> successfullCallback.onClick(this)));
    if (abbortCalllback != null) {
        parentCard.getCardBody().findViewById(R.id.btn_no).setOnClickListener((v -> abbortCalllback.onClick(this)));
    } else {
        parentCard.getCardBody().findViewById(R.id.btn_no).setOnClickListener(v -> this.dismiss());
    }
}

EmployeeSpinnerAdapter

public class EmployeeSpinnerAdapter extends ArrayAdapter<Employee> {

private Map<String, ColorStateList> employeeColors;

private List<Employee> employees;

private RandomMaterialColor randomColor;

public EmployeeSpinnerAdapter(Context context, List<Employee> employees) {
    super(context, R.layout.employee_spinner, employees);
    setDropDownViewResource(R.layout.employee_spinner);
    this.employees = employees;
    this.employeeColors = new HashMap<>();
}

private class ViewHolder {

    private GradientDrawable background;
    private TextView header;
    private TablePart idField;

    private ViewHolder(View view) {
        if (randomColor == null) randomColor = new RandomMaterialColor(view.getResources(), view.getContext());
        ImageView iv = view.findViewById(R.id.icon_view);
        background = (GradientDrawable) iv.getBackground().mutate();
        header = view.findViewById(R.id.header);
        idField = view.findViewById(R.id.description);
    }

    private void populate(Employee employee) {
        header.setText(employee.getName());
        idField.update(employee.getId());
        ColorStateList color = employeeColors.getOrDefault(employee.getId(), ColorStateList.valueOf(randomColor.getMatColor("A700")));
        background.setColor(color);
    }

}

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

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

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

private View obtainView(int position, View convertView, ViewGroup parent) {
    if (convertView == null) {
        LayoutInflater mInflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        convertView = mInflater.inflate(R.layout.employee_spinner, parent, false);
        convertView.setTag(new ViewHolder(convertView));
    }
    ViewHolder vh = (ViewHolder) convertView.getTag();
    vh.populate(employees.get(position));
    return convertView;
}
}

EmployeeSpinnerHandler class 的目的是使用完全相同的代码处理对话框内外的 Spinner,以找出错误。

public class EmployeeSpinnerHandler {

private Context context;

private Spinner spinner;

private EmployeeSpinnerAdapter adapter;

private List<Employee> employees;

public EmployeeSpinnerHandler(Context context, Spinner spinner, List<Employee> employees) {
    this.context = context;
    this.spinner = spinner;
    this.employees = employees;

    setUp();
}

private void setUp() {

    adapter = new EmployeeSpinnerAdapter(getContext(), employees);
    spinner.setAdapter(adapter);

    spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            FancyToast.makeText(getContext(), "selected: " + employees.get(position).getName(), Toast.LENGTH_LONG, FancyToast.SUCCESS, true).show();
            //toggleYesBtn(true);
        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {
            //toggleYesBtn(false);
            FancyToast.makeText(getContext(), "selected: none", Toast.LENGTH_LONG, FancyToast.SUCCESS, true).show();
        }
    });

}

public void updateObjects(List<Employee> employees) {
    this.employees.clear();
    this.employees.addAll(employees);
    adapter.notifyDataSetChanged();
}
}

普通Fragment中的Spinner

spinner = view.findViewById(R.id.employeeSpinner);
    EmployeeSpinnerHandler spinnerHandler = new EmployeeSpinnerHandler(getContext(), spinner, objects);

注意:在 boath 情况下,日期是从 REST 中获取的-Api,但它仍然只适用于正常的 Fragment。

现在的问题是,如果我在位于普通 Fragment 中的微调器上使用 EmployeeSpinnerHandler,它可以工作,但如果我在选择对话框中使用微调器,您可以单击微调器并显示项目,但它们不可选择(见图片)。

图片:

Custom Dialog with Spinner

Popup from Spinner in custom Dialog, were you can see the data is present, but if clicked it only closes the popup view

Spinner Working inside the Normal Fragment

如果下面的代码 return 一个项目,它似乎缺少视觉更新: FancyToast.makeText(getContext(), "selected: " + employees.get(position).getName(), Toast.LENGTH_LONG, FancyToast.SUCCESS, true).show();

在这种情况下,我建议添加 spinner.setSelection(位置)

适配器允许单焦点视图android:focusable="false" 用于所有其他项目android:focusable="true" 用于包含标题的 TextView。