如何在ListFragment中添加固定标头

Hom*_*mam 2 android android-listfragment

我有一个扩展ListFragment的类.我想添加一个固定的标题.

我试过这种方式:

@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);
}
Run Code Online (Sandbox Code Playgroud)

但是使用这种方式,标题不固定,它会滚动.在ListFragment中添加固定标头的解决方案是什么?

Bla*_*elt 5

所述ListViewheaderView滚动用的其它元件ListView.如果你想要一个修复headerView,ListView的元素在它下面滚动,你必须改变你在里面返回的布局onCreateView.例如:

<?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>
Run Code Online (Sandbox Code Playgroud)

例如:

 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;
}
Run Code Online (Sandbox Code Playgroud)

detailListHeader是你的标题