使用Fragment Pager Adapter时,android-getItem()方法被调用两次

前端之家收集整理的这篇文章主要介绍了使用Fragment Pager Adapter时,android-getItem()方法被调用两次前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在使用片段寻呼机适配器实例化我的片段类.我能够这样做但我的问题是我的getItem()方法调用两次,这进一步产生了问题.你解释我为什么会发生这种情况.
  1. package com.creatiosoft.RSSFeed.adaptor;
  2.  
  3. import android.content.Context;
  4. import android.support.v4.app.Fragment;
  5. import android.support.v4.app.FragmentManager;
  6. import android.support.v4.app.FragmentPagerAdapter;
  7. import android.util.Log;
  8.  
  9. import com.creatiosoft.RSSFeed.utils.RSSItem;
  10. import com.viewpagerindicator.IconPagerAdapter;
  11.  
  12. public class NewsFeedsAdapter extends FragmentPagerAdapter implements
  13. IconPagerAdapter {
  14.  
  15. int[] icon = null;
  16. String[] content = null;
  17. String[] URLs = null;
  18. Context cont;
  19.  
  20. public NewsFeedsAdapter(FragmentManager fm,Context context) {
  21. super(fm);
  22. Log.i("jk","constructor");
  23. this.cont = context;
  24. RSSItem newsFeedAppliaction = (RSSItem) cont;
  25. /*
  26. * Retrieving the values of the Icons and contents from the application
  27. * class in utils package
  28. */
  29. icon = newsFeedAppliaction.getICONS();
  30. content = newsFeedAppliaction.getCONTENT();
  31. URLs = newsFeedAppliaction.getURL();
  32.  
  33. }
  34.  
  35. /** instantiate a new fragment class */
  36. @Override
  37. public Fragment getItem(int position) {
  38. Log.i("yt","hello" + position);
  39. return TestFragment.newInstance(position % content.length,cont);
  40. }
  41.  
  42. @Override
  43. public CharSequence getPageTitle(int position) {
  44. return content[position % content.length].toUpperCase();
  45. }
  46.  
  47. public int getIconResId(int index) {
  48. return icon[index];
  49. }
  50.  
  51. /** return the no of views on the basis of array items */
  52. @Override
  53. public int getCount() {
  54.  
  55. Log.i("hi","length" + content.length);
  56. return content.length;
  57. }
  58. }

我用这段代码调用Adapter:

  1. NewsFeedsAdapter adapter = new NewsFeedsAdapter(
  2. getSupportFragmentManager(),getApplicationContext());
  3. /**
  4. * get the id of the view pager declared in the xml resource and set the
  5. * adaptor on the view pager
  6. */
  7. ViewPager pager = (ViewPager) findViewById(R.id.pager);
  8. pager.setAdapter(adapter);
  9. //pager.setCurrentItem(0);
  10.  
  11. /**
  12. * Tab page indicator class is used to indicate the tabs and is accessed
  13. * from the library class
  14. */
  15. TabPageIndicator indicator = (TabPageIndicator) findViewById(R.id.indicator);
  16. indicator.setViewPager(pager);

解决方法

请注意,寻呼机至少保持一页.这意味着当创建寻呼机时,他创建至少两个页面 – 他显示页面和下一个页面,以便允许“分页”.

猜你在找的Android相关文章