2

I have a custom listview, which is re-arrangeable(drag & drop) and obviously scrollable. I need to add a custom header with this listview which should scroll with the list.

I have tried "listView.addHeaderView(header)" with a custom header layout. But header becomes messed up during list re-arrangement. So I don't want to use addHeaderView(...).

I have also tried to put my activity layout which is a LinearLayout, inside a ScrollView and expand the list as suggested here - How can I put a ListView into a ScrollView without it collapsing?. But, scrolling seems slow and ScrollView is creating issue in list item re-arrangement.

So is there any other way to accomplish this effect? I would prefer some solution in layout.

I am fairly inexperienced in Android UI. Thanks in advance.

Community
  • 1
  • 1
mpb
  • 23
  • 1
  • 5

3 Answers3

10

You can add a Header View to your list, using the following code,

View header = getLayoutInflater().inflate(R.layout.header, null); 
ListView listView = getListView();  
listView.addHeaderView(header);  
setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_single_choice,android.R.id.text1, names)); 

But for this, you need to define layour for the Header View, check this Link Android ListActivity with a header or footer

Edit- As per comment, I would like to mention that, It is not a good practice to use a ListView inside a ScrollView. The best possible solution, I found so far is to add HeaderView to your ListView. It would be better if the OP could mention, what problems he is facing with the HeaderView

If you want a scrollable header, you should definitely go with the HeaderView. But if you want to have a static header above the ListView, you could use a TextView as a Heading of the ListView.

Sahil Mahajan Mj
  • 11,033
  • 8
  • 53
  • 100
  • He mentioned that he doesn't want addHeaderView – Arash GM Dec 31 '12 at 07:19
  • @Arash: I have myself worked on List with Header view and I haven't found any problem in it. But I would love to hear what problem did OP found in it. – Sahil Mahajan Mj Dec 31 '12 at 07:26
  • me either!:) i've also added working headerview to listview,my only problem with headerview was that it was scrolling with the list content and it doesn't freeze like what we expect with static header. – Arash GM Dec 31 '12 at 07:30
  • @Arash : I think you would love to have a look at my edited stuff. BTW, if you want a static header, why dont you use a TextView as the Header of the List. – Sahil Mahajan Mj Dec 31 '12 at 07:36
  • yeah i've mentioned it in my answer too and your edited answer is right too – Arash GM Dec 31 '12 at 07:40
  • Hi Sahil and Arash. Thanks for your comments. The problem that I am getting with my custom headerview (that contains a button and text) is that when I expand and unexpand the ListView (drag and drop type), the header view display gets messed up, as header is also part of window size computation. I have tried to fix that with some workaround to exclude header from dragging list items, but its not working. So looking for some other alternatives. – mpb Dec 31 '12 at 08:17
0

you can add static header in layout I mean the layout where you have scrollview above that add a linear layout for static header go through this question it will help you

Community
  • 1
  • 1
Sourabh Saldi
  • 3,567
  • 6
  • 34
  • 57
0

If you don't want that your list having a horizontal scrollview so you can create a Xml layout with horizontal linear layout(same as your list Layout) then include it at the top of your listview layout but if you want it horizontally srcollable so you have two choice : one is what you mention as addheader view and second is a Custom ScrollView which you can find a good solution here

Community
  • 1
  • 1
Arash GM
  • 10,316
  • 6
  • 58
  • 76