Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android ListView custom header

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.

like image 864
mpb Avatar asked Dec 08 '25 22:12

mpb


1 Answers

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.

like image 83
Sahil Mahajan Mj Avatar answered Dec 11 '25 14:12

Sahil Mahajan Mj



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!