为什么内部Listener(addAddress和cancelAddress)不起作用? Everythings OK但是在使viewStub无效后点击Listeners。这些按钮已添加到该viewStub中。
private View addressStubInflated;
//...
final View fview = inflater.inflate(R.layout.fragment_address, container, false);
//...
final ViewStub addressStub = (ViewStub) fview.findViewById(R.id.address_viewStub);
final Button addAddress = (Button) fview.findViewById(R.id.add_address_button);
final Button cancelAddress = (Button) fview.findViewById(R.id.cancel_address_button);
final ViewGroup addressesParent = (ViewGroup) fview.findViewById(R.id.addresses_container);
final Button addNewAddress = (Button) fview.findViewById(R.id.add_address);
addNewAddress.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//some codes ... OK
if (addressStub.getParent() != null) {
addressStubInflated = addressStub.inflate(); //OK
} else {
addressStubInflated.setVisibility(View.VISIBLE); //OK
}
addressStub.setOnInflateListener(new ViewStub.OnInflateListener() {
@Override
public void onInflate(ViewStub viewStub, View view) {
addAddress.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
addressStubInflated.setVisibility(View.GONE); //not work
addNewAddress.setVisibility(View.VISIBLE); //not work
addressesParent.setVisibility(View.VISIBLE); //not work
}
});
cancelAddress.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
addressStubInflated.setVisibility(View.GONE); //not work
addNewAddress.setVisibility(View.VISIBLE); //not work
addressesParent.setVisibility(View.VISIBLE); //not work
}
});
}
});
}
});
答案 0 :(得分:1)
我认为这里的问题是,每次点击一个按钮,您都会一次又一次地设置addressStub.setOnInflateListener
。
您只需要在onCreate中设置一次inflate监听器。然后按钮可以保持视图膨胀,当然,至少应该触发膨胀监听器。
此外,让您的类实现OnClickListener接口可以删除“回调地狱”