I have a class that extends ListFragment. I want to add a fixed header.
I tried this way:
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
View mHeaderView = getActivity().getLayoutInflater().inflate(R.layout.remedy_header_view, null);
getListView().addHeaderView(mHeaderView);
if (mAdapter == null) {
int layout = Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB ? android.R.layout.simple_list_item_activated_1: android.R.layout.simple_list_item_1;
mAdapter = new SimpleCursorAdapter(getActivity(), layout, null, new String[] { ClinicalTip.Remedies.COLUMN_NAME_REMEDY_NAME }, new int[] { android.R.id.text1 }, 0);
}
setListAdapter(mAdapter);
setListShown(false);
Activity().getSupportLoaderManager().initLoader(0, null, this);
}
@Override
public void onDestroyView() {
// TODO Auto-generated method stub
super.onDestroyView();
setListAdapter(null);
}
But using this way, the header is not fixed, it gets scrolled. What could be the solution for adding fixed header in ListFragment?
The ListView
's headerView
scroll with the other elements of the ListView
. If you want to have a fix headerView with the ListView's elements scrolling beneath it, you have to change the Layout you returned inside onCreateView
. for instance:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/header"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="myHeader" />
<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
</LinearLayout>
For instance:
View detailListHeader ;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
super.onCreateView(inflater, container,
savedInstanceState);
View view = inflater.inflate(R.layout.myxml, container, false);
detailListHeader = view.findViewById(R.id.header);
return view;
}
detailListHeader is your header