FragmentPagerAdapter中的Fragment / ViewPager不更新其View

时间:2012-10-03 10:28:34

标签: android view

我有一个FragmentPagerAdapter,它包含几个片段。这些片段的内容发生了变化,但我没有在更新功能中更新片段。 我已经毫不费力地尝试了这些建议:

ViewPager PagerAdapter not updating the View

Android force Fragment to rebuild View

以下是我的代码:

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

在另一个选项菜单中,片段更新正确。

1 个答案:

答案 0 :(得分:0)

它正在发挥作用,我只是打错了。