我想为 UI 需要可滚动的 fragment 实现 UI。这个 fragment 有
- 三个具有动态高度的列表 fragment (取决于行数)
- 后跟一个可扩展列表(这也在 fragment 内)
简而言之,例如 HomeFragment UI 有 4 个 fragment 一个接一个,它应该占据屏幕空间并根据它们的高度滚动。
到目前为止我尝试做的是这个。
这是我放置 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 中添加我的其他 fragment ,
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"
ScrollView ,第一个 listfragment 会占据整个屏幕。但是,如果我将 android:layout_height="wrap_parent"
给 scrollview,则 fragment 高度很小。
关于如何将多个具有动态高度的 fragment 放入 ScrollView 的线性布局中的任何想法?
最佳答案
您可以使用此方法根据 child 制作 ListView 高度。
设置 ListView 适配器后调用此方法
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();
}
关于android - 如何将多个具有动态高度的 fragment 放入 ScrollView 的线性布局中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30101707/