单击复选框后,我需要添加新视图。我想要的是显示复选框的更改状态,然后才显示新视图,但是现在当我单击复选框时,ui 会停止一秒钟或几秒钟,然后同时显示新视图和复选框显示为选中状态。
我的代码很简单
{
checkbox = (CheckBox) findViewById(R.id.select_param_value_checkbox);
if (checkbox != null) {
checkbox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
onCheckboxChanged(isChecked);
}
});
}
}
protected void onCheckboxChanged(boolean isChecked) {
//this listener is adding in constructor and creates in view where i want to add child
on_change_listener.onFilterParamValueChanged(this);
}
该侦听器的代码是:
public void onFilterParamValueChanged(MyClass obj) {
addView(new MyView(getContext(), obj.getTitle()));
}
MyView 的代码:
class MyView extends LinearLayout{
private TextView title;
public MyView(Context context, String text) {
super(context);
ViewGroup.inflate(context, R.layout.my_view, this);
title = (TextView) findViewById(R.id.selected_filter_param_value_title);
if (title != null) {
title.setText(filter_value_data.toString());
}
}
}
感谢@Diljeet,对我有用的UPD 解决方案在下面的评论中。我把 addView 在 Runnable
public void onFilterParamValueChanged(MyClass obj) {
new Handler().post(new Runnable(){
public void run() {
addView(new MyView(getContext(), obj.getTitle()));
}
});
}