使用StickyGridHeaders

时间:2013-05-28 21:52:58

标签: android listview android-gridview

StickyGridHeaders,但它不能正常工作,或者至少按照我的意愿工作。

所以我得到了这个列表里面的其他列表,并且我创建了一个标题列表(我使用原始列表)和一个完整的项目列表(所有子项目),并调用适配器如下:

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

然后我有我的适配器:

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

我得到的结果只是一个普通的列表视图,所有项目都在另一个下面排序,没有gridview也没有标题。 我做错了什么?

1 个答案:

答案 0 :(得分:10)

终于找到了怎么做。

主要问题是它不能成为一个listfragment,因为,listfragment有一个listview。它必须是一个片段然后你的视图需要有一个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;
        }
    }