【问题标题】:Interface like Google Play Store类似 Google Play 商店的界面
【发布时间】:2016-12-21 11:55:30
【问题描述】:

我正在尝试实现类似于 Google Play 商店的目标。我需要做的是有一个可滚动的activity,由horizo​​ntal 项目列表组成,然后是一个垂直项目列表,然后是一个水平项目列表,最后是另一个垂直项目列表。

要获取所有四个列表的数据,我必须调用四个不同的 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


【解决方案1】:

尝试用ScrollViewLinearLayout 包装在您的activity_main.xml 中:

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        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>

</ScrollView>

注意:记得将LinearLayout的高度改为wrap_content

【讨论】:

    【解决方案2】:

    我终于设法通过以下方式使其工作。首先,我用 NestedScrollView 包装了包含所有片段的 LinearLayout

    <android.support.v4.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="true"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">
    
            <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>
    
    </android.support.v4.widget.NestedScrollView>
    

    然后在我调用的每个片段上 mRecyclerView.setNestedScrollingEnabled(false);

    最后我将每个片段中的 layout_height 设置为 wrap_content,也在 RecyclerView 上。

    <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="wrap_content"
    tools:showIn="@layout/activity_main">
    
    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
    

    【讨论】:

      【解决方案3】:

      您可以使用click this link recyclerView 适配器(Core-adapter)处理多种类型的视图。只需阅读教程并查看示例代码,即可找到正确答案。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-04-05
        • 2013-06-04
        • 2017-01-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-03-17
        相关资源
        最近更新 更多