我有一个扩展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);
}
但是使用这种方式,标题不是固定的,而是滚动的。在ListFragment中添加固定报头的解决方案是什么?
发布于 2013-06-22 16:30:22
ListView
的headerView
与ListView
的其他元素一起滚动。如果你想有一个固定的headerView,让列表视图的元素在它下面滚动,你必须改变你在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>
例如:
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是您的标头
发布于 2013-06-22 16:29:49
显然,它将与列表一起滚动。
不这样做,而是在ListView上设置一个视图,并将标题视图的内容添加到该视图中。
这样做,标题视图将始终保持静态,但列表将是可滚动的。
另外,如果您只想将它与列表片段一起使用,那么:
ListFragment有一个由单个列表视图组成的默认布局。但是,如果您愿意,可以通过从onCreateView(LayoutInflater、ViewGroup、Bundle)返回自己的视图层次结构来自定义片段布局。为此,视图层次结构必须包含一个id为"@android:id/ list“的ListView对象(如果是在代码中,则为list)。
因此,您的布局文件将如下所示。
<?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>
https://stackoverflow.com/questions/17248792
复制相似问题