android–ViewPager片段 – 片段未显示

android–ViewPager片段 – 片段未显示,第1张

概述我试图使用viewpager来显示几个片段.目前我只有一个片段,但它似乎永远不会被实例化.我尝试使用pager.setCurrentItem(0),但永远不会调用适配器中的getItem方法.有谁知道为什么我的片段没有加载到下面的代码?主要活动public class MainActivity extends SherlockFragmentActivity

我试图使用vIEwpager来显示几个片段.目前我只有一个片段,但它似乎永远不会被实例化.我尝试使用pager.setCurrentItem(0),但永远不会调用适配器中的getItem方法.有谁知道为什么我的片段没有加载到下面的代码?

主要活动

public class MainActivity extends SherlockFragmentActivity {MyPagerAdapter adapter;@OverrIDeprotected voID onCreate(Bundle savedInstanceState) {    super.onCreate( savedInstanceState );    setContentVIEw( R.layout.activity_main );    // Create the FragmentPager Adapter and assign it to the vIEwpager    adapter = new MyPagerAdapter( getSupportFragmentManager(),this );    VIEwPager pager = ( VIEwPager ) findVIEwByID( R.ID.pager );    pager.setAdapter( adapter );    pager.setCurrentItem( 0 );    // Setup the tab page indicator    TabPageIndicator indicator = ( TabPageIndicator ) findVIEwByID( R.ID.indicator );    indicator.setVIEwPager( pager );}class MyPagerAdapter extends FragmentStatePagerAdapter {    private SparseArray

MainActivity布局

ScheduleListFragment

public class ScheduleListFragment extends SherlockListFragment implements VIEwPagerPage {private ScheduleAdapter adapter;private final String Title = "Schedule";public static ScheduleListFragment newInstance() {    ScheduleListFragment newFrag = new ScheduleListFragment();    Bundle args = new Bundle();    newFrag.setArguments( args );    return newFrag;}public VIEw onCreateVIEw(LayoutInflater inflater,VIEwGroup container,Bundle savedInstanceState) {    return inflater.inflate( R.layout.fragment_schedule_List,null );}@OverrIDepublic voID onCreateOptionsMenu(Menu menu,MenuInflater inflater) {    inflater.inflate( R.menu.fragment_schedule_List,menu );}// Container Activity must implement this interfacepublic interface OnAddItemClickedListener {    public voID onAddItemClicked();}@OverrIDepublic boolean onoptionsItemSelected(MenuItem item) {    // Handle item selection    switch ( item.getItemID() ) {    case R.ID.add_item:        Intent myIntent = new Intent( getActivity(),AddScheduleItemActivity.class );        startActivityForResult( myIntent,1 );        return true;    default:        return super.onoptionsItemSelected( item );    }}public voID onActivityCreated(Bundle savedInstanceState) {    super.onActivityCreated( savedInstanceState );    setHasOptionsMenu( true );    adapter = new ScheduleAdapter( getActivity() );    setlistadapter( adapter );}private class SampleItem {    public String tag;    public int iconRes;    public SampleItem(String tag,int iconRes) {        this.tag = tag;        this.iconRes = iconRes;    }}public class ScheduleAdapter extends ArrayAdapter

片段布局

谢谢,
弥敦道

现有解决方案

更改了适配器构造函数以初始化第一个片段并将其添加到内部映射.有没有人看到这样做有什么问题,或者改进上面发布的任何代码?谢谢!

public MyPagerAdapter(FragmentManager fm,Activity context) {        super( fm );        fragmentPageMap = new SparseArray
最佳答案getCount()在MyPagerAdapter中返回0,因此不会创建片段,如果要实例化N片段,它应返回N.

fragmentPageMap是在构造函数中创建的,当调用getCount()并在getItem(index)之前调用getCount()时为空,这就是getCount()返回0的原因

在这种情况下,您不需要切换,因为您每次都要实例化相同的Fragment.但是,如果您在实例化不同的碎片,那么您将需要它.

@OverrIDepublic Fragment getItem(int index) {  switch (index) {    case 0:      return new Fragment1();    case 1:      return new Fragment2();  }  return null;}
总结

以上是内存溢出为你收集整理的android – ViewPager片段 – 片段未显示全部内容,希望文章能够帮你解决android – ViewPager片段 – 片段未显示所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://www.outofmemory.cn/web/1140187.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-31
下一篇 2022-05-31

发表评论

登录后才能评论

评论列表(0条)