2016-06-20 112 views
1

這個下面的類是我的自定義對話框來顯示和隱藏對話框,在這個類我重寫onDismiss DialogFragment隱藏對話框,但在實現了,不能聽那個簡單的片段,爲例如,這是我的自定義對話框:Android監聽器onDismiss不工作片段

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從片段做工精細的任何活動,但它的確如此在這個片段NT上工作傑斯的評論

+0

嘗試移動super.onDismiss(對話)的方法,而不是開始的結束 - 看看是否改變任何東西。 –

+0

@ jt-gilkeson does not work sir –

+1

片段不是活動,所以在對話框中調用getActivity不應返回您的FragmentRobotAssisstant片段。你確定片段的父活動實現了OnDismissListener嗎? –

回答

0

大廈,在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); 
} 
+0

不適用於我的代碼 –

+1

您使用什麼上下文來顯示對話框? FragmentRobotAssisstant與getFragmentManager()?或父母的活動?如果您希望能夠處理FragmentRobotAssisstant中的onDismiss,則必須使用getFragmentManager上下文顯示RobotFragment,而不是活動的上下文。 –