2012-10-03 85 views
0

我有一個FragmentPagerAdapter,它包含多個片段。這些碎片的內容會發生變化,但我沒有在更新函數中更新碎片。 我已經嘗試過這些建議沒有運氣:FragmentPagerAdapter中的片段/ ViewPager未更新其視圖

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

,碎片被更新正確。

+0

[ViewPager PagerAdapter不更新視圖]的可能重複(http://stackoverflow.com/questions/7263291/viewpager-pageradapter-not-updating-the-view) –

回答

0

這是工作,我只是犯了一個錯字。

相關問題