Android spinnerAdapter getView()被调用了600多次
我有一个微调器,我为它创建了一个自定义适配器 现在我注意到当微调器加载UI中的值时,UI中出现了冻结。 当我调试它时,我发现在activity start上,它加载了14项,这很好,但加载了大约600次 这是我的密码Android spinnerAdapter getView()被调用了600多次,android,performance,android-layout,android-spinner,Android,Performance,Android Layout,Android Spinner,我有一个微调器,我为它创建了一个自定义适配器 现在我注意到当微调器加载UI中的值时,UI中出现了冻结。 当我调试它时,我发现在activity start上,它加载了14项,这很好,但加载了大约600次 这是我的密码 public class CountriesAdapter extends ArrayAdapter<CountriesDTO> { public CountriesAdapter(Context context,List<Countri
public class CountriesAdapter extends ArrayAdapter<CountriesDTO> {
public CountriesAdapter(Context context,List<CountriesDTO> countriesDTO) {
super(context, R.layout.country_component, countriesDTO);
}
@Override
public View getDropDownView(int position, View convertView,ViewGroup parent) {
return initView(position, convertView, parent);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
return initView(position, convertView, parent);
}
private View initView(int position, View convertView, ViewGroup parent){
Log.d("ADP_POSITION", String.valueOf(position)); //this line gets printed on logs around 2000 times on activity start
if(convertView == null){
convertView= LayoutInflater.from(getContext()).inflate(R.layout.country_component, parent, false);
}
TextView label = convertView.findViewById(R.id.tvCountry);
ImageView ivFlag = convertView.findViewById(R.id.ivCountryFlag);
CountriesDTO countriesDTO= getItem(position);
label.setText(countriesDTO.getCountryName());
Glide.with(GlobalApplication.Companion.getAppContext())
.load(countriesDTO.getCompleteFlagURL())
.into(ivFlag);
return convertView;
}
}
适配器布局
<Spinner
android:id="@+id/spinner"
android:layout_width="0dp"
android:layout_height="50dp"
android:background="@null"
android:overlapAnchor="false"
app:layout_constraintEnd_toEndOf="parent" />
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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:layout_height="40dp"
android:padding="8dp"
android:background="@color/Black_Color"
>
<TextView
android:id="@+id/tvCountry"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:textStyle="normal" />
<ImageView
android:id="@+id/ivCountryFlag"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_marginStart="12dp"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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:layout_height="40dp"
android:padding="8dp"
android:background="@color/Black_Color"
>
<TextView
android:id="@+id/tvCountry"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:textStyle="normal" />
<ImageView
android:id="@+id/ivCountryFlag"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_marginStart="12dp"
/>
</androidx.constraintlayout.widget.ConstraintLayout>