【问题标题】:how to put multiple fragments with dynamic height inside a linearlayout of a scrollview如何将具有动态高度的多个片段放在滚动视图的线性布局中
【发布时间】:2015-06-23 21:08:25
【问题描述】:

我想为需要滚动的片段实现 UI。这个片段有

  • 三个具有动态高度的列表片段(取决于行数)
  • 后跟一个可扩展列表(这也在片段内)

简而言之,例如 HomeFragment UI 有 4 个片段一个接一个,它应该占据屏幕空间并根据它们的高度滚动。

到目前为止,我尝试做的就是这个。

这是我放置HomeFragment 表单的主要容器 导航抽屉。

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

我的 HomeFragment 有这个布局,

<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>

我在我的 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();

我的第一个 Fragment UI 是这样的,

<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>

第二个Fragment是这样的,

   <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>

但问题是,如果我给 android:layout_height="match_parent" 滚动视图,第一个 listfragment 会占据整个屏幕。但是如果我给滚动视图android:layout_height="wrap_parent",片段高度很小。

关于如何将具有动态高度的多个片段放入滚动视图的线性布局中的任何想法?

【问题讨论】:

    标签: android android-layout android-fragments


    【解决方案1】:

    您可以使用此方法根据子级制作列表视图高度。

    设置列表视图适配器后调用此方法

    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();
    } 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-27
      • 1970-01-01
      • 2012-05-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多