在PopupWindow.dismiss清爽父片段 [英] Refreshing parent fragment on PopupWindow.dismiss

查看:227
本文介绍了在PopupWindow.dismiss清爽父片段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有修改了加载在父窗口一个微调一个SQLight表popupWindow。我想在用新值刷新,当我解雇PopupWindow父窗口的微调。下面的code展示我的进步创造一个监听器,将检测PopupWindow的解雇。我至今监听器不工作。我认为,我已经错过了在构建听众的东西。我已经包含了ShowPopup类以及片段(Tab3Fragment),这是父窗口。

showPopup.java

 公共类showPopup扩展PopupWindow {
上下文m_context;
按钮btnDismiss;
PopupWindow弹出;
Tab3Fragment Tab3Fragment;
OnDismissListener监听;公共showPopup(上下文的背景下){
    超级(上下文);
    m_context =背景; //被注释掉    的setContentView(LayoutInflater.from(上下文).inflate(R.layout.popup_layout,NULL));
    自动调用setHeight(LayoutParams.WRAP_CONTENT);
    setWidth(LayoutParams.WRAP_CONTENT);
}
公共无效的init(视图v){
    LayoutInflater吹气=(LayoutInflater)m_context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    查看popUpView = inflater.inflate(R.layout.popup_layout,空,假);
    最后PopupWindow弹出=新PopupWindow(popUpView,600,400,TRUE);    popup.setContentView(popUpView);
    popup.showAtLocation(ⅴ,Gravity.CENTER_HORIZONTAL,0,0);    btnDismiss =(按钮)popUpView.findViewById(R.id.btndismissxml);
    btnDismiss.setOnClickListener(新View.OnClickListener(){
        @覆盖
        公共无效的onClick(查看锚){
            popup.dismiss();
        }
    });
}
@覆盖
公共无效setOnDismissListener(OnDismissListener监听){
    this.listener =侦听器;
  }
}

Tab3Fragment.java

 公共类Tab3Fragment扩展片段实现OnClickListener {
上下文语境;
按钮btnPopup,btnSpinnerRefresh;
微调spinnerSpecies;
公共静态INT iSpeciesPosition;
showPopup showPopup;
ArrayAdapter<串GT; arrayAdapterSpecies;
OnDismissListener dismissListener;@覆盖
公共查看onCreateView(LayoutInflater吹气,ViewGroup中containerGroup,捆绑savedInstanceState){
    视图V = inflater.inflate(R.layout.tab3_fragment,containerGroup,FALSE);    btnP​​opup =(按钮)v.findViewById(R.id.btnPopupxml);
    btnP​​opup.setOnClickListener(本);    btnSpinnerRefresh =(按钮)v.findViewById(R.id.btnSpinnerRefreshxml);
    btnSpinnerRefresh.setOnClickListener(本);    spinnerSpecies =(微调)v.findViewById(R.id.spinnerSpeciesxml);
    spinnerSpecies.setAdapter(arrayAdapterSpecies);    如果(savedInstanceState!= NULL){
        iSpeciesPosition = savedInstanceState.getInt(speciesPosition_key);
        populateTab3Fragment(五);
    }否则如果(savedInstanceState == NULL){
        populateTab3Fragment(五);
    }
    返回伏;
}
// @覆盖
公共无效onViewCreated(视图v){
    populateTab3Fragment(五);
    /******************************************************************************************************
    可以setOnDismissListener showPopup类的外部使用,以表明showPopup驳回?
    ******************************************************************************************************/
    showPopup弹出=新showPopup(背景);
    popup.setOnDismissListener(新OnDismissListener(){        @覆盖
        公共无效onDismiss(){
            Toast.makeText(getActivity()getApplicationContext(),onDismiss()的工作。,Toast.LENGTH_LONG).show();
            loadSpinnerData();
        }
    });
}
@覆盖
公共无效的onClick(视图v){
    开关(v.getId()){
    案例R.id.btnPopupxml:
        新showPopup(getActivity()getApplicationContext()。)的init(ⅴ)。
        打破;
    案例R.id.btnSpinnerRefreshxml:
        loadSpinnerData(); //临时解决方法刷新微调...
        打破;
    }
}
/ **
 *功能加载从SQLite数据库的数据微调
 * * /
公共无效loadSpinnerData(){
    //省略
}
公共无效populateTab3Fragment(视图v){
    loadSpinnerData();
   }
}


