2

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?

2 Answers 2

5

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

Sign up to request clarification or add additional context in comments.

4 Comments

How I can make use of this in ListFragment?
I'm using your code exactly but I'm getting this error: "java.lang.IllegalStateException: Can't be used with a custom content view"
try calling super.onCreateView(inflater, container, savedInstanceState); before any other operation. Also I notice that you are using setListShow(boolean). Take a look here: code.google.com/p/android/issues/detail?id=21742
I commented setListShown, now its working! Can you please tell me what could be the alternative to setListShown?
0

Obviously it will get scrolled with the list.
Instead of doing this, set a View on top of the ListView and add the content of your header view to it.

By doing this, Header view will remain always static but list will be scroll-able.

Also if you want to use it with the List Fragment only then:
ListFragment has a default layout that consists of a single list view. However, if you desire, you can customize the fragment layout by returning your own view hierarchy from onCreateView(LayoutInflater, ViewGroup, Bundle). To do this, your view hierarchy must contain a ListView object with the id "@android:id/list" (or list if it's in code).

So your layout file will look like.

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingLeft="8dp" android:paddingRight="8dp">

 `<TextView android:id="@id/header"
           android:layout_width="match_parent"
           android:layout_height="match_parent" />`

 `<ListView android:id="@id/android:list"
           android:layout_width="match_parent"
           android:layout_height="match_parent" />`

</LinearLayout>

5 Comments

How can I do what you are suggesting using ListFragment?
If there is any issue in using this in list fragment, use simple fragment. Just make sure that in the layout for that fragment you have a view that is always on top(or before) of the listview.
List fragment is an easy way to use list as it provides some more overridden methods but the purpose will be solved by the normal fragment also.
I am using your layout exactly, I'm getting this error: "Error: No resource found that matches the given name (at 'id' with value '@id/header')."
oh come on ... that was a boilerplate code ... the correct syntax was @+id/header ... one should have easily fixed this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.