gpt4 book ai didi

android - 保留 viewpager fragment android 的状态

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:53:22 26 4
gpt4 key购买 nike

我在一个 fragment 中有一个 viewpager,我在其中显示了 3 个 fragment ,但是当我第一次打开该 fragment 时,它打开得很好,当我移动到另一个 fragment 并返回到 viewpager fragment 时,它没有显示 fragment 查看寻呼机然后我必须改变手机的方向或继续滑动 viewpager 以显示隐藏的 fragment 。

我用谷歌搜索了一下,现在我知道我必须为每个 fragment 设置标签并使用 findfragmentbytag 方法检索 fragment ,但问题是当我尝试将标签设置为 fragment 时

getSupportFragmentManager().beginTransaction().add(myFragment, "Some Tag").commit();

上面一行使我的应用程序崩溃并且 logcat 显示“无法将标签设置为 fragment ”,我知道我犯了一些愚蠢的错误

这是我的 fragment 的完整代码。如果有人能指导我在哪里编写设置 fragment 标签的代码,我将不胜感激。提前致谢。

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

}

}

enter image description here

编辑:我现在尝试通过标签从 viewpager 获取 fragment ,而不是每次都创建新 fragment 。但问题还是一样。

FixturesListingFragment fixture = (FixturesListingFragment) getActivity().getSupportFragmentManager().findFragmentByTag("android:switcher:"+R.id.pager+":1");
if(fixture == null)
fixture = new FixturesListingFragment();

最佳答案

您是否尝试过使用setOffscreenPageLimit?如果您有 3 个页面,则将其设置为 2。这样即使应用程序处于空闲状态,也不会重新创建页面。这是 link

关于android - 保留 viewpager fragment android 的状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21451544/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com