【发布时间】: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