我正在尝试在“自定义”对话框中使用android Spinner,但是当我点击Spinner时,带有项目的列表将打开,您可以点击它们并关闭视图,但是没有选中任何项目。当我在正常的Fragment中使用完全相同的代码时,效果很好。
由于我发现任何地方都无济于事,因此在Dialog中使用微调框时,我需要知道些什么吗?
CustomDialog类:
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 该类的目的是在对话框内和对话框外使用完全相同的代码处理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();
}
}
普通片段内的微调框
spinner = view.findViewById(R.id.employeeSpinner);
EmployeeSpinnerHandler spinnerHandler = new EmployeeSpinnerHandler(getContext(), spinner, objects);
注意::在某些情况下,该日期是从REST-Api中获取的,但仍然只能在常规Fragment中使用。
现在的问题是,如果我在普通片段中的Spinner上使用EmployeeSpinnerHandler可以工作,但是如果我在选择对话框中使用Spinner,则可以单击Spinner并显示项目,但是它们可选arent(请参见图片)。
图片:
答案 0 :(得分:1)
适配器允许单焦点视图使android:focusable="false"
成为您的TextView的所有其他项android:focusable="true"
,其中包含将起作用的标题。
答案 1 :(得分:0)
如果以下代码返回一个项目,则似乎缺少视觉更新: FancyToast.makeText(getContext(),“ selected:” + employee.get(position).getName(),Toast.LENGTH_LONG,FancyToast.SUCCESS,true).show();
在这种情况下,我建议添加 spinner.setSelection(position)