Android Scrollview高度动态调整

时间:2016-08-19 05:06:37

标签: android android-scrollview android-nestedscrollview

我在CoordinatorLayout中有一个nestedScrollview。 现在,在nestedScrollview中有5-6个视图,并且visibilty将根据网络上可用的数据而变化。 问题是,当我隐藏任何视图的可见性时,scrollview总是占用相同的高度(所有5-6个视图的总数)并在底部留下一个空白区域。有没有人可以告诉我如何重新调整scrollview的高度,以便在scrollview底部没有空白区域。

<android.support.design.widget.CoordinatorLayout
    android:id="@+id/main_content"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    android:orientation="vertical">

<android.support.design.widget.AppBarLayout
    android:id="@+id/MyAppbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@android:color/white"
    android:fitsSystemWindows="true">

    <android.support.design.widget.CollapsingToolbarLayout
        android:id="@+id/collapse_toolbar"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        app:collapsedTitleGravity="center_horizontal"
        app:expandedTitleGravity="center_horizontal"
        app:layout_scrollFlags="scroll|exitUntilCollapsed">

        <!--Some content here-->

    </android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>

<android.support.v4.widget.NestedScrollView
    android:id="@+id/nested_scroll_view"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"

        <include
            android:id="@+id/profile_extra_details"
            layout="@layout/profile_extra_details_layout" />

        <LinearLayout
            android:id="@+id/see_project_listing_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"

            <include layout="@layout/divider" />

            <ViewSwitcher
                android:id="@+id/work_switcher"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                <include
                    android:id="@+id/see_project_listing"
                    layout="@layout/photos_with_heading" />

                <TextView
                    android:id="@+id/txt_no_work_found"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_gravity="center"
                    android:gravity="center"
                    android:text="There are no Design Boards"
                    />
            </ViewSwitcher>

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

这是NestedScrollview。以下是profile_extra_details_layout。这种布局的可见性是根据网络的数据可用性以及正在查看其配置文件的用户类型来控制的。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <include layout="@layout/divider" />

    <TextView
        android:id="@+id/user_description"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="@dimen/activity_horizontal_margin"
        android:layout_marginRight="@dimen/activity_horizontal_margin"
        android:visibility="gone" />

    <LinearLayout
        android:id="@+id/location_buget_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:gravity="center"
        android:orientation="horizontal"
        android:visibility="visible">

        <LinearLayout
            android:id="@+id/ll_budget_yearEstb"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:orientation="vertical"
            android:visibility="gone">

            <ImageView
                android:id="@+id/img_budget_yearEstb"
                android:layout_width="60dp"
                android:layout_height="60dp"
                android:layout_gravity="center_horizontal"
                android:src="@drawable/id_proejct_budget" />

            <TextView
                android:id="@+id/txt_budget_yearEstb"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal" />

        </LinearLayout>

        <LinearLayout
            android:id="@+id/ll_location"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:orientation="vertical"
            android:visibility="gone">

            <ImageView
                android:layout_width="60dp"
                android:layout_height="60dp"
                android:layout_gravity="center_horizontal"
                android:padding="5dp"
                android:src="@drawable/id_project_city" />

            <TextView
                android:id="@+id/location"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:padding="5dp"
                android:text="Mumbai,India"
                android:textStyle="bold" />
        </LinearLayout>

    </LinearLayout>

    <LinearLayout
        android:id="@+id/individual_firm_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_vertical"
        android:orientation="horizontal"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:visibility="gone">

        <ImageView
            android:layout_width="@dimen/image_size_beside_text"
            android:layout_height="@dimen/image_size_beside_text"
            android:src="@drawable/id_expert_firm_ind" />

        <TextView
            android:id="@+id/individual_firm_txt"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Firm/Individual" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/awards_ll"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_vertical"
        android:orientation="horizontal"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:visibility="gone">

        <ImageView
            android:layout_width="@dimen/image_size_beside_text"
            android:layout_height="@dimen/image_size_beside_text"
            android:src="@drawable/id_expert_awards" />

        <TextView
            android:id="@+id/awards_txt"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
    </LinearLayout>

    <LinearLayout
        android:id="@+id/web_add_ll"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_vertical"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:visibility="gone">

        <ImageView
            android:layout_width="@dimen/image_size_beside_text"
            android:layout_height="@dimen/image_size_beside_text"
            android:src="@drawable/id_seller_web" />

        <TextView
            android:id="@+id/web_add_txt"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Abc.xyz" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/ll_location_ho"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_vertical"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:visibility="gone">

        <ImageView
            android:layout_width="@dimen/image_size_beside_text"
            android:layout_height="@dimen/image_size_beside_text"
            android:src="@drawable/id_project_city" />

        <TextView
            android:id="@+id/location_ho_txt"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Abc.xyz" />
    </LinearLayout>
</LinearLayout>

如果我需要在任何地方详细说明,请告诉我。

2 个答案:

答案 0 :(得分:0)

这会对你有所帮助: 当您隐藏视图时使用View.GONE,我认为您正在使用View.INVISIBLE

答案 1 :(得分:0)

尝试将nestedscrollview height更改为match_parent,将子线性布局高度更改为wrap_content。之后根据您的数据在运行时更改线性布局的子布局的可见性。