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?

4

2 回答 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

于 2013-06-22T08:30:22.113 回答
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>

于 2013-06-22T08:29:49.360 回答