我有一个Activity
与34 Switch
,加载活动很好。
但是,当我尝试用Switch
替换CheckBox
时,活动需要一整秒才能加载。
这是CheckBox
的一般问题还是我的代码不合适?
我的活动代码,简化(仅显示1个CheckBox
)
public class Myactivity extends AppCompatActivity {
CheckBox MyCheckbox1;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_myactivity);
MyCheckbox1 = findViewById(R.id.mycheck1);
Mycheckbox1.OnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
onCheckedChanged(CompoundButton view, boolean isChecked) {
if(((CompoundButton) view).isChecked()){
//do something
}else{
//do something else
}
}
}
}
}
我的复选框XML代码:
<CheckBox
android:id="@+id/mycheck1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/Option1"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
我认为在XML中创建52个开关或Checkbox会加重UI渲染。您应该使用RecyclerView重用视图,这样渲染时间就会非常短。