我有一個viewpager在一個片段中,我顯示了3個片段,但是當我第一次打開片段時,它打開的很好,當我移動到另一個片段並返回到viewpager片段時,它不會在查看尋呼機中不顯示片段,然後我必須改變手機的方向,或者不斷刷看視圖,以顯示隱藏的片段。保留viewpager片段的狀態android
我用Google搜索它,現在我知道我要的標籤設置爲每個片段並用findfragmentbytag方法來檢索片段,但問題是,當我嘗試設置標籤片段
getSupportFragmentManager().beginTransaction().add(myFragment, "Some Tag").commit();
上面一行崩潰我應用程序和logcat顯示「不能設置標記到片段」,我知道我在做一些愚蠢的錯誤
這是我的片段的完整代碼。如果能指導我在哪裏編寫用於設置片段標籤的代碼,我將非常感激。提前致謝。
public class MatchesListingFragment extends Fragment implements ActionBar.TabListener {
Context context;
SectionsPagerAdapter mSectionsPagerAdapter;
ViewPager mViewPager;
ActionBar actionBar;
Matchlistings db;
Bundle savedInstanceState;
Fragment livescore,fixture,results;
@Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
this.savedInstanceState = savedInstanceState;
db = new Matchlistings(getActivity());
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_matches, container, false);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
context = getActivity().getApplicationContext();
if(savedInstanceState == null){
livescore = new LiveScoreListingFragment();
fixture = new FixturesListingFragment();
results = new ResultsListingFragment();
}
if(!Constants.is_listing_refreshed){
Log.i(MainActivity.TAG,"refreshing list");
db.loadListingsFromServer().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
actionBar = getActivity().getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
if(mSectionsPagerAdapter == null)
mSectionsPagerAdapter = new SectionsPagerAdapter(getActivity().getSupportFragmentManager());
mViewPager = (ViewPager) getView().findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);
mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
@Override
public void onPageSelected(int position) {
actionBar.setSelectedNavigationItem(position);
}
});
for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {
Tab tb = actionBar.newTab()
.setText(mSectionsPagerAdapter.getPageTitle(i))
.setTabListener(this);
actionBar.addTab(tb);
}
}
public class SectionsPagerAdapter extends FragmentPagerAdapter {
Fragment fragment;
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
switch(position){
case 0:
fragment = livescore;
return fragment;
case 1:
fragment = fixture;
return fragment;
case 2:
fragment = results;
return fragment;
case 3:
fragment = new LiveScoreListingFragment();
return fragment;
}
return null;
}
@Override
public int getCount() {
return 4;
}
@Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return "LIVE";
case 1:
return "FIXTURES";
case 2:
return "RESULTS";
case 3:
return "HOT";
}
return null;
}
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
actionBar.removeAllTabs();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
}
@Override
public void onTabReselected(Tab arg0, FragmentTransaction arg1) {
// TODO Auto-generated method stub
}
@Override
public void onTabSelected(Tab arg0, FragmentTransaction arg1) {
// TODO Auto-generated method stub
}
@Override
public void onTabUnselected(Tab arg0, FragmentTransaction arg1) {
// TODO Auto-generated method stub
}
}
編輯: 我現在已經試圖通過標籤來得到viewpager的片段,而不是創造新的片段,每次的。但問題仍然是一樣的。
FixturesListingFragment fixture = (FixturesListingFragment) getActivity().getSupportFragmentManager().findFragmentByTag("android:switcher:"+R.id.pager+":1");
if(fixture == null)
fixture = new FixturesListingFragment();
FragmentPagerAdapter使用標籤本身 – MGDroid
我該如何使用它來獲取片段? –