【问题标题】:Refreshing parent fragment on PopupWindow.dismiss在 PopupWindow.dismiss 上刷新父片段
【发布时间】:2015-02-17 20:16:30
【问题描述】:

我有一个 popupWindow,它修改了在父窗口中加载微调器的 SQLight 表。当我关闭 PopupWindow 时,我希望用新值刷新父窗口中的微调器。下面的代码显示了我创建侦听器的进度,该侦听器将检测 PopupWindow 的解雇。到目前为止,我的听众不起作用。我认为我在构建听众时遗漏了一些东西。我已经包含了 ShowPopup 类以及作为此窗口父级的片段 (Tab3Fragment)。

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();              
   }
}

【问题讨论】:

  • 好的 - 我已经广泛修改了 showPopup 类并删除了无关的语言。甚至可以在被解除的 popupWindow 类之外实现 onDismissListener() 吗?我在这里想念什么? TIA 显示了耐心并提供了有用的反馈。

标签: android performance android-fragments listener popupwindow


【解决方案1】:

从来没有达到我可以直接在子 PopupWindow 关闭时刷新父窗口的地步。最终解决方案(解决方法)是在来自微调器的 onTouch 事件之后使用 fragmentmanager replace(),仅当设置了指示修改的 SQL 微调器查找表的静态标志 (iSpeciesRefresh) 时。

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();       
    }
}

【讨论】:

    【解决方案2】:

    在您的onCreateViewonViewCreated 方法中,您执行btnPopup.setOnClickListener(this);,但随后在您的showPopup 类的init() 方法中,您将片段覆盖为侦听器,而是分配一个新的匿名侦听器。我的猜测是,您需要重新设计分配侦听器的方式,并确保不会那样覆盖它。

    附:为了可维护性(以及查看您代码的其他开发人员的理智),使用大写驼峰式命名类是惯例,它们不应包含动词。像MyCustomPopup 这样的东西会更好。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多