繁体   English   中英

像Google Play商店这样的界面

[英]Interface like Google Play Store

我正在努力实现类似于Google Play商店的目标。 我需要做的是有一个由水平项目列表,垂直项目列表,水平项目列表以及最后一个垂直项目列表组成的可滚动activity

要获取所有四个列表的数据,我将必须调用四个不同的API端点。

我现在拥有的主机活动有4个片段。 每个片段负责调用API并获取项目列表。 每个片段都会创建一个recyclerview并使用适配器来构建列表并显示它。 我可以成功地从所有fragments获取数据,但是由于某种原因该活动仅显示第一个片段(我也怀疑拥有4个recyclerviews的活动将能够处理垂直滚动)。

如果设置了固定的重量,它将显示压缩的4个片段但没有滚动。 但这不是我想要的。

主要活动

    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>

关于如何实现自己正在尝试的任何想法?

尝试使用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

我终于设法通过以下方式使其工作。 首先,我用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);。

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM