首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在ListFragment中添加固定报头

如何在ListFragment中添加固定报头
EN

Stack Overflow用户
提问于 2013-06-22 16:27:03
回答 2查看 4.9K关注 0票数 2

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

我试过这样做:

代码语言:javascript
运行
复制
@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中添加固定报头的解决方案是什么?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-06-22 16:30:22

ListViewheaderViewListView的其他元素一起滚动。如果你想有一个固定的headerView,让列表视图的元素在它下面滚动,你必须改变你在onCreateView中返回的布局。例如:

代码语言:javascript
运行
复制
<?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>

例如:

代码语言:javascript
运行
复制
 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是您的标头

票数 5
EN

Stack Overflow用户

发布于 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">

代码语言:javascript
运行
复制
 `<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>

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17248792

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档