我遇到了令人沮丧的问题。我能够创建寻呼机适配器处理的每个片段,并且我能够向右滑动以查看所有这些片段;然而,在向左滑动时,碎片要么消失,要么只是我已经看过的碎片的副本。我google了一下,找不到太多,这让人感到困惑,因为FragmentPagerAdapter的API说它将每个片段保存在内存中。我将显示最多20个片段,因此内存不是问题。无论如何,这是我的代码,我感谢您提供的任何反馈;它仍处于前阿尔法阶段。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_events_screen);
// Create the adapter that will return a fragment for each of the three
// primary sections of the activity.
SectionsPagerAdapter adapter = new SectionsPagerAdapter(getFragmentManager());
// Set up the ViewPager with the sections adapter.
ViewPager pager = (ViewPager) findViewById(R.id.pager);
pager.setAdapter(adapter);
}
/**
* TEMPORARY
*/
public void onBackPressed() {
finish();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.events_screen, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A {@link FragmentPagerAdapter} that returns a fragment corresponding to
* one of the sections/tabs/pages.
*/
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
// getItem is called to instantiate the fragment for the page.
// Return a PlaceholderFragment (defined as a static inner class
// below).
return EventFragment.newInstance(position + 1);
}
/**
* Total number of pages (fragments) there are
* Given by size of the array returned by Service.getEvents()
*/
@Override
public int getCount() {
return connection.getEvents().size();
}
@Override
public CharSequence getPageTitle(int position) {
return null;
}
}
/**
* The fragment holding the text for each event.
*/
public static class EventFragment extends Fragment {
static int index;
/**
* Returns a new instance of this fragment for the given section number.
*/
public static EventFragment newInstance(int sectionNumber) {
index = sectionNumber;
EventFragment fragment = new EventFragment();
return fragment;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_events_screen, container, false);
TextView textView = (TextView) rootView.findViewById(R.id.section_label);
textView.setText(connection.getEvents().get(index - 1));
return rootView;
}
}
答案 0 :(得分:1)
是的,你是正确的片段保存在内存中,但它将使用大量的内存,因此提供了你想要的预期结果,正如文档所说:
用户访问的每个页面的片段将保留在内存中,但其视图层次结构可能会在不可见时被销毁。这可能导致使用大量内存,因为片段实例可以保持任意数量的状态。对于较大的页面集,请考虑FragmentStatePagerAdapter。
如果你想在适配器中有很多页面/片段,FragmentStatePagerAdapter就是你要找的那个。
答案 1 :(得分:1)
使用FragmentStatePagerAdapter后,我能解决两个问题:
1)当我向左和向左滑动时,重复的片段令人沮丧地消失了。这是因为getItem()因为EventFragment.newInstance(position + 1)而对同一个片段调用了两次。
2)保存片段的实例状态,以便我可以连续左右滑动而不会出现空白页。
感谢您的帮助@Rod_Algonquin。救了我几个小时的压力。