0

I have a listview in android with several columns, and for each column I want to set a separate title, as shown in this example below (I mean the fields 'Name and Number', 'SMS, 'Phone'):

enter image description here

(The image is just to explain what I want; in my layout there are more columns which are not as self-explaining as in the example).

Is there a good way to do that? Als, is it possible that this title row can be visible at all time, even when I have many many rows and scroll down?

Alex
  • 41,580
  • 88
  • 260
  • 469
  • 4
    why don't you create this using linear layout and just place listview below it – Vivek Mishra Feb 07 '16 at 09:00
  • 1
    Do it using ListViewHeader instead of adding any extra layout – ρяσѕρєя K Feb 07 '16 at 09:03
  • Yes good idea Vivek... – Alex Feb 07 '16 at 09:05
  • Try using RecyclerView. ListView is not recommended anymore to show a list in Android. Here's how you can achieve your goal by setting a header in your list. This is a example of how you add a footer in your RecyclerView. But anyway, adding a footer is almost the same thing. You just need to implement your own logic. http://stackoverflow.com/questions/26448717/android-5-0-add-header-footer-to-a-recyclerview/31154402#31154402 – Reaz Murshed Feb 07 '16 at 09:06
  • Replacing my working list view with something else will take me at LEAST a week to do... – Alex Feb 07 '16 at 09:06

1 Answers1

0

If you want to have a fixed header item in the list that doesn't scroll with the other items, use ListView's addHeaderView to indicate which view(s) should stick to the top.

If you just want the first item to be different than the others, you'll have to code your ListAdapter to have two types of views, one for the header, and another for the rest. This involves

  • Making getViewTypeCount return 2 to say it can make two different kinds of views.
  • Making getItemViewType return 0 or 1 to say which type of view will be generated for the given position in the list.
  • Making getView return the correct view object for the given position in the list.

Presumably you'd want position 0 to be the header view and all other positions to be data.

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441