我以前使用过这段代码来获得两个布局的滑动选项卡视图,它可以正常工作.
public class MainActivity extends FragmentActivity {/** * The {@link androID.support.v4.vIEw.PagerAdapter} that will provIDe * fragments for each of the sections. We use a * {@link androID.support.v4.app.FragmentPagerAdapter} derivative, which * will keep every loaded fragment in memory. If this becomes too memory * intensive, it may be best to switch to a * {@link androID.support.v4.app.FragmentStatePagerAdapter}. */SectionsPagerAdapter mSectionsPagerAdapter;/** * The {@link VIEwPager} that will host the section contents. */VIEwPager mVIEwPager;@OverrIDeprotected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_main); // Create the adapter that will return a fragment for each of the three // primary sections of the app. mSectionsPagerAdapter = new SectionsPagerAdapter( getSupportFragmentManager()); // Set up the VIEwPager with the sections adapter. mVIEwPager = (VIEwPager) findVIEwByID(R.ID.pager); mVIEwPager.setAdapter(mSectionsPagerAdapter);}@OverrIDepublic boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true;}/** * 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 given page. // Return a DummySectionFragment (defined as a static inner class // below) with the page number as its lone argument. Fragment fragment = new DummySectionFragment(); Bundle args = new Bundle(); args.putInt(DummySectionFragment.ARG_OBJECT, position); fragment.setArguments(args); return fragment; } @OverrIDe public int getCount() { // Show 2 total pages. return 2; } @OverrIDe public CharSequence getPageTitle(int position) { Locale l = Locale.getDefault(); switch (position) { case 0: return getString(R.string.Title_section1).toupperCase(l); case 1: return getString(R.string.Title_section2).toupperCase(l); } return null; }}/** * A dummy fragment representing a section of the app, but that simply * displays dummy text. */public static class DummySectionFragment extends Fragment { /** * The fragment argument representing the section number for this * fragment. */ public static final String ARG_SECTION_NUMBER = "section_number"; public static final String ARG_OBJECT = "object"; public DummySectionFragment() { } @OverrIDe public VIEw onCreateVIEw(LayoutInflater inflater, VIEwGroup container, Bundle savedInstanceState) { Bundle args = getArguments(); int position = args.getInt(ARG_OBJECT); int tabLayout = 0; switch (position) { case 0: tabLayout = R.layout.tab_new; break; case 1: tabLayout = R.layout.tab_summary; break; } VIEw rootVIEw = inflater.inflate(tabLayout, container, false); return rootVIEw; }}public boolean onoptionsItemSelected(MenuItem item) { switch (item.getItemID()) { case R.ID.action_settings: Intent i = new Intent(this, SettingsActivity.class); startActivity(i); } return true;}
}
为此,我只创建了2个xml文件,并在标签正确定位时初始化它们;但是,现在我想要2个选项卡,但第二个必须是列表.我不知道该怎么做.我尝试扩展ListFragment,但它崩溃了找不到的资源.我已经被困了很长时间了,有人请帮忙吗?我搜索了很多并查看了所有示例,但没有一个显示如何在选项卡中创建列表以及如何调用它.
这就是我现在所拥有的
主要活动
public class MainActivity extends FragmentActivity {SimpleListFragment x = new SimpleListFragment();/** * The {@link androID.support.v4.vIEw.PagerAdapter} that will provIDe * fragments for each of the sections. We use a * {@link androID.support.v4.app.FragmentPagerAdapter} derivative, which * will keep every loaded fragment in memory. If this becomes too memory * intensive, it may be best to switch to a * {@link androID.support.v4.app.FragmentStatePagerAdapter}. */SectionsPagerAdapter mSectionsPagerAdapter;/** * The {@link VIEwPager} that will host the section contents. */VIEwPager mVIEwPager;@OverrIDeprotected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_main); // Create the adapter that will return a fragment for each of the three // primary sections of the app. mSectionsPagerAdapter = new SectionsPagerAdapter( getSupportFragmentManager()); // Set up the VIEwPager with the sections adapter. mVIEwPager = (VIEwPager) findVIEwByID(R.ID.pager); mVIEwPager.setAdapter(mSectionsPagerAdapter);}@OverrIDepublic boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true;}/** * 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 given page. // Return a DummySectionFragment (defined as a static inner class // below) with the page number as its lone argument. Fragment fragment = new DummySectionFragment(); Bundle args = new Bundle(); args.putInt(DummySectionFragment.ARG_OBJECT, position + 1); fragment.setArguments(args); return fragment; } @OverrIDe public int getCount() { // Show 3 total pages. return 2; } @OverrIDe public CharSequence getPageTitle(int position) { Locale l = Locale.getDefault(); switch (position) { case 0: return getString(R.string.Title_section1).toupperCase(l); case 1: return getString(R.string.Title_section2).toupperCase(l); } return null; }}/** * A dummy fragment representing a section of the app, but that simply * displays dummy text. */public static class DummySectionFragment extends Fragment { /** * The fragment argument representing the section number for this * fragment. */ public static final String ARG_SECTION_NUMBER = "section_number"; public static final String ARG_OBJECT = "object"; public DummySectionFragment() { } @OverrIDe public VIEw onCreateVIEw(LayoutInflater inflater, VIEwGroup container, Bundle savedInstanceState) { Bundle args = getArguments(); int position = args.getInt(ARG_OBJECT); int tabLayout = 0; switch (position) { case 0: tabLayout = R.layout.tab_home; break; case 1: SimpleListFragment simpleListFragment = new SimpleListFragment(); break; } VIEw rootVIEw = inflater.inflate(tabLayout, container, false); return rootVIEw; }}
}
解决方法:
ATM您正在DummySectionFragment中创建ListFragment.那是不对的.
这是一种做法.不是最好的,但有一个很好理解.
在主活动中创建片段并将它们传递给适配器. DummyListFragment是从ListFragment派生的新类.这里重要的是它使用的XML布局包含一个具有ID @androID:ID / List的ListVIEw. (因此片段能够理解它必须/可以使用哪个列表视图.
public class MainActivity extends FragmentActivity {SectionsPagerAdapter mSectionsPagerAdapter;VIEwPager mVIEwPager;List<Fragment> mFragments;@OverrIDe protected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_main); mFragments = new ArrayList<Fragment>(); mFragments.add(new DummySectionFragment(); mFragments.add(new DummyListFragment(); mSectionsPagerAdapter = new SectionsPagerAdapter( getSupportFragmentManager(), mFragments);
…
public class SectionsPagerAdapter extends FragmentPagerAdapter { List<Fragment> fragmentList; public SectionsPagerAdapter(FragmentManager fm, List<Fragment> fragments) { super(fm); fragmentList = fragments; }@OverrIDepublic int getCount() { return fragmentList.size();} @OverrIDe public Fragment getItem(int position) { Fragment fragment = fragmentList.get(position); return fragment; }
. .
public static class DummyListFragment extends ListFragment {public DummyListFragment() {}@OverrIDepublic VIEw onCreateVIEw(LayoutInflater inflater, VIEwGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.my_List_fragment, container, false);}
总结 以上是内存溢出为你收集整理的如何在Android轻扫选项卡中使用ListFragment?全部内容,希望文章能够帮你解决如何在Android轻扫选项卡中使用ListFragment?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)