我有一个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();
}
}
答案 0 :(得分:3)
从来没有达到我可以直接刷新子窗口PopupWindow关闭的父窗口的程度。最后的解决方案(解决方法)是仅在设置了表示修改后的SQL微调器查找表的静态标志(iSpeciesRefresh)时才在微调器的onTouch事件之后使用fragmentmanager replace()。
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();
}
}
答案 1 :(得分:1)
在您的onCreateView
和onViewCreated
方法中,您执行btnPopup.setOnClickListener(this);
,然后在您的showPopup类中使用init()
MyCustomPopup
方法,您将片段覆盖为侦听器,而是分配新的匿名侦听器。我的猜测是你需要重新设计你的听众如何分配,并确保你不会像那样覆盖它。
P.S。出于可维护性的考虑(以及查看代码的其他开发人员的理智),使用上部驼峰案例命名类并且不应包含动词的约定。像{{1}}这样的东西会更好。