本文介绍了如何嵌入视图片段在ListFragment的头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在的onCreate有一个 FragmentActivity 在那里我动态地添加片段取值。我希望有一个片段上方 ListFragment 。当然,我想的第一件事是碎片在一个的LinearLayout 在一个滚动型,但随后的 ListFragment 只能一次显示一行。

I have a FragmentActivity where I'm dynamically adding Fragments in my onCreate. I want a Fragment above a ListFragment. Of course, the first thing I tried was the fragments in one LinearLayout in one ScrollView, but then the ListFragment would only show one row at a time.

我发现Scrolling 2片段(一个片段,一个ListFragment)为一体,但我不知道怎么弄的片段的父视图添加到标题视图的 ListFragment 。我明明得到NPE电话时 fragment.getView() fragment.getView()的getParent(),因为我M仍然在的onCreate 我的活动,这样的观点尚未初始化。

I found Scrolling 2 fragment (one Fragment and one ListFragment) as one, but I don't know how to get the parent view of the Fragment to add to the header view of the ListFragment. I obviously get an NPE when call fragment.getView() and fragment.getView().getParent() because I'm still in the onCreate of my activity, so the views haven't been initialized yet.

如何可以把一个片段上方 ListFragment

推荐答案

创建一个包含您的第一个片段的布局,像这​​样:

Create a layout that contains your first fragment, like so:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >

        <fragment
            android:id="@+id/content"
            android:name="your.fragment.YourFragment"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" />

    </FrameLayout>

然后,在你的ListFragment,添加此布局的标题,像这样:

And then, in your ListFragment, add this layout as a header, like so:

View header = inflater.inflate(R.layout.widget_user_fragment, null, false);
getListView().addHeaderView(header, null, true);

这篇关于如何嵌入视图片段在ListFragment的头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-25 18:44