Tony
Tony

Reputation: 2748

Add listView header between list

This is my listViewenter image description here

I want to add a February header on top of 2016-02-02 and January in 2016-01-31. Is it possible?

Upvotes: 0

Views: 126

Answers (2)

drew
drew

Reputation: 319

Yes, you can do this by returning a different view in your getView() method in your adapter class. In your master list, that you pass to your adapter, you can add a divider item, a String or however you are holding all this data, I assume a custom class, that you know is meant to show a Month title. You can do a quick check in your getView() method and return a different view that displays the month..

In your getView() method, you can do this...

    @Override
public View getView(final int position, View convertView, ViewGroup parent){
    LayoutInflater mInflator = LayoutInflater.from(getContext());
    View customView = mInflator.inflate(R.layout.times_layout, parent, false);
    Time temp = getItem(position);

    //Check to see if the time is supposed to be a header
    //This is where you check to see if it meant to be a section header
    if(temp.getDate.equals("HEADER")){
        //Header, return section view instead of normal view
        View sectionHeader = mInflator.inflate(R.layout.layout_list_divider, parent, false);
        TextView txt_Section = (TextView) sectionHeader.findViewById(R.id.txt_Header);
        sectionHeader.setClickable(false);
        return sectionHeader;
    }
    //Normal View... do what you would do normally



    return customView;
}

I hope this helps! Let me know.. it worked for me

Upvotes: 1

meda
meda

Reputation: 45490

In android It's called ExpandableListView

You can try this tutorial:

http://www.androidhive.info/2013/07/android-expandable-list-view-tutorial/

It also has a sample to download.

Upvotes: 1

Related Questions