title: ViewPager taking time to load Fragments containing ListViews
body: <p><strong>Scope:</strong></p>
  
  <p>I am using a ViewPager inside a fragment(Schedule Fragment) which in turn shows 5 instances of a same fragment(ScheduleListSubFragment) containing different data in respective ListViews. And I am using FragmentPagerAdaper (ScheduleFragmentAdapter).</p>
  
  <p><strong>Code:</strong></p>
  
  <p>The code is as follows:</p>
  
  <p><em>Schedule Fragment:</em></p>
  
  <pre><code>    public class ScheduleFragment extends Fragment{
  ScheduleFragmentAdapter mAdapter;
  ViewPager mPager;
  PageIndicator mIndicator;
  TitlePageIndicator indicator;
  
  @Override
  public void onResume() {
      Log.d(" onResume()------------------ScheduleFrament","oijlkjlijijoij");
      super.onResume();
  }
  
  public void setNstartAnimation(final View v, int id, int duration)
  {
      Animation anim = AnimationUtils.loadAnimation(getActivity(), id);
      anim.setAnimationListener(new Animation.AnimationListener() {
  
          @Override
          public void onAnimationStart(Animation animation) {
              v.setVisibility(View.VISIBLE);
          }
  
          @Override
          public void onAnimationRepeat(Animation animation) {
              // TODO Auto-generated method stub
          }
  
          @Override
          public void onAnimationEnd(Animation animation) {
              // TODO Auto-generated method stub
  
          }
      });
  
      anim.setDuration(duration);
      v.startAnimation(anim);
  }
  
  
  @Override
  public void onViewStateRestored(Bundle savedInstanceState) {
      Log.d(" onViewState Restored()------------------ScheduleFrament","oijlkjlijijoij");
  
      super.onViewStateRestored(savedInstanceState);
  }
  
  @Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
  
      Log.d(" onCreate()------------------ScheduleFrament","oijlkjlijijoij");
  
  }
  
  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
      Bundle savedInstanceState) {
      View v = inflater.inflate(R.layout.scheduled_list, null);
  
      MainActivity.setTouchModewithCurrentPage();
  
      Log.d(" onCreateView()------------------ScheduleFrament","oijlkjlijijoij");
  
  
      mAdapter = new ScheduleFragmentAdapter(getChildFragmentManager());
      prepareSlidingPager(v);
  
      final TextView tv = (TextView) v.findViewById(R.id.category_header);
      tv.setTypeface(Utils.getHeroFace(getActivity().getAssets()));
      setNstartAnimation(tv, R.anim.slide_in_top, 1000);
  
      return v;
  }
  
  private void prepareSlidingPager(View v)
  {
  
          mPager = (ViewPager)v.findViewById(R.id.pager);
  
          mPager.setOffscreenPageLimit(4);
  
          mPager.setAdapter(mAdapter);
          mAdapter.notifyDataSetChanged();
  
  
          mIndicator = (TitlePageIndicator)v.findViewById(R.id.indicator);
          indicator = (TitlePageIndicator) mIndicator;
          mIndicator.setViewPager(mPager);
          mIndicator.setCurrentItem(getArguments().getInt("page"));
  
          mIndicator.setOnPageChangeListener(new pageListener());
  
  
          final float density = getResources().getDisplayMetrics().density;
          indicator.setBackgroundColor(0x18AF0000);
          indicator.setFooterColor(0xFFAA2222);
  
          //indicator.setFooterColor(0xFFAA33);
  
          indicator.setFooterLineHeight(2 * density); //1dp
          indicator.setFooterIndicatorHeight(3 * density); //3dp
          indicator.setFooterIndicatorStyle(IndicatorStyle.Underline);
  
          indicator.setTextColor(0xAA000000);
          indicator.setSelectedColor(0xFF000000);
          indicator.setSelectedBold(true);
  }
  
  
  public static ScheduleFragment newInstance(int currentPage)
  {
      ScheduleFragment frag = new ScheduleFragment();
      Bundle bundle = new Bundle();
      bundle.putInt("page", currentPage);
      frag.setArguments(bundle);
      return frag;
  }
  
  
  @Override
  public void onDetach() {
      super.onDetach();
  
      try {
          Field childFragmentManager = Fragment.class.getDeclaredField("mChildFragmentManager");
          childFragmentManager.setAccessible(true);
          childFragmentManager.set(this, null);
  
      } catch (NoSuchFieldException e) {
          throw new RuntimeException(e);
      } catch (IllegalAccessException e) {
          throw new RuntimeException(e);
      }
  }
  </code></pre>
  
  <p>}</p>
  
  <p><em>ScheduleListSubFragment</em></p>
  
  <pre><code>    public class ScheduleListSubFragment extends Fragment implements OnItemClickListener, UpdateableFragment{
  
  long startDate;
  long endDate;
  
  
  private ListView eventlist;
  private listadapter adapter;
  private List events;
  private MyDB db;
  
  
  
  @Override
  public void onResume() {
  
      Utils.log("on resume......subfragment");
  
  
  
      super.onResume();
  }
  
  @Override
  public void onSaveInstanceState(Bundle outState) {
      Log.v(" OnsaveInstanceState()------------------","oijlkjlijijoij");
      super.onSaveInstanceState(outState);
  }
  
  @Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
  
      db=new MyDB(this.getActivity());
  
      db.open();
      events=db.getScheduleEntries_bytime(getArguments().getLong("start_time"), getArguments().getLong("end_time"));
      db.close();
      if(events!=null)
      {
      adapter=new listadapter(getActivity(), android.R.layout.simple_expandable_list_item_1, events);
  
      }
      Log.d(" onCreate()------------------sublist schedule","oijlkjlijijoij");
  
  }
  
  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
          Bundle savedInstanceState) {
      return inflater.inflate(R.layout.eventslist_subfragment_layout, null);
  
  }
  
  @Override
  public void onViewCreated(View view, Bundle savedInstanceState) {
      // TODO Auto-generated method stub
      super.onViewCreated(view, savedInstanceState);
  
      Log.d(" onViewCreated()------------------sublist schedule","asdfasdf");// Toast.LENGTH_SHORT).show();
  
      eventlist=(ListView)view.findViewById(R.id.Event_list);
      eventlist.setOnItemClickListener(this);
      //new updateViewTask().execute(new Object());
      fetcheventdata();
  
  }
  
  private class updateViewTask extends AsyncTask&lt;Object, Object, Object&gt;
  {
  
      @Override
      protected Object doInBackground(Object... params) {
          //try{Thread.sleep(2000);}catch(Exception e){e.printStackTrace();}
          publishProgress(new Object());
          // TODO Auto-generated method stub
          return null;
      }
  
      @Override
      protected void onProgressUpdate(Object... values) {
          // TODO Auto-generated method stub
          super.onProgressUpdate(values);
  
      }
  
  
  }
  
  public static ScheduleListSubFragment newInstance(long starttime, long endtime)
  {
      ScheduleListSubFragment frag = new ScheduleListSubFragment();
      Bundle bundle = new Bundle();
  
      bundle.putLong("start_time", starttime);
      bundle.putLong("end_time", endtime);
      frag.setArguments(bundle);
  
      return frag;
  }
  
  
  
   private void fetcheventdata()
      {
  
  
          if(events!=null)
          {
          eventlist.setAdapter(adapter);
  
          adapter.notifyDataSetChanged();
          Utils.log(".list size: subframgment:"+events.size()+"");//, duration)
          }
          else
              Toast.makeText(getActivity(), "No Events Yet", Toast.LENGTH_LONG);
  
          //Toast.makeText(getActivity(), events.size(), Toast.LENGTH_LONG).show();
  
  
  
      }
  
  
   private class listadapter extends ArrayAdapter&lt;Event&gt;
      {
          private List items;
  
          public listadapter(Context context, int textViewResourceId,
                  List objects){
              super(context, textViewResourceId, objects);
              items=new ArrayList();
              items=objects;
  
          }
  
          @Override
          public View getView(int position, View convertView, ViewGroup parent) {
  
              View v=convertView;
  
              ScheduleEntryViewHolder viewHolder=null;
              if(v==null)
              {
                  LayoutInflater vi=(LayoutInflater)getActivity().getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
                  v=vi.inflate(R.layout.schedule_entry, null);
  
                  viewHolder = new ScheduleEntryViewHolder();
  
                  viewHolder.nameView=(TextView)v.findViewById(R.id.Name);
                  viewHolder.nameView.setTypeface(Utils.getHeroFace(getActivity().getAssets()));
                  viewHolder.nameView.setTextColor(getResources().getColor(R.color.black));
  
  
                  viewHolder.roundView=(TextView)v.findViewById(R.id.Round);
                  viewHolder.roundView.setTypeface(Utils.getHeroFace(getActivity().getAssets()));
  
                  Utils.log("entry "+position+"th in listview");
  
                  viewHolder.venueView=(TextView)v.findViewById(R.id.Venue);
                  viewHolder.venueView.setTypeface(Utils.getHeroFace(getActivity().getAssets()));
                  viewHolder.venueView.setTextColor(getResources().getColor(R.color.darkgrey));
  
                  viewHolder.timeView=(TextView)v.findViewById(R.id.time);
                  viewHolder.timeView.setTextColor(getResources().getColor(R.color.black));
  
  
                  viewHolder.categoryView=(TextView)v.findViewById(R.id.cat_name_view);
                  viewHolder.favView = (ImageView) v.findViewById(R.id.favourite_view);
  
  
  
                  v.setTag(viewHolder);
              }
              else
                  viewHolder = (ScheduleEntryViewHolder) v.getTag();
  
              ScheduleEntry scheduleentry=(ScheduleEntry)items.get(position);
              if(scheduleentry!=null)
              {
  
                  viewHolder.nameView.setText(scheduleentry.getEventName());
                  viewHolder.venueView.setText(scheduleentry.getVenue());
                  viewHolder.roundView.setText("("+scheduleentry.getRoundName()+")");
                  viewHolder.timeView.setText(Html.fromHtml("&lt;b&gt;"+scheduleentry.getStartTime12()+"-"+scheduleentry.getEndTime12()+"&lt;/b&gt;&lt;br&gt;&lt;/br&gt;"+scheduleentry.getStartDate()));
  
                  if(scheduleentry.getFav_status().equals("1"))
                      viewHolder.favView.setVisibility(View.VISIBLE);
                  else
                      viewHolder.favView.setVisibility(View.INVISIBLE);
  
                  viewHolder.categoryView.setText(scheduleentry.getCategory());
                  viewHolder.categoryView.setBackgroundColor(Utils.getColorforCategory(scheduleentry.getCategory().toUpperCase(), getActivity()));
              }
              return v;
          }
  
      }
  
  
   @Override
      public void onItemClick(AdapterView&lt;?&gt; arg0, View arg1, int pos, long arg3) {
          eventnotifierapp app=(eventnotifierapp)getActivity().getApplication();
  
          db.open();
          app.crntEvent=db.getEvent( ((ScheduleEntry)events.get(pos)).getEventName()  );
  
          db.close();
          //startActivity(new Intent(this.getActivity(), EventDescription.class));
  
          EventDescriptionFragment frag = EventDescriptionFragment.NewInstance();
          ((MainActivity)getActivity()).OpenNextFragment(frag);
  
      }
  
  
  
  @Override
  public void update(long start, long end) {
      Toast.makeText(getActivity(), "finally.... ------------------------------called", Toast.LENGTH_SHORT).show();
      fetcheventdata();
  
  }
  </code></pre>
  
  <p>}</p>
  
  <p><em>ScheduleFragmentAdapter</em></p>
  
  <pre><code>  public class ScheduleFragmentAdapter extends FragmentPagerAdapter{
      String cat_name;
      protected static final String[] CONTENT = new String[] { "All", "Thursday, Feb 27", 
          "Friday, Feb 28", "Saturday, Mar 1", "Sunday, Mar 2", };
  
      private List&lt;Fragment&gt; fragments;
      private static long startdates[]={day1start, day2start, day3start, day4start};
      private static long enddates[]={day1end, day2end, day3end, day4end};
  
      private int mCount = CONTENT.length;
      private int mindex=0;
  
  
      public void setmIndex(int i)
      {
          mindex=i;
      }
  
      public ScheduleFragmentAdapter(FragmentManager fm) {
          super(fm);
  
          fragments = new ArrayList&lt;Fragment&gt;();
          fragments.add(ScheduleListSubFragment.newInstance(day1start, day4end));
          fragments.add(ScheduleListSubFragment.newInstance(day1start, day1end));
          fragments.add(ScheduleListSubFragment.newInstance(day2start, day2end));
          fragments.add(ScheduleListSubFragment.newInstance(day3start, day3end));
          fragments.add(ScheduleListSubFragment.newInstance(day4start, day4end));
  
  
      }
  
  
  
  
  
      @Override
      public int getItemPosition(Object item) {
          Log.v("getItemPosition() callled......", "yeah-----------------");
  
          return super.getItemPosition(item);
      }
  
  
  
  
  
      @Override
      public Fragment getItem(int position) {
  
          Log.v("getItem() callled......", "getItem() ...........");
          return fragments.get(position);
  
  
      }
  
      @Override
      public int getCount() {
          return mCount;
      }
  
      @Override
      public CharSequence getPageTitle(int position) {
        return CONTENT[position % CONTENT.length];
      }
  }
  </code></pre>
  
  <p><strong>Problem</strong></p>
  
  <p>So, coming to the problem. Whenever I replace the ScheduleFragment on my Frame in my Activity, it takes a lot of time for this fragment to come up. To add into your knowledge; It nearly hangs the main Thread.</p>
  
  <p>To prevent this from annoying the user, I made an asynctask that shows the progress bar for 3 seconds. But since the main thread also gets stuck for these 3 minutes, the progress bar comes and also stucks(means the circle is not rotating) for 3 minutes and then disappears.</p>
  
  <p>So guys Please help me out how can I make this ViewPager loading to be efficient. By efficient I mean, How can I load the data in 'ScheduleListSubFragment instances' asynchronously so that the main thread doesn't get stuck.</p>
  
  <p><strong>Supplementary knowledge</strong></p>
  
  <ol>
  <li>In my ListView item, I have 6 textViews and 1 ImageView, because of which, perhaps the view generation is taking time.</li>
  </ol>