Android listener onDismiss无法处理片段
下面这个类是我用来显示和隐藏对话框的自定义对话框,在这个类中,我将覆盖onDismiss DialogFragment来隐藏对话框,但在实现的简单片段中,它无法侦听对话框,例如,这是我的自定义对话框:Android listener onDismiss无法处理片段,android,android-fragments,Android,Android Fragments,下面这个类是我用来显示和隐藏对话框的自定义对话框,在这个类中,我将覆盖onDismiss DialogFragment来隐藏对话框,但在实现的简单片段中,它无法侦听对话框,例如,这是我的自定义对话框: public class RobotDialog extends DialogFragment { @Bind(R.id.robot_dialog_icon) ImageView robot_dialog_icon; @Bind(R.id.frame) Rela
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;
}
}
公共类FragmentRoboAssistant扩展片段实现DialogInterface.OnDismissListener{
私有文本视图图标\箭头\右侧;
私有文本视图活动\u机器人\u标题;
私有文本视图图标发送消息给机器人;
私人编辑文本用户与机器人聊天;
私有列表消息=新的ArrayList();
私人RecyclerView MMessageView;
专用回收器查看适配器mAdapter;
私人通告RevalView RevalView;
私有android.os.Handler;
私人机器人对话;
私有文本视图图标\显示\机器人\助手;
私人语境;
私人背景色;
公共静态OnCloseSlidingLayer OnCloseSlidingLayer;
@凌驾
CreateView上的公共视图(布局、充气机、视图组容器、捆绑包保存状态){
视图=充气机。充气(R.layout.activity\u market\u robot,container,false);
context=getActivity().getBaseContext();
//@格式化程序:关闭
activity\u robot\u title=(TextView)view.findViewById(R.id.activity\u market\u robot\u title);
icon_send_message_to_robot=(TextView)view.findViewById(R.id.icon_send_message_to_robot);
icon\u arrow\u right=(TextView)view.findViewById(R.id.icon\u arrow\u right);
icon\u show\u robot\u helper=(TextView)view.findViewById(R.id.icon\u show\u robot\u helper);
用户与机器人聊天=(EditText)视图.findViewById(R.id.user与机器人聊天);
mMessagesView=(RecyclerView)view.findviewbyd(R.id.messages);
revealView=(CircularRevealView)view.findViewById(R.id.reveal);
//@格式化程序:打开
...
返回视图;
}
@凌驾
公共对话框(最终对话框界面对话框){
Log.e(“驳回”、“点击”);
}
公共接口OnCloseSlidingLayer{
void onClose();
}
公共静态无效集OnCloseSlidingLayer(OnCloseSlidingLayer l){
onCloseSlidingLayer=l;
}
}
onDismiss
在任何活动中都可以正常工作,但在这个片段上不起作用基于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);
}
基于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);
}
尝试将super.onDismiss(dialog)移到方法的末尾而不是开始处-看看这是否会改变任何东西。@jt gilkeson不起作用,因为片段不是活动,所以在对话框中调用getActivity不应返回FragmentRoboAssistant片段。您是否确保片段的父活动实现了OnDismissListener?通过将简单侦听器定义为inretface来解决此问题。我不会在父活动上实现此功能。无法在创建和显示RobotDialog的位置添加代码。请尝试将super.onDismiss(dialog)移到方法的末尾而不是开始处-查看是否有任何变化。@jt gilkeson不起作用,因为片段不是活动,所以在对话框中调用getActivity不应返回FragmentRobotAssistant片段。您是否确保片段的父活动实现了OnDismissListener?通过将简单侦听器定义为inretface来解决此问题。我不会在父活动上实现此功能。请不要在创建和显示RobotDialog的位置添加代码。您使用什么上下文显示该对话框?使用getFragmentManager()的FragmentRoboAssistant?还是家长活动?如果希望能够在FragmentRobotAssistant中处理onDismiss,则必须使用getFragmentManager上下文显示RobotFragment,而不是活动的上下文。您使用什么上下文显示对话框?使用getFragmentManager()的FragmentRoboAssistant?还是家长活动?如果希望能够在FragmentRobotAssistant中处理onDismiss,则必须使用getFragmentManager上下文显示RobotFragment,而不是活动的上下文。