I have a ListFragment
that displays a list. All good. I wanted to put some well formatted textview preceding the list.
I tried creating a linear layout with a textview and adding it as a header to the list but it does not work.
The following does work:
TextView tv = new TextView(getActivity());
tv.setText(“Some title with comments”);
getListView().addHeaderView(tv);
tv.setGravity(Gravity.LEFT);
tv.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20);
I get the text before the list but I would like to actually set a layout instead as I would like text with 2 different fonts (it is like a title/caption with big font and a small description with smaller font).
How can I do that?
I don’t have any layout with list view defined. I only have a ListFragment
and my custom listAdapter
Update:
What I also tried and did not work:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
this.headerView = inflater.inflate(R.layout.my_list_header, container, false);
return super.onCreateView(inflater, container, savedInstanceState);
}
And later when setting the adapter:
getListView().addHeaderView(this.headerView);
But I got exceptions
Write an ArrayAdapter extending ArrayAdapter then do this inside getView method...
You can create a custom view with the specifics you need, and then then, for the method "addHeaderView" you can assign you view.
eg:
In your adapter, inside your
getView()
, you can load your header layout while position is0
.Check out my code.
Here are the steps for properly adding a header in a ListFragment: