如何在Android中使用片段创建可滚动模板

时间:2019-05-22 09:50:27

标签: android android-layout android-fragments

我正在尝试实现以下目标

Wireframe vertical layout. Search bar fragment, Banner Slider, Items Fragment

我正在尝试建立一个连接3个片段的活动。顶部片段包含一个搜索栏(EditText),中间片段包含一个横幅滑块(ViewPager),底部片段包含一个GridView,动态加载了n个项目。我想使中间和底部的片段垂直滚动,同时固定顶部的片段。但是,当高度设置为wrap_content时,“网格”视图将在其内部滚动。

如果GridView的高度固定,则说明其正常工作。但是我不能给它一个固定的高度,因为它的内容是动态加载的。

家庭活动:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    android:background="@color/colorLightGray"
    tools:context=".HomeActivity">

    <ScrollView
        android:id="@+id/scrollableContents"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="true"
        android:padding="5dp">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <FrameLayout
                android:id="@+id/fragmentContainerSearchBar"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">
            </FrameLayout>

            <FrameLayout
                android:id="@+id/fragmentContainerSlider"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">
            </FrameLayout>

            <FrameLayout
                android:id="@+id/fragmentContainerPopularItems"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">
            </FrameLayout>

        </LinearLayout>
    </ScrollView>      

</RelativeLayout>

SearchBarFragment:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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:context=".fragments.HomeSearchBarFragment">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingHorizontal="2dp"
        android:paddingVertical="2dp"
        android:focusable="true"
        android:focusableInTouchMode="true">
        <EditText
            android:id="@+id/etSearchBoxHome"
            android:inputType="text"
            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:drawableRight="@drawable/ic_search_24dp"
            android:drawablePadding="5dp"
            android:background="@drawable/et_search_home"
            android:paddingLeft="15dp"
            android:paddingRight="15dp"
            android:layout_marginTop="0dp"
            android:clickable="true"
            android:cursorVisible="true"
            android:focusable="true"
            android:textSize="14dp"
            android:hint="@string/home_searchbox_hint"/>
    </RelativeLayout>

</FrameLayout>

横幅滑块片段:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="200dp"
    android:layout_margin="2dp"
    tools:context=".fragments.HomePromoSliderFragment">

    <android.support.v7.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_margin="2dp"
        card_view:cardBackgroundColor="@color/colorWhite"
        card_view:cardCornerRadius="3dp">

        <android.support.v4.view.ViewPager
            android:id="@+id/view_pager"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
        </android.support.v4.view.ViewPager>

    </android.support.v7.widget.CardView>

</RelativeLayout>

热门项目片段:

<?xml version="1.0" encoding="utf-8"?>
<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:context=".fragments.HomePopularItemsFragment">

        <GridView
            android:id="@+id/gvPopularItems"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:columnWidth="150dp"
            android:gravity="center"
            android:numColumns="auto_fit"
            android:stretchMode="columnWidth">
        </GridView>
</RelativeLayout>

2 个答案:

答案 0 :(得分:0)

您可以在ViewPager和Gridview之外使用nestedScrollView。

将回收站视图设置为 setNestedScrollingEnable(false)

<android.support.v4.widget.NestedScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent">


    <android.support.v4.view.ViewPager
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    </android.support.v4.view.ViewPager>

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

    </GridView>

</android.support.v4.widget.NestedScrollView>

答案 1 :(得分:0)

Try using `Constraint Layout` for whole layout, `autocomplete text view` for search bar, `viewpager` for banners, recycler view for showing the loaded items.

<android.support.constraint.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

        <AutoCompleteTextView
                android:id="@+id/search"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:layout_constraintTop_toTopOf="parent"/>

         <android.support.v4.view.ViewPager
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintBottom_toBottomOf="parent">

           <android.support.v7.widget.RecyclerView
                    android:layout_width="match_parent"
                    android:layout_height="0dp"
                    app:layout_constraintBottom_toBottomOf="parent">
            </android.support.v7.widget.RecyclerView>

        </android.support.constraint.ConstraintLayout>

**根据用户界面设置边距

Set your layout manager as Gridlayout manager for recycler view as below:

recyclerview.layoutManager = GridLayoutManager(this, 2, OrientationHelper.VERTICAL, false)

希望它能正常工作。让我知道是否有任何问题。