jeudi 7 mai 2015

how to put multiple fragments with dynamic height inside a linearlayout of a scrollview

I wan to implement an UI for a fragment where the ui needs to be scrollable. this fragment has

  • three listfragments which have dynamic height (depends on number of rows)
  • followed by an expandable list (this is also inside a fragment)

In short for example HomeFragment UI has 4 fragment one after another and it should occupy screen space and scroll based on their height.

what I have tried to do so far is this.

This is my main container where I put my HomeFragment form navigation drawer.

 <FrameLayout android:id="@+id/container" android:layout_width="match_parent"
    android:layout_height="wrap_content"
  />

my HomeFragment has this layout,

<ScrollView
xmlns:android="http://ift.tt/nIICcg"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
android:background="@color/blue"
>

<LinearLayout
    android:id="@+id/rootViewContainer"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:background="@color/green"
    >
</LinearLayout>

I add my other fragments like this in my HomeFragment,

   View rootView = inflater.inflate(R.layout.fragment_home, container, false);

    LinearLayout rootViewContainer = (LinearLayout) rootView.findViewById(R.id.rootViewContainer);
    rootViewContainer.removeAllViews();

    FragmentManager fragMan = getFragmentManager();
    FragmentTransaction fragTransaction;




    fragTransaction = fragMan.beginTransaction();
    fragTransaction.add(rootViewContainer.getId(), HomeTrendingArticleFragment.newInstance() , "fragment2");
    fragTransaction.commit();


    fragTransaction = fragMan.beginTransaction();
    fragTransaction.add(rootViewContainer.getId(), HomeCategoryArticleFragment.newInstance() , "fragment_video");
    fragTransaction.commit();

My first Fragment UI is like this,

<LinearLayout
xmlns:android="http://ift.tt/nIICcg"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>

<ListView
    android:id="@android:id/list"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    >
</ListView>

and the second Fragment is like this,

   <LinearLayout
    xmlns:android="http://ift.tt/nIICcg"
    android:id="@+id/relativelayout"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">
<idunnololz.widgets.AnimatedExpandableListView
    xmlns:android="http://ift.tt/nIICcg"
    android:id="@+id/categoryList"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:groupIndicator="@android:color/transparent"
    android:background="@color/base"
/>
</LinearLayout>

But the problem is, the first listfragment takes the whole screen if I give android:layout_height="match_parent" to scroll view. But if I give android:layout_height="wrap_parent" to scrollview, the fragment heights are small.

any idea about how to put multiple fragments with dynamic height inside a linearlayout of a scrollview ?

Aucun commentaire:

Enregistrer un commentaire