【发布时间】:2016-12-21 11:55:30
【问题描述】:
我正在尝试实现类似于 Google Play 商店的目标。我需要做的是有一个可滚动的activity,由horizontal 项目列表组成,然后是一个垂直项目列表,然后是一个水平项目列表,最后是另一个垂直项目列表。
要获取所有四个列表的数据,我必须调用四个不同的 API 端点。
我现在拥有的是一个包含 4 个片段的主机活动。每个片段负责调用 API 并获取项目列表。每个片段创建一个recyclerview 并使用一个适配器来构建列表并显示它。我可以成功地从所有fragments 中获取数据,但由于某种原因,活动仅显示第一个片段(我也怀疑持有 4 recyclerviews 的活动是否能够处理垂直滚动)。
如果我设置一个固定的权重,它将显示 4 个片段被挤压但没有滚动。但这不是我想要的。
MainActivity
public class MainActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.add(R.id.first_container, FirstFragment.newInstance(parameter1));
transaction.add(R.id.second_container, SecondFragment.newInstance(parameter2));
transaction.add(R.id.third_container, FirstFragment.newInstance(parameter3));
transaction.add(R.id.fourth_container, SecondFragment.newInstance(parameter4));
transaction.commit();
}
}
activity_main
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<FrameLayout
android:id="@+id/first_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<FrameLayout
android:id="@+id/second_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<FrameLayout
android:id="@+id/third_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<FrameLayout
android:id="@+id/fourth_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
然后我所有的四个fragments 都有一个带有recyclerview 的布局,在代码中我将其设置为水平 或垂直,具体取决于fragment。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:showIn="@layout/activity_main">
<android.support.v4.widget.SwipeRefreshLayout
android:id="@+id/SwipeRefreshLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</android.support.v4.widget.SwipeRefreshLayout>
知道如何实现我正在尝试的目标吗?
【问题讨论】:
-
如果片段 layout_width 是 wrap_content 会发生什么?你不应该这样做,但是垂直滚动活动中每个片段上的滑动刷新让我担心,这似乎值得关注
-
如果我为每个片段设置 layout_width 为 wrap_content 什么都不会发生。另外我要删除滑动刷新。
-
如果每个片段执行一个片段事务会怎样? (即,执行四次 BeginTransaction 和 Commit)
-
同样的结果,只显示第一个片段。
-
谢谢。不确定将什么标记为已接受的答案,因为它结合了您的提示、Kamran 建议和我自己的研究。
标签: android android-layout android-fragments scroll android-recyclerview