在fragment里加了个Popup窗口,打开后在其他fragment里也能看到,如何让他只在打开的那个fragment里显示
在fragment里加了个Popup窗口,打开后在其他fragment里也能看到,怎么让他只在打开的那个fragment里显示?
在fragment里加了个Popup窗口,打开后在其他fragment里也能看到,怎么让他只在打开的那个fragment里显示?
用actionBar和fragment做的3个Tab间切换,每个Tab是一个Fragment。程序如下:
就在这个fragment里,点击btnOpenPopup按钮,显示一个popup窗口,但是这个popup窗口在其他3个fragment里也能看到,我估计是这里LayoutInflater layoutInflater = (LayoutInflater)getActivity().getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);的getActivity()得到的是MainActivity的context,所以里面3个fragment都能看到。但是我不知道怎么得到fragment的context。
------解决思路----------------------
LayoutInflater layoutInflater = (LayoutInflater)getActivity().getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View popupView = layoutInflater.inflate(R.layout.popup_win, null);
可以换成
View popupView = LayoutInflater.from(getActivity()).inflate(R.layout.popup_win, null);试试
在fragment里加了个Popup窗口,打开后在其他fragment里也能看到,怎么让他只在打开的那个fragment里显示?
用actionBar和fragment做的3个Tab间切换,每个Tab是一个Fragment。程序如下:
public class MainActivity extends Activity {
ActionBar.Tab tab1, tab2, tab3;
Fragment fragmentTab1 = new Tab_Conf();
Fragment fragmentTab2 = new Tab_Results();
Fragment fragmentTab3 = new Tab_History();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ActionBar actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
tab1 = actionBar.newTab().setText("1");
tab2 = actionBar.newTab().setText("2");
tab3 = actionBar.newTab().setText("3");
tab1.setTabListener(new MyTabListener(fragmentTab1));
tab2.setTabListener(new MyTabListener(fragmentTab2));
tab3.setTabListener(new MyTabListener(fragmentTab3));
actionBar.addTab(tab1);
actionBar.addTab(tab2);
actionBar.addTab(tab3);
}
}
public class MyTabListener implements ActionBar.TabListener {
Fragment fragment;
public MyTabListener(Fragment fragment) {
this.fragment = fragment;
}
public void onTabSelected(Tab tab, FragmentTransaction ft) {
ft.replace(R.id.fragment_container, fragment);
}
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
ft.remove(fragment);
}
public void onTabReselected(Tab tab, FragmentTransaction ft) {
// nothing done here
}
}
就在这个fragment里,点击btnOpenPopup按钮,显示一个popup窗口,但是这个popup窗口在其他3个fragment里也能看到,我估计是这里LayoutInflater layoutInflater = (LayoutInflater)getActivity().getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);的getActivity()得到的是MainActivity的context,所以里面3个fragment都能看到。但是我不知道怎么得到fragment的context。
public class Tab_Results extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.tab_results, container, false);
final Button btnOpenPopup = (Button) view.findViewById(R.id.button1);
btnOpenPopup.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View arg0) {
LayoutInflater layoutInflater = (LayoutInflater)getActivity().getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View popupView = layoutInflater.inflate(R.layout.popup_win, null);
final PopupWindow popupWindow = new PopupWindow(popupView,LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
Button btnDismiss = (Button) popupView.findViewById(R.id.dismiss);
btnDismiss.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
popupWindow.dismiss();
}
});
popupWindow.showAsDropDown(btnOpenPopup, 50, -30);
}
});
return view;
------解决思路----------------------
LayoutInflater layoutInflater = (LayoutInflater)getActivity().getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View popupView = layoutInflater.inflate(R.layout.popup_win, null);
可以换成
View popupView = LayoutInflater.from(getActivity()).inflate(R.layout.popup_win, null);试试