androidviewviewgroup

Swipe / (Fling) with dynamic views


What i want to:

I want to add a swipe or what i learned it's named on android fling, to my app. I have a dynamic number of views, the number is the amount of dates from an ICS file which i parse, i want to make a swipe effekt on all of these views together. But if i have let's say 12 of these each having a ListView with 8 items (max) it would use a lot of memory i guess. So i would like that only the 2 before current selected view and the 2 after to be initialized.

What i have tried:

In my search i stumpled around this stackoverflow question which mentions HorizontalPager. But i dont know to make it work with a number of ListView's and load them dynamically.

I tried a ViewGroup and then add and remove a ListView but it's not working, it display's the ViewGroup but not the ListView

public class HourView extends ViewGroup
{
    private ListView listView;

    /**
     * @param context
     */
    public HourView(Context context)
    {
        super(context);
        init(false);
    }

    /**
     * 
     * @param context
     * @param day the current day
     */
    public HourView(Context context, String day,
            boolean shouldBeVisible)
    {
        super(context);
        this.day = day;

        init(shouldBeVisible);
    }

        private void init(boolean shouldBeVisible)
    {       
               if (shouldBeVisible)
        {   
            listView = new ListView(getContext());

            if (day == null)
            {
                day = Event.dtFormatDay.format(new Date());
            }

                        new GetEvents(day).execute();
                       addView(listView);
                  }
                  else
                  {
                        removeAllViews();
                        listView = null;                  
                  }
    }
}

The GetEvents() is a AsyncTask (a class inside the viewgroup class) that gets some events from a local database, the code is the onPostExecute is as follows

protected void onPostExecute(String errMsg)
{
  if (errMsg != null)
  {
     listView.setAdapter(new SkemaHoursRowAdapter(getContext(), eventItems));
      listView.requestLayout();
      addView(listView, layoutParams);
  }
}

eventItems is an array i parse to my custom rowadapter (which i know works). The view group is displayed but the listView is not.

Any suggestions??


Solution

  • I ended up not using a viewgroup instead i made a loader-listadapter which i display if the correct list has not been updated yet.

    So instead of extending ViewGroup it extends ListView and it's adapter get set at first to the load-adapter and when loaded it sets it to the correct listview.