lopesdasilva lopesdasilva -4 years ago 46
Android Question

Using StickyGridHeaders

StickyGridHeaders, but its not working as it should, or at least as I want.

So I got this list with other lists inside, and with that i create a header list(I use the original list) and a full items list (all the subitems), and the call the adapter like so:

List<CalendarDate.CalendarTvShowEpisode> lista=new LinkedList<CalendarDate.CalendarTvShowEpisode>();
for(CalendarDate l:response){
lista.addAll(l.episodes);
}
setListAdapter(new WeekCalendarAdapter(getActivity(), weekCalendar,lista));


Then i have my Adapter:

public class WeekCalendarAdapter extends BaseAdapter implements StickyGridHeadersBaseAdapter {

private final List<CalendarDate> mListHeaders;
private final List<CalendarDate.CalendarTvShowEpisode> lista;
private LayoutInflater inflater;

public WeekCalendarAdapter(Context context, List<CalendarDate> mList, List<CalendarDate.CalendarTvShowEpisode> lista) {
this.mListHeaders = mList;
this.lista=lista;
inflater = LayoutInflater.from(context);
}

@Override
public int getCount() {
return lista.size();
}

@Override
public Object getItem(int i) {
return lista.get(i);
}

@Override
public long getItemId(int i) {
return lista.get(i).episode.firstAired.getDay();
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = inflater.inflate(R.layout.calendarweek_item_episode, parent, false);
}

AQuery aq= new AQuery(convertView);
aq.id(R.id.imageViewCalendarEpisodeBackdrop).image(lista.get(position).episode.images.screen);
aq.id(R.id.textViewCalendartext).text(lista.get(position).episode.firstAired.getDay()+"");


return convertView;
}


@Override
public int getCountForHeader(int header) {
return mListHeaders.get(header).episodes.size();
}

@Override
public int getNumHeaders() {
return mListHeaders.size();
}

@Override
public View getHeaderView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = inflater.inflate(R.layout.calendarweek_item, parent, false);
}

AQuery aq = new AQuery(convertView);
SimpleDateFormat format = new SimpleDateFormat("E MM dd, yyyy");
aq.id(R.id.textViewCalendarWeekDay).text(format.format(mListHeaders.get(position).date) + "");

return convertView;
}


The result I get is just a normal listview with all items ordered on below another, no gridview nor header.
What I'm a doing wrong?

Answer Source

Finally found out how to do it.

And the main problem is that it cant be a listfragment because, well listfragment has a listview. It has to be a Fragment and then your view need to have a com.tonicartos.widget.stickygridheaders.StickyGridHeadersGridView

So the code:

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.calendarweek_fragment, container, false);


        StickyGridHeadersGridView l = (StickyGridHeadersGridView) rootView.findViewById(R.id.listViewCalendar);
        mAdapter = new WeekCalendarAdapter(getActivity(), weekCalendar, lista);
        l.setAdapter(mAdapter);
return rootView;
}

And the View:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="match_parent">

    <com.tonicartos.widget.stickygridheaders.StickyGridHeadersGridView
        android:id="@+id/listViewCalendar"

        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:columnWidth="160dp"
        android:numColumns="auto_fit"
        android:stretchMode="columnWidth"
        android:gravity="left"
        android:background="@color/calendar_background"/>
</LinearLayout>

My adapter

public class WeekCalendarAdapter extends BaseAdapter implements StickyGridHeadersBaseAdapter {

        private final List<CalendarDate> mListHeaders;
        private final List<CalendarDate.CalendarTvShowEpisode> lista;
        private LayoutInflater inflater;

        public WeekCalendarAdapter(Context context, List<CalendarDate> mList, List<CalendarDate.CalendarTvShowEpisode> lista) {
            this.mListHeaders = mList;
            this.lista = lista;
            inflater = LayoutInflater.from(context);
        }

        @Override
        public int getCount() {
            return lista.size();
        }

        @Override
        public CalendarDate.CalendarTvShowEpisode getItem(int i) {
            return lista.get(i);
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if (convertView == null) {
                convertView = inflater.inflate(R.layout.calendarweek_item_episode, parent, false);
            }

           //EDIT YOUR GRID ITEM HERE
            return convertView;
        }

        @Override
        public int getCountForHeader(int i) {
            return mListHeaders.get(i).episodes.size();
        }

        @Override
        public int getNumHeaders() {
            return mListHeaders.size();
        }

        @Override
        public View getHeaderView(int position, View convertView, ViewGroup parent) {
            if (convertView == null) {
                convertView = inflater.inflate(R.layout.calendarweek_item, parent, false);
            }
           //EDIT YOUR VIEW HEADER HERE


            return convertView;
        }
    }
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download