2016-11-16 104 views
0

我有片段A,其中包含兩個按鈕(讓說ButtonA和ButtonB),同時按ButtonB我加載片段B(片段B包含1個按鈕,讓說按鈕C),而按下按鈕C,彈出窗口即將出現。片段回來按,加載回片段

我面對現在,當我返回按然後popwindow是disapperaing但裝載片段A.

其實我當popwindow在B片段中顯示需要,以及用戶按後退按鈕然後,彈出窗口只應該問題消失,它仍然在片段B,而不是片段A.

我做了什麼,從片段B我創建了一個接口,並通過一個活動中的Popwindow實例,並從活動我解散popwindow 。

@Override 
    public void onHandleRequest(PopupWindow popupWindow, PopupWindow backpopwindow, String fragmentName) { 
     if(fragmentName !=null){ 
      this.fragmentName=fragmentName; 
      if(popupWindow!=null){ 
       this.popupWindow=popupWindow; 
       this.backpopwindow=backpopwindow; 

      } 
     } 



@Override 
    public void onBackPressed() { 
     super.onBackPressed(); 

     if(popupWindow!=null && popupWindow.isShowing()){ 
      popupWindow.dismiss(); 
      backpopwindow.dismiss(); 
     } 
} 

在我的片段類:

public interface OnHandleCallBack{ 
     public void onHandleRequest(PopupWindow popupWindow,PopupWindow backpopwindow,String fragmentName); 
    } 

public void showPopup(View view, Context context,ArrayList<String> arrayList) { 

     if(popupWindow!=null && popupWindow.isShowing()){ 
      popupWindow.dismiss(); 
     } 

     mLayoutManager.setScrollEnabled(false); 
     LayoutInflater inflater = (LayoutInflater) context 
       .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     //Making transparent layout 

     final View popupView1 = inflater.inflate(R.layout.transparent_layout, 
       (ViewGroup) view.findViewById(R.id.transprent)); 

     popupWindow1 = new PopupWindow(popupView1, WindowManager.LayoutParams.MATCH_PARENT,WindowManager.LayoutParams.MATCH_PARENT); 
     popupWindow1.showAtLocation(popupView1, Gravity.CENTER, 0 , 0); 
     popupWindow1.setBackgroundDrawable(new BitmapDrawable()); 
     popupWindow1.setOutsideTouchable(true); 
     popupWindow1.showAsDropDown(popupView1, 0, 0); 

     final View popupView = inflater.inflate(R.layout.my_order_truck_popup_win, 
       (ViewGroup) view.findViewById(R.id.popup_win)); 

     RecyclerView mRecyclerView = (RecyclerView) popupView.findViewById(R.id.truck_recyler_id); 
     TextView textView=(TextView) popupView.findViewById(R.id.txt_truck); 
     RelativeLayout cancl_rl=(RelativeLayout)popupView.findViewById(R.id.rl_cncl); 
     textView.setText(context.getResources().getString(R.string.trucks)); 
     Config.colorFont(context,null,textView,null); 
     popupView.setAnimation(AnimationUtils.loadAnimation(context, R.anim.pop_up_window)); 

     /********* Here is my PopUp window ********/ 
     Float m=TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 300, getResources().getDisplayMetrics()); 
     int m1=Math.round(m); 
     popupWindow = new PopupWindow(popupView,m1,WindowManager.LayoutParams.WRAP_CONTENT); 

     popupWindow.showAtLocation(popupView, Gravity.CENTER, 0 , 0); 
     popupWindow.setBackgroundDrawable(new BitmapDrawable()); 
     popupWindow.setOutsideTouchable(true); 

     MyOrderPopUPAdapter mAdapter = new MyOrderPopUPAdapter(context, arrayList); 
     LinearLayoutManager LayoutManager = new LinearLayoutManager(context); 
     mRecyclerView.setLayoutManager(LayoutManager); 
     mRecyclerView.setItemAnimator(new DefaultItemAnimator()); 
     mRecyclerView.setAdapter(mAdapter); 
     onHandleCallBack.onHandleRequest(popupWindow,popupWindow1,"orderfragment"); 

     popupWindow1.setTouchInterceptor(new View.OnTouchListener() { 
      @Override 
      public boolean onTouch(View v, MotionEvent event) { 
       popupWindow.dismiss(); 
       popupWindow1.dismiss(); 
       mLayoutManager.setScrollEnabled(true); 
       return true; 
      } 
     }); 



     cancl_rl.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       popupWindow.dismiss(); 
       popupWindow1.dismiss(); 
       mLayoutManager.setScrollEnabled(true); 

      } 
     }); 

     popupWindow.showAsDropDown(popupView, 0, 0); 
    } 

@Override 
    public void onAttach(Context context) { 
     super.onAttach(context); 
     try { 
      onHandleCallBack = (OnHandleCallBack)context; 

     } catch (ClassCastException e) { 
      throw new ClassCastException(context.toString() 
        + " must implement onHandleCallBack"); 
     } 
    } 
+0

什麼popwindow從延伸? –

+0

@masoudvali當我點擊片段B中的按鈕C時,會出現Popwindow,這個程序員像這樣創建PopupWindow popupWindow = new PopupWindow(popupView,LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT); –

+0

我知道,我想知道它的類是基於alertdialog還是不是 –

回答

0

不像AlertDialogs,PopupWindows並未隱駁回背壓。我們必須明確地寫出它的邏輯。

更改您的onBackPressed()如下:

@Override 
    public void onBackPressed() { 
     if(popupWindow!=null && popupWindow.isShowing()){ 
      popupWindow.dismiss(); 
      backpopwindow.dismiss(); 
     } else { 
      super.onBackPressed(); 
     } 
} 

在這裏,我們首先檢查PopupWindow是否可見。如果它是可見的,只要通過調用super.onBackPressed()就可以關閉PopupWindow,否則允許正常的onBackPressed流。

不知道你使用PopupWindow的確切原因。檢查了這一點了更多的選擇和決定什麼適合你的需要:

Android Popup Window vs Android Dialog

Dialogs and Popups in Android

+0

我認爲他沒有popwayow的問題,而後面的新聞,片段A進入​​可見性,他希望片段B當popwindow解僱 –