解决方案

从来没有在那里我可以直接刷新父窗口对孩子PopupWindow驳回点。最终的解决方案(解决方法)是仅当一个静态的标志(iSpeciesRefresh)已设置指示修改后的SQL微调查找表中的微调的onTouch事件后使用fragmentmanager更换()。

 公共类dataCapture扩展片段实现OnClickListener {
串szSpecies;
静态公众诠释iSpeciesRefresh = 1;
微调spinnerSpecies;
...
@覆盖
公共查看onCreateView(LayoutInflater充气器,容器的ViewGroup,捆绑savedInstanceState){
    视图V = inflater.inflate(R.layout.data_capture,集装箱,FALSE);
...
    spinnerSpecies =(微调)v.findViewById(R.id.spinnerSpeciesxml);    spinnerSpecies.setOnTouchListener(新View.OnTouchListener(){//需要刷新的片段......
        @覆盖
        公共布尔onTouch(查看视图,MotionEvent motionEvent){
            如果(iSpeciesRefresh == 1){//这是默认的应用
                //没做什么
            }否则如果(iSpeciesRefresh == 0){//值复位为0SAVE,UPDATE或DELETE在speciesPopupWindow。
                refreshDataCapture();
                iSpeciesRefresh = 1;
            }
            返回false;
        }
    });
...
}
公共无效refreshDataCapture(){
    片段currentFragment =(dataCapture)getFragmentManager()findFragmentByTag(data_capture);
    如果(currentFragment == NULL){
        currentFragment =新dataCapture();
    }否则如果(currentFragment!= NULL){
        。getFragmentManager()popBackStack(NULL,FragmentManager.POP_BACK_STACK_INCLUSIVE);
        。getFragmentManager()调用BeginTransaction()取代(R.id.fragment_placeholder,新dataCapture(),data_capture)addToBackStack(空).commit()。
    }
}

I have a popupWindow that modifies a SQLight table that loads a spinner in the parent window. I would like to have the spinner in the parent window refreshed with the new values when I dismiss the PopupWindow. The code below shows my progress creating a listener that would detect the PopupWindow's dismissal. The listener that I have so far does not work. I think that I have missed something in structuring the listener. I've included the ShowPopup class as well as the fragment (Tab3Fragment) that is the parent to this window.

showPopup.java

public class showPopup extends PopupWindow{
Context m_context;
Button btnDismiss;
PopupWindow popup;
Tab3Fragment Tab3Fragment;
OnDismissListener listener;

public showPopup(Context context){
    super(context);
    m_context = context;//was commented out

    setContentView(LayoutInflater.from(context).inflate(R.layout.popup_layout, null));
    setHeight(LayoutParams.WRAP_CONTENT);
    setWidth(LayoutParams.WRAP_CONTENT);
}
public void init(View v){
    LayoutInflater inflater = (LayoutInflater)m_context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View popUpView = inflater.inflate(R.layout.popup_layout, null, false);
    final PopupWindow popup = new PopupWindow(popUpView, 600, 400, true);  

    popup.setContentView(popUpView);
    popup.showAtLocation(v, Gravity.CENTER_HORIZONTAL, 0, 0);

    btnDismiss = (Button) popUpView.findViewById(R.id.btndismissxml);
    btnDismiss.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View anchor) {
            popup.dismiss();
        }
    });
}
@Override
public void setOnDismissListener(OnDismissListener listener){
    this.listener = listener;
  }  
}

Tab3Fragment.java

public class Tab3Fragment extends Fragment implements OnClickListener{
Context context;
Button btnPopup, btnSpinnerRefresh;
Spinner spinnerSpecies;
public static int iSpeciesPosition;
showPopup showPopup;
ArrayAdapter<String> arrayAdapterSpecies;
OnDismissListener dismissListener;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup containerGroup, Bundle savedInstanceState) {    
    View v = inflater.inflate(R.layout.tab3_fragment, containerGroup, false);    

    btnPopup = (Button)v.findViewById(R.id.btnPopupxml);
    btnPopup.setOnClickListener(this);

    btnSpinnerRefresh = (Button)v.findViewById(R.id.btnSpinnerRefreshxml);
    btnSpinnerRefresh.setOnClickListener(this);

    spinnerSpecies = (Spinner) v.findViewById(R.id.spinnerSpeciesxml);
    spinnerSpecies.setAdapter(arrayAdapterSpecies);

    if(savedInstanceState != null){
        iSpeciesPosition = savedInstanceState.getInt("speciesPosition_key");
        populateTab3Fragment(v);
    }else if(savedInstanceState == null){
        populateTab3Fragment(v);
    }
    return v;       
}
//@Override
public void onViewCreated(View v) {
    populateTab3Fragment(v);
    /******************************************************************************************************
    Can setOnDismissListener be used outside of showPopup class to indicate that showPopup has dismissed? 
    ******************************************************************************************************/
    showPopup popup = new showPopup(context);
    popup.setOnDismissListener(new OnDismissListener(){

        @Override
        public void onDismiss(){
            Toast.makeText(getActivity().getApplicationContext(), "onDismiss() works.", Toast.LENGTH_LONG).show();
            loadSpinnerData();
        }  
    });         
}
@Override
public void onClick(View v) {
    switch (v.getId()) {
    case R.id.btnPopupxml:
        new showPopup(getActivity().getApplicationContext()).init(v);
        break;
    case R.id.btnSpinnerRefreshxml:
        loadSpinnerData();//temporary workaround to refresh spinner...
        break;
    }
}
/**
 * Function to load the spinner data from SQLite database
 * */
public void loadSpinnerData() {
    //omitted
}
public void populateTab3Fragment(View v){   
    loadSpinnerData();              
   }
}

解决方案

Never got to the point where I could refresh the parent window directly on the child PopupWindow dismiss. The final solution (workaround) was to use fragmentmanager replace() after an onTouch event from the spinner only when a static flag (iSpeciesRefresh) had been set that indicated a modified SQL spinner lookup table.

public class dataCapture extends Fragment implements OnClickListener {
String szSpecies;
static public int iSpeciesRefresh = 1;
Spinner spinnerSpecies;
...
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.data_capture, container, false);
...
    spinnerSpecies = (Spinner) v.findViewById(R.id.spinnerSpeciesxml);

    spinnerSpecies.setOnTouchListener(new View.OnTouchListener() {//refreshes fragment as needed...
        @Override
        public boolean onTouch(View view, MotionEvent motionEvent) {
            if (iSpeciesRefresh == 1) {//this is application default
                //do nothing
            } else if (iSpeciesRefresh == 0) {//value is reset to "0" at SAVE, UPDATE, or DELETE in speciesPopupWindow.
                refreshDataCapture();
                iSpeciesRefresh = 1;
            }
            return false;
        }
    });
...
}    
public void refreshDataCapture() {
    Fragment currentFragment = (dataCapture) getFragmentManager().findFragmentByTag("data_capture");
    if (currentFragment == null) {
        currentFragment = new dataCapture();
    } else if (currentFragment != null) {
        getFragmentManager().popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
        getFragmentManager().beginTransaction().replace(R.id.fragment_placeholder, new dataCapture(), "data_capture").addToBackStack(null).commit();       
    }
}

这篇关于在PopupWindow.dismiss清爽父片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