我有一个包含几个片段的 FragmentPagerAdapter。这些 Fragment 的内容发生了变化,但我没有在我的更新函数中获得要更新的 Fragment。我已经尝试了这些建议,但没有运气:
这是我的代码的截图:
private void updateNavigation() {
//update code to redraw the fragment
//not workind
//mNavigationViewPager.getAdapter().notifyDataSetChanged();
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
Bundle args = new Bundle();
AFragment newFragment = new AFragment();
args.putString("test");
newFragment.setArguments(args);
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.navigation_pager, newFragment);
transaction.addToBackStack(null);
transaction.commit();
这是片段:
public static class AFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment, draw layout dynamically
MyLinearLayout fl = new MyLinearLayout(getActivity());
Button btn = new Button(getActivity());
btn.setText(String.valueOf(cnt++));
btn.setGravity(Gravity.LEFT);
btn.setPadding(28, 7, 0, 0);
fl.addView(btn, new MyLinearLayout.LayoutParams(7, 5, 70, 50));
fl.setPadding(5, 5, 5, 5);
}
return fl;
}
}
创建活动:(..)
mNavigationViewPager = new ViewPager(this);
mNavigationViewPager.setId(R.id.navigation_pager);
mTabsAdapter = new TabsAdapter(this, mNavigationViewPager);
mTabsAdapter.addTab(actionBar.newTab().setText("text1"),
fragmentNr1, null);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
当我打电话
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
一个选项菜单交互,然后做
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
在另一个选项菜单中,片段得到正确更新。