我有片段A,其中包含两个按钮(让我们说ButtonA和ButtonB),按下ButtonB我正在加载片段B(片段B包含1个按钮,让我们说按钮C),同时按下按钮C,弹出窗口将会出现。
我现在面临的问题,当我回来按下然后popwindow正在消失但加载片段A。
实际上我需要在片段B中显示popwindow时,然后用户按下后退按钮,弹出窗口应该只消失并且它仍然保留在片段B中,而不是去片段A。
我做了什么,从片段B我创建了一个接口并在一个Activity中传递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 :(得分:0)
与AlertDialogs不同,PopupWindows在后退时不会被隐式解雇。我们必须为它明确地编写逻辑。
更改你的onBackPressed()如下:
@Override
public void onBackPressed() {
if(popupWindow!=null && popupWindow.isShowing()){
popupWindow.dismiss();
backpopwindow.dismiss();
} else {
super.onBackPressed();
}
}
在这里,我们首先检查PopupWindow是否可见。如果它是可见的,只需关闭PopupWindow,然后通过调用super.onBackPressed()
来允许正常的onBackPressed流。
不知道使用PopupWindow的确切原因。请查看此更多选项并确定适合您需求的选项: