Shahriar
Shahriar

Reputation: 824

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

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://schemas.android.com/apk/res/android"
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://schemas.android.com/apk/res/android"
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://schemas.android.com/apk/res/android"
    android:id="@+id/relativelayout"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">
<idunnololz.widgets.AnimatedExpandableListView
    xmlns:android="http://schemas.android.com/apk/res/android"
    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 ?

Upvotes: 2

Views: 1021

Answers (1)

Divya Jain
Divya Jain

Reputation: 393

You can use this method to make list view height based on children.

Call this method after set your list-view adapter

setListViewHeightBasedOnItsChildren(listView);

public static void setListViewHeightBasedOnItsChildren(ListView listView) {

    if (listView.getAdapter() == null) {
        // pre-condition adaptershould not be null
        return;
    }

    int totalHeight = 0;
    for (int i = 0; i < listView.getAdapter().getCount(); i++) {
        View listItem = listView.getAdapter().getView(i, null, listView);
        listItem.measure(0, 0);
        totalHeight += listItem.getMeasuredHeight();
    }
    //set layout params for listview
    ViewGroup.LayoutParams params = listView.getLayoutParams();
    params.height = totalHeight
            + (listView.getDividerHeight() * (listView.getAdapter()
                    .getCount() - 1));
    listView.setLayoutParams(params);
    listView.requestLayout();
} 

Upvotes: 1

Related Questions