4

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?

4

1 回答 1

10

终于知道怎么做了。

主要问题是它不能是一个列表片段,因为列表片段有一个列表视图。它必须是一个片段,然后您的视图需要有一个 com.tonicartos.widget.stickygridheaders.StickyGridHeadersGridView

所以代码:

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;
}

和视图:

<?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>

我的适配器

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;
        }
    }
于 2013-05-30T14:11:31.537 回答