我在我的一个应用程序片段中使用ViewPager / FragmentPagerAdapter在每个页面中显示不同的列表视图(参见下图):
问题是,在我更改当前视图(片段)并再次重新打开它之后,列表视图未呈现(参见下图):
行为是随机的,有时当我在不同的标签/页面之间滑动时会显示列表视图。
这是我的片段代码:
public class AstucesFragment extends Fragment implements ActionBar.TabListener {
ActionBar actionBar;
AppSectionsPagerAdapter mAppSectionsPagerAdapter;
ViewPager mViewPager;
public static String[] tabslist = new String[5];
public AstucesFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_astuces, container, false);
tabslist[0] = getActivity().getResources().getString(R.string.astuces_quotidien);
tabslist[1] = getActivity().getResources().getString(R.string.astuces_resto);
tabslist[2] = getActivity().getResources().getString(R.string.astuces_loisirs);
tabslist[3] = getActivity().getResources().getString(R.string.astuces_transports);
tabslist[4] = getActivity().getResources().getString(R.string.astuces_tous);
mAppSectionsPagerAdapter = new AppSectionsPagerAdapter(getActivity().getSupportFragmentManager());
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
mViewPager = (ViewPager) view.findViewById(R.id.astuces_pager);
mViewPager.setAdapter(mAppSectionsPagerAdapter);
mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
@Override
public void onPageSelected(int position) {
actionBar.setSelectedNavigationItem(position);
}
});
actionBar.addTab(
actionBar.newTab()
.setCustomView(R.layout.fragment_astuces_tabs_quotidien)
.setTabListener(this));
actionBar.addTab(
actionBar.newTab()
.setCustomView(R.layout.fragment_astuces_tabs_resto)
.setTabListener(this));
actionBar.addTab(
actionBar.newTab()
.setCustomView(R.layout.fragment_astuces_tabs_loisirs)
.setTabListener(this));
actionBar.addTab(
actionBar.newTab()
.setCustomView(R.layout.fragment_astuces_tabs_transports)
.setTabListener(this));
actionBar.addTab(
actionBar.newTab()
.setCustomView(R.layout.fragment_astuces_tabs_tous)
.setTabListener(this));
return view;
}
@Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
mViewPager.setCurrentItem(tab.getPosition());
Log.i("AstucesFragment","onTabSelected()");
}
@Override
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
}
@Override
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
mViewPager.setCurrentItem(tab.getPosition());
Log.i("AstucesFragment","onTabReselected()");
}
/**
* A {@link android.support.v4.app.FragmentPagerAdapter} that returns a fragment corresponding to one of the primary
* sections of the app.
*/
public static class AppSectionsPagerAdapter extends FragmentPagerAdapter {
public AppSectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int i) {
Log.i("AppSectionsPagerAdapter","Page ID : "+i);
// The other sections of the app are dummy placeholders.
Fragment fragment = new AstucesListFragment();
Bundle args = new Bundle();
args.putString(AstucesListFragment.ARG_PAGE_CAT, tabslist[i]);
fragment.setArguments(args);
return fragment;
}
@Override
public int getCount() {
return tabslist.length;
}
@Override
public CharSequence getPageTitle(int position) {
return tabslist[position];
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
super.destroyItem(container, position, object);
}
}
/**
* A dummy fragment representing a section of the app, but that simply displays dummy text.
*/
public static class AstucesListFragment extends Fragment {
public static final String ARG_PAGE_CAT = "page_cat";
private List<Astuce> astuceList = new ArrayList<Astuce>();
private ListView listView;
private AstuceAdapter adapter;
private AstuceDAO astuceDAO;
public AstucesListFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_astuces_page, container, false);
Bundle args = getArguments();
Log.i("AstucesListFragment","onCreateView()");
String theme = "";
if (args.getString(ARG_PAGE_CAT) == getString(R.string.astuces_quotidien)) theme = "Q";
else if (args.getString(ARG_PAGE_CAT) == getString(R.string.astuces_resto)) theme = "R";
else if (args.getString(ARG_PAGE_CAT) == getString(R.string.astuces_loisirs)) theme = "L";
else if (args.getString(ARG_PAGE_CAT) == getString(R.string.astuces_transports)) theme = "T";
else theme = "";
astuceDAO = new AstuceDAO();
astuceList = astuceDAO.findAstuceByTheme(theme);
listView = (ListView) rootView.findViewById(R.id.astuces_list);
adapter = new AstuceAdapter(getActivity(), astuceList);
listView.setAdapter(adapter);
adapter.notifyDataSetChanged();
return rootView;
}
}
}
如何解决此问题?
答案 0 :(得分:4)
如果您希望片段在更改标签后仍保持其状态, 试试这个,
mViewPager.setOffscreenPageLimit(5); //因为你有5个片段
但这将使活动在调用活动的同时初始化所有片段。
http://developer.android.com/reference/android/support/v4/app/FragmentStatePagerAdapter.html
答案 1 :(得分:0)
您没有正确比较字符串。比较两个字符串时,您需要使用equals()方法而不是==运算符。因此onCreateView()
AstucesListFragment
替换
if (args.getString(ARG_PAGE_CAT) == getString(R.string.astuces_quotidien)) theme = "Q";
else if (args.getString(ARG_PAGE_CAT) == getString(R.string.astuces_resto)) theme = "R";
else if (args.getString(ARG_PAGE_CAT) == getString(R.string.astuces_loisirs)) theme = "L";
else if (args.getString(ARG_PAGE_CAT) == getString(R.string.astuces_transports)) theme = "T";
else theme = "";
带
if (args.getString(ARG_PAGE_CAT).equals(getString(R.string.astuces_quotidien))) theme = "Q";
else if (args.getString(ARG_PAGE_CAT).equals( getString(R.string.astuces_resto))) theme = "R";
else if (args.getString(ARG_PAGE_CAT).equals( getString(R.string.astuces_loisirs))) theme = "L";
else if (args.getString(ARG_PAGE_CAT).equals( getString(R.string.astuces_transports))) theme = "T";
else theme = "";
这可能会解决您的问题,因为==运算符的结果将不一致,因为两个字符串可能会或可能不会指向同一个对象。