gaymer
gaymer

Reputation: 562

Fragment inside scrollView

I need to have a arrayAdapter (fragment) inside a scroll view with buttons underneath, Unfortunately, when doing that my fragment have the size of one element and is scrolling himself

here is my view:

<ScrollView
    android:id="@+id/scrollView1"
    android:layout_width="match_parent"
    android:layout_height="fill_parent" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >

        <fragment
            android:id="@+id/myfragment"
            android:name="com.myapply.MyListFragment"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="clip_vertical" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="TEST TEST TEST"
            android:textColor="#FFFFFF"
            android:textSize="25dp" />
    </LinearLayout>
</ScrollView>

any help will be welcome

Upvotes: 5

Views: 10362

Answers (2)

Dan Devine
Dan Devine

Reputation: 877

I did something similar to this, and I used the view ids as 'hooks' for the Fragments.

From my understanding of Fragments, they need to be hooked to a particular View ID, otherwise they don't get displayed. I simply created a View container that had twenty empty child views, all with id's numbered 0-19. I loaded this with the Activity, then attached the fragments to the views however I wanted.

Looked like:

<LinearLayout android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <FrameLayout
        android:id="@+id/fragment_0"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    ....
    <FrameLayout
        android:id="@+id/fragment_19"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
</LinearLayout>

I wasn't as good at creating and attaching child views dynamically as I am today. These days, I think I'd just have an empty LinearLayout or ScrollView, then attach views dynamically and generate IDs for them as I needed them. One of the problems that I ran into with this, was the inability to detach and re-attach Fragments to anything but their own ViewID; once attached to a ViewID, it must be reattached to that same ViewID. Small slowdown, but not if you keep this ID, you may be able to recreate a new FrameLayout and set the ID to what it was before (don't know for sure).

Using this approach: 1) create an Activity with LinearLayout or ScrollView (no child views yet). 2) For each fragment you wish to attach, create a new FrameLayout (or whatever) 3) Set the ViewID for the FrameLayout (or whatever) to either a new generated view id or the previous id if the fragment was previously attached. 4) Add the FrameLayout (or whatever) to the LinearLayout/ScrollView. 5) Use the FragmentManager to attach the fragment to the view that you just added.

Hope this helps.

Upvotes: 0

gunar
gunar

Reputation: 14710

Short answer is that you cannot have a ListView inside a ScrollView. Here is a related SO thread.

Looking at your layout you could redesign it to a RelativeLayout:

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

    <fragment
            android:id="@+id/myfragment"
            android:name="com.myapply.MyListFragment"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="clip_vertical"
            android:layout_above="@+id/bottomTextView" />

        <TextView
            android:id="@+id/bottomTextView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="TEST TEST TEST"
            android:textColor="#FFFFFF"
            android:layout_alignParentBottom="true"
            android:textSize="25dp" />

</RelativeLayout>

Upvotes: 1

Related Questions