[英]Retaining the instance state of Fragments in Fragment Pager Adapter
我遇到一個令人沮喪的問題。 我能夠創建尋呼機適配器處理的每個片段,並且能夠向右輕掃以查看所有片段; 但是,向左滑動時,這些片段要么消失了,要么只是我已經看過的片段的副本。 我四處搜尋,卻找不到很多,這讓人感到困惑,因為FragmentPagerAdapter的API表示將每個片段都保留在內存中。 我將最多顯示20個片段,因此內存不是問題。 無論如何,這是我的代碼,感謝您提供的任何反饋; 它仍然處於預Alpha階段。
@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;
}
}
是的,您是對的片段被保留在內存中,但是它將占用大量內存,因此,正如文檔所說,不會提供您想要的預期結果:
用戶訪問的每個頁面的片段將保留在內存中,盡管其視圖層次結構在不可見時可能會被破壞。 由於片段實例可以保持任意數量的狀態,因此這可能導致使用大量內存。 對於較大的頁面集,請考慮FragmentStatePagerAdapter。
如果要在適配器中包含很多頁面/片段,則是FragmentStatePagerAdapter 。
使用FragmentStatePagerAdapter時,我能夠解決兩個問題:
1)左右滑動時,令人沮喪地消失的重復片段。 這是因為由於EventFragment.newInstance(position + 1),getItem()對相同的片段調用了兩次。
2)保存片段的實例狀態,以便我可以連續左右滑動而不會出現空白頁。
感謝您的幫助,@ Rod_Algonquin。 節省了我數小時的壓力。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.