Skip to content
This repository has been archived by the owner on Jan 10, 2024. It is now read-only.

SimpleStickyListHeadersAdapter

Kordian Bruck edited this page Aug 9, 2017 · 1 revision

Introduction

StickyListHeaders is a library to create ListView with headers. SimpleStickyListHeadersAdapter is created to simplify the usage of this library, which requires the user to implement StickyListHeadersAdapter. This abstract class implements some methods required, and the only necessary method for the user to implement is getView(int position, View convertView, ViewGroup parent).

Usage

Model

First, you need to create a model class which implements SimpleStickyListItem, an interface provided by this class to callback.

There are 2 methods:

  • String getHeadName() returns the header name of this item for the adapter to display.
  • String getHeaderId() returns a String used to generate header ID.
@Override
public String getHeadName() {
    return getSemester_name();
}

@Override
public String getHeaderId() {
    return getSemester_id();
}

Adapter

Then you can create an adapter extends SimpleStickyListHeadersAdapter<T>. Most of the methods required by StickyListHeadersAdapter are implemented, except getView(int position, View convertView, ViewGroup parent). This method returns the view for a single item in the list. Usually a viewHolder class is created to handle this.

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    final ViewHolder holder;
    View view = convertView;
    if(view == null){
        view = getInflater().inflate(R.layout.activity_barrier_free_more_info_listview, parent, false);

        // Crate UI element
        holder = new ViewHolder();
        holder.title = (TextView) view.findViewById(R.id.barrierfreeMoreInfoTitle);
        view.setTag(holder);
    } else {
        holder = (ViewHolder) view.getTag();
    }

    // set title
    final BarrierfreeMoreInfo info = getInfoList().get(position);
    if (info != null){
        holder.title.setText(info.getTitle());
    }

    return view;
}

// the layout of the list
static class ViewHolder {
    TextView title;
}

Generate Header Title

Sometimes you may want to use String resource to change or modify the content of the header title, which is difficult to do so in a model class. Such a modification can be done by override genenrateHeaderName(T item) method.

It is called when generating the header view and originally returns item.getHeadName() itself. So overriding it will modify the title for every item.

@Override
String genenrateHeaderName(LecturesSearchRow item) {
    String headerText = super.genenrateHeaderName(item);
    headerText = headerText.replaceAll("Sommersemester", this.context.getString(R.string.semester_summer));
    headerText = headerText.replaceAll("Wintersemester", this.context.getString(R.string.semester_winter));
    return headerText;
}

In this case, the german title will be modified according to the user's language environment.

Clone this wiki locally