java - 嵌套微调器 Android 设计问题 ConstraintLayout
问题描述
我第一次使用微调器,我不明白为什么第二个微调器看起来不完全像第一个,即使它们实际上是相同的(唯一的区别是数据)。这些是嵌套的。
该设计使用“约束布局”实现它
<Spinner
android:id="@+id/spCategorie"
android:layout_width="0dp"
android:layout_height="28dp"
android:layout_marginStart="10dp"
android:layout_marginTop="32dp"
android:layout_marginEnd="32dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toEndOf="@+id/categorieText"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/categorieText"
android:layout_width="73dp"
android:layout_height="28dp"
android:layout_marginStart="16dp"
android:layout_marginTop="32dp"
android:text="Categorie:"
android:textAlignment="viewStart"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Spinner
android:id="@+id/spProduct"
android:layout_width="0dp"
android:layout_height="28dp"
android:layout_marginStart="10dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="32dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/productText"
app:layout_constraintTop_toBottomOf="@+id/spCategorie" />
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, R.layout.support_simple_spinner_dropdown_item, productsCat);
arrayAdapter.setDropDownViewResource(R.layout.support_simple_spinner_dropdown_item);
categorie.setAdapter(arrayAdapter);
categorie.setOnItemSelectedListener(new SpinnersEvents());
product.setOnItemSelectedListener(new SpinnersEvents());
private class SpinnersEvents implements AdapterView.OnItemSelectedListener {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
if (parent.getId() == R.id.spCategorie){
String[] productsName = getProductName(productsCat[position]);
ArrayAdapter<String> arrayAdapterChild = new ArrayAdapter<String>(getBaseContext(),R.layout.support_simple_spinner_dropdown_item, productsName);
arrayAdapterChild.setDropDownViewResource(R.layout.support_simple_spinner_dropdown_item);
product.setAdapter(arrayAdapterChild);
}else{
price.setText(String.valueOf(tempList.get(position).getPrice()));
imgPrd.setImageResource(tempList.get(position).getImage());
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
}
解决方案
解决方案是在代码中,而不是在布局中。
第二个微调器必须是:
ArrayAdapter<String> arrayAdapterChild = new ArrayAdapter<String>(MainActivity.this,R.layout.support_simple_spinner_dropdown_item, productsName);
反而:
ArrayAdapter<String> arrayAdapterChild = new ArrayAdapter<String>(getBaseContext(),R.layout.support_simple_spinner_dropdown_item, productsName);
在这种情况下,调用 getBaseContext() 方法会产生该特定问题。
对困惑感到抱歉。
推荐阅读
- python - 如何使用 OpenCV 用一些 Python 代码拼接这两个图像?
- python - 字符串与数据库中数据的比较
- android - 如何在按钮单击以打开另一个活动时输入密码?
- laravel - 如何在没有 shell 命令的情况下在实时服务器中安装软件包?
- angular - When select emoji message gets instantly sent instead of waiting for clicking send button
- java - How can I extract the formal java method arguments in python and add them to a list of strings?
- jquery - 使用空 alt="" 更改图像的 alt 属性
- java - 为什么
并不是 ? - python - python版本之间的种子随机数是否不一致?
- chart.js - 删除 Chart.js 网格线重叠