填充Sticky Listview Android

时间:2015-01-08 05:45:54

标签: android listview android-listview

来自mCalendarModel的数据用于通过适配器填充下面的列表视图。对于与该年度相对应的所有数据,必须只显示一个月的问题。例如:

*Current result: 
 Header -> January 
  Item -> Series St Pierre

 Header -> January 
  Item -> Series St Pierre

 Header -> January 
  Item -> Series St Pierre

 Header -> February
  Item -> Series St Aubin 


Expected Result:

    Header -> January 
    Item -> Series St Pierre
    Item -> Series St Pierre
    Item -> Series St Pierre
    Item -> Series St Pierre
    Item -> Series St Pierre
    Item -> Series St Pierre
    Item -> Series St Pierre
    Item -> Series St Pierre
    Item -> Series St Pierre

    Header -> February
    Item -> Series St Aubin 
    ...*

有没有办法只显示预期的结果

    *mCalendarModel = new ArrayList<CalendarModel>();
    mCalendarModel.add(new CalendarModel("JANVIER", "Dimanche 2/01", "Series St Pierre", "22 km", "$1000", "course_trail","1","1"));
    mCalendarModel.add(new CalendarModel("JANVIER", "Dimanche 2/01", "Series St Pierre", "22 km", "$1000", "course_trail","1","1"));
    mCalendarModel.add(new CalendarModel("JANVIER", "Dimanche 2/01", "Series St Pierre", "22 km", "$1000", "course_trail","1","1"));
    mCalendarModel.add(new CalendarModel("JANVIER", "Dimanche 2/01", "Series St Pierre", "22 km", "$1000", "course_trail","1","1"));
    mCalendarModel.add(new CalendarModel("JANVIER", "Dimanche 2/01", "Series St Pierre", "22 km", "$1000", "course_trail","1","1"));
    mCalendarModel.add(new CalendarModel("JANVIER", "Dimanche 2/01", "Series St Pierre", "22 km", "$1000", "course_trail","1","1"));
    mCalendarModel.add(new CalendarModel("JANVIER", "Dimanche 2/01", "Series St Pierre", "22 km", "$1000", "course_trail","1","1"));
    mCalendarModel.add(new CalendarModel("JANVIER", "Dimanche 2/01", "Series St Pierre", "22 km", "$1000", "course_trail","1","1"));
    mCalendarModel.add(new CalendarModel("JANVIER", "Dimanche 2/01", "Series St Pierre", "22 km", "$1000", "course_trail","1","1"));
    mCalendarModel.add(new CalendarModel("JANVIER", "Dimanche 2/01", "Series St Pierre", "22 km", "$1000", "course_trail","1","1"));
    mCalendarModel.add(new CalendarModel("JANVIER", "Samedi 3/01", "Series St Aubin", "22 km", "$1000", "course_route","1","1"));
    mCalendarModel.add(new CalendarModel("JANVIER", "Samedi 2/01", "Series St Aubin", "22 km", "$1000", "course_route","1","1"));
    mCalendarModel.add(new CalendarModel("JANVIER", "Samedi 2/01", "Series St Aubin", "22 km", "$1000", "course_route","2","1"));
    mCalendarModel.add(new CalendarModel("JANVIER", "Samedi 2/01", "Series St Aubin", "22 km", "$1000", "course_route","1","1"));
    mCalendarModel.add(new CalendarModel("FEVRIER", "Samedi 2/01", "Series St Aubin", "22 km", "$1000", "course_route","2","2"));*






public void populateHeaderList(){

    SectionAdapter sectionAdapter = new SectionAdapter(getActivity(), mCalendarModel,mLayoutInflator){

        private ViewHeaderHolder headHolder;

        @Override
        public int numberOfSections() {
            return mCalendarModel.size();
        }

        @Override
        public int numberOfRows(int section) {
            return 1;
        }

        @Override
        public String getRowItem(int section, int row) {
            return null;
        }

        @Override
        public boolean hasSectionHeaderView(int section) {
            return true;
        }


        @Override
        public int getSectionHeaderViewTypeCount() {
            return 2;
        }

        @Override
        public int getSectionHeaderItemViewType(int section) {
            return section % 2;
        }

        @SuppressLint("InflateParams") 
        @Override
        public View getSectionHeaderView(int section, View convertView, ViewGroup parent) {
            View vi = convertView;

            if (convertView == null) {

                vi = mLayoutInflator.inflate(R.layout.header_row_field, null);

                headHolder = new ViewHeaderHolder();

                headHolder.mTextHeader = (Button) vi.findViewById(R.id.mTextHeader);

                vi.setTag(headHolder);

            } else
                headHolder = (ViewHeaderHolder) vi.getTag();

            headHolder.mTextHeader.setText(mCalendarModel.get(section).month);
            headHolder.mTextHeader.setTextColor(getResources().getColor(R.color.calendrier_vert));

            return vi;

        }

        @Override
        public void onRowItemClick(AdapterView<?> parent, View view, int section, int row, long id) {
            super.onRowItemClick(parent, view, section, row, id);

            Intent intentFicheCourse = new Intent (getActivity(),FicheCourse.class);
            startActivity(intentFicheCourse);
            getActivity().overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_left);

        }

        @SuppressLint("InflateParams") 
        @Override
        public View getRowView(int section, int row, View convertView,
                ViewGroup parent) {
            // TODO Auto-generated method stub
            View vi = convertView;

            if (convertView == null) {

                vi = mLayoutInflator.inflate(R.layout.header_row_item, null);

                holder = new ViewHolder();

                holder.mTextDate = (TextView) vi.findViewById(R.id.mTextDate);
                holder.mTextAddress = (TextView) vi.findViewById(R.id.mTextAddress);
                holder.mTextDistance = (TextView) vi.findViewById(R.id.mTextDistance);
                holder.mTextTarif = (TextView) vi.findViewById(R.id.mTextTarif);
                //holder.mLDivider= (LinearLayout) vi.findViewById(R.id.mLDivider);

                vi.setTag(holder);

            } else
                holder = (ViewHolder) vi.getTag();

            holder.mTextDate.setText(mCalendarModel.get(row).date);
            holder.mTextAddress.setText(mCalendarModel.get(row).title);
            holder.mTextTarif.setText(mCalendarModel.get(row).tarrif);
            holder.mTextDistance.setText(mCalendarModel.get(row).distance);

            return vi;
        }
    };

    mHeaderList.setAdapter(sectionAdapter);

}

1 个答案:

答案 0 :(得分:0)

为什么不尝试扩展listView?

创建2个数组,一个可用于填充父级的数月,第二个用于子级的数组,可用于填充子级。

此外,您可以保持父行始终展开,并且它将完全按照您的要求显示。