下面这个类是我自定义的对话框来显示和隐藏对话框,在这个类中我重写了onDismiss DialogFragment以隐藏对话框,但是在实现中无法监听的简单片段中,例如这是我的自定义对话框:
public class RobotDialog extends DialogFragment {
@Bind(R.id.robot_dialog_icon)
ImageView robot_dialog_icon;
@Bind(R.id.frame)
RelativeLayout frame;
public RobotDialog() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(0));
View view = inflater.inflate(R.layout.fragment_robot_dialog, container, false);
ButterKnife.bind(this, view);
return view;
}
@Override
public void onStart() {
super.onStart();
Window window = getDialog().getWindow();
WindowManager.LayoutParams windowParams = window.getAttributes();
windowParams.dimAmount = 0.0f;
window.setAttributes(windowParams);
}
@Override
public void onDismiss(final DialogInterface dialog) {
super.onDismiss(dialog);
final Activity activity = getActivity();
if (activity != null && activity instanceof DialogInterface.OnDismissListener) {
((DialogInterface.OnDismissListener) activity).onDismiss(dialog);
}
}
@Override
public void onActivityCreated(Bundle arg0) {
super.onActivityCreated(arg0);
getDialog().getWindow()
.getAttributes().windowAnimations = R.style.DialogAnimation;
}
}
这是我要展示的简单片段,必须听取onDismiss
关闭对话框:
public class FragmentRobotAssisstant extends Fragment implements DialogInterface.OnDismissListener {
private TextView icon_arrow_right;
private TextView activity_robot_title;
private TextView icon_send_message_to_robot;
private EditText user_chat_with_robot;
private List<Message> mMessages = new ArrayList<Message>();
private RecyclerView mMessagesView;
private RecyclerView.Adapter mAdapter;
private CircularRevealView revealView;
private android.os.Handler handler;
private RobotDialog robotDialog;
private TextView icon_show_robot_helper;
private Context context;
private int backgroundColor;
public static OnCloseSlidingLayer onCloseSlidingLayer;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.activity_market_robot, container, false);
context = getActivity().getBaseContext();
//@formatter:off
activity_robot_title = (TextView) view.findViewById(R.id.activity_market_robot_title);
icon_send_message_to_robot = (TextView) view.findViewById(R.id.icon_send_message_to_robot);
icon_arrow_right = (TextView) view.findViewById(R.id.icon_arrow_right);
icon_show_robot_helper = (TextView) view.findViewById(R.id.icon_show_robot_helper);
user_chat_with_robot = (EditText) view.findViewById(R.id.user_chat_with_robot);
mMessagesView = (RecyclerView) view.findViewById(R.id.messages);
revealView = (CircularRevealView) view.findViewById(R.id.reveal);
//@formatter:on
...
return view;
}
@Override
public void onDismiss(final DialogInterface dialog) {
Log.e("DISMISS", " CLICKED");
}
public interface OnCloseSlidingLayer {
void onClose();
}
public static void setOnCloseSlidingLayer(OnCloseSlidingLayer l) {
onCloseSlidingLayer = l;
}
}
来自片段的 onDismiss
可以在任何活动上正常工作,但它对此片段无效
答案 0 :(得分:0)
以Jace的评论为基础,在RobotoDialog中:
@Override
public void onDismiss(final DialogInterface dialog) {
final Fragment parent = getParentFragment();
if (parent != null && parent instanceof DialogInterface.OnDismissListener) {
((DialogInterface.OnDismissListener) parent).onDismiss(dialog);
}
super.onDismiss(dialog);
}