在片段分页器适配器中保留片段的实例状态



我遇到了一个令人沮丧的问题。我能够创建寻呼机适配器处理的每个片段,并且我能够向右滑动以查看所有这些片段;然而,当向左滑动时,碎片要么消失了,要么只是我已经看到的那个的副本。我用谷歌搜索了一下,找不到太多,这是令人困惑的,因为FragmentPagerAdapter的API说它将每个片段保存在内存中。我将显示最多20个片段,所以内存不是问题。无论如何,这是我的代码,我感谢任何反馈你可以给;它还在pre-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)当我左右滑动时令人沮丧地消失的重复碎片。这是由于gettitem()调用两次相同的片段由于EventFragment。newInstance(position + 1).

2)保存片段的实例状态,这样我就可以不断地向左和向右滑动,而不会出现空白页。

谢谢你的帮助,@Rod_Algonquin。

最新更新