【发布时间】:2012-09-27 21:51:47
【问题描述】:
我的目标是 API 15 和最低 8 的 Android 应用程序。所以我使用支持库来管理片段。我有一组片段,我在应用程序的几个部分中使用它们。
现在,在一个活动中,我的布局中有一个 ListView:
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/listOfEvents"
android:layout_width="match_parent" android:layout_height="match_parent">
</ListView>
我想在 ListView 标题中添加我的一个片段。我试过这个:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.event_open);
listOfEvents = (ListView) findViewById(R.id.listOfEvents);
Fragment fragment = new SortingStandardFragment();
getSupportFragmentManager()
.beginTransaction()
.add(fragment, null)
.commit();
View fragmentView = fragment.getView(); // problem: fragment is null!
listOfEvents.addHeaderView(fragmentView);
}
但我得到一个错误,因为 fragment.getView() 返回 null(api 参考文档说我必须在 add 调用中放置一个 GroupView Id,但是我应该将 GroupView 放在布局中的哪个位置? 还有其他方法可以达到目标吗?
【问题讨论】:
-
我不确定这是否可以这样工作,但片段的想法是创建一个设置 id 的框架布局(您可以通过在资源中添加 id 名称来制作 id),添加为标题,然后制作一个用片段替换该 id 的事务
标签: android android-listview android-fragments android-support-library