Not be able to fully scroll inside a NestedScrollView

落爺英雄遲暮 提交于 2021-02-07 14:33:31

问题


Somehow I'm not able to fully scroll inside my NestedScrollView. Inside my NestedScrollView is a TextView with a lot of text and a button below it. When I scroll down I can only see a the top part of the button (only a few pixels). It doesn't scroll far enough.

This is my layout:

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

<android.support.design.widget.AppBarLayout
        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:stateListAnimator="@drawable/appbar_always_elevated"
        android:layout_height="128dp">

    <android.support.design.widget.CollapsingToolbarLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:contentScrim="?attr/colorPrimary"
            app:layout_scrollFlags="scroll|enterAlways|exitUntilCollapsed"
            app:toolbarId="@+id/toolbar">

        <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                app:title="@string/app_name"
                app:layout_collapseMode="pin">

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


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

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

        <TextView
                android:id="@+id/textView"
                style="@style/Text.Body1.Medium"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginStart="@dimen/margin"
                android:layout_marginTop="@dimen/margin"
                android:layout_marginEnd="@dimen/margin"
                android:text="@string/large_text"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@id/appbar"/>

        <Button
                android:id="@+id/continueButton"
                style="@style/Widget.Button.Green"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="@dimen/margin"
                android:layout_marginStart="@dimen/margin"
                android:layout_marginEnd="@dimen/margin"
                android:layout_marginBottom="@dimen/margin"
                android:text="@string/continue_text"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"/>
    </LinearLayout>
   </android.support.v4.widget.NestedScrollView>

  </android.support.design.widget.CoordinatorLayout>

回答1:


Change the height of the LinearLayout inside your NestedScrollView to be wrap_content.

The idea behind NestedScrollView and its single scrolling child is that the outside (the scroll view) should have some fixed / predetermined height, and the inside (the scrolling child) should have a dynamic height that is larger than the parent. After all, if the content isn't larger than the parent, then what's scrolling?




回答2:


You need to add android:fitsSystemWindows="true" in your CoordinatorLayout , AppBarLayout and CollapsingToolbarLayout ,

Read more about fitsSystemWindows

Why would I want to fitsSystemWindows?

Try this

<?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"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="128dp"
        android:fitsSystemWindows="true">

        <android.support.design.widget.CollapsingToolbarLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fitsSystemWindows="true"
            app:contentScrim="?attr/colorPrimary"
            app:layout_scrollFlags="scroll|enterAlways|exitUntilCollapsed"
            app:toolbarId="@+id/toolbar">

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                app:layout_collapseMode="pin"
                app:title="@string/app_name">

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


    <android.support.v4.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:clipToPadding="false"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

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

            <TextView
                android:id="@+id/textView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/large_text" />

            <Button
                android:id="@+id/continueButton"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="continue_text" />
        </LinearLayout>
    </android.support.v4.widget.NestedScrollView>
</android.support.design.widget.CoordinatorLayout>



回答3:


This is my working .xml code .

Please Apply it.

strings.xml

<string name="appbar_scrolling_view_behavior" translatable="false">android.support.design.widget.AppBarLayout$ScrollingViewBehavior</string>

main_activity.xml file

<android.support.design.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/adView">

        <android.support.design.widget.AppBarLayout
            android:id="@+id/appBarLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

            <android.support.design.widget.CollapsingToolbarLayout
                android:id="@+id/collapsingToolbarLayout"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:fitsSystemWindows="true"
                app:expandedTitleMarginEnd="64dp"
                app:expandedTitleMarginStart="48dp"
                app:layout_scrollFlags="scroll|exitUntilCollapsed">
                <!--app:contentScrim="?attr/colorPrimary"-->


                <RelativeLayout
                    android:id="@+id/lay"
                    android:layout_width="match_parent"
                    android:layout_height="@dimen/inhouse_ad_card_height"
                    android:background="@drawable/app_gradient_square"
                    android:gravity="center"
                    android:orientation="vertical"
                    android:visibility="visible"
                    app:layout_collapseMode="parallax"
                    app:layout_scrollFlags="scroll|enterAlways">

                <!--write tag here-->

                </RelativeLayout>

                <android.support.v7.widget.Toolbar
                    android:id="@+id/toolbar"
                    android:layout_width="match_parent"
                    android:layout_height="?attr/actionBarSize"
                    android:background="@drawable/bg_transition"
                    android:visibility="visible"
                    app:contentInsetEnd="@dimen/dim_10"
                    app:contentInsetStart="@dimen/dim_10"
                    app:layout_collapseMode="pin"
                    app:popupTheme="@style/ThemeOverlay.AppCompat.Light">

                    <RelativeLayout
                        android:id="@+id/layBtns"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center">

                        <ImageView
                            android:id="@+id/btnSetting"
                            android:layout_width="@dimen/btn_height"
                            android:layout_height="@dimen/btn_height"
                            android:layout_alignParentLeft="true"
                            android:layout_alignParentStart="true"
                            android:layout_centerVertical="true"
                            android:gravity="center"
                            ads:srcCompat="@drawable/ic_editor_settings" />

                        <TextView
                            android:id="@+id/txtAppTitle"
                            style="@style/TitleFont"
                            android:layout_width="wrap_content"
                            android:layout_height="@dimen/img_btn_height"
                            android:layout_centerInParent="true"
                            android:gravity="center"
                            android:text="@string/display_name"
                            android:textColor="@color/white"
                            android:textSize="@dimen/font_size_extra_large"
                            android:visibility="gone" />

                    </RelativeLayout>

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

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

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

        <android.support.v4.widget.NestedScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:scrollbars="none"
            app:layout_behavior="@string/appbar_scrolling_view_behavior">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:focusableInTouchMode="true"
                android:orientation="vertical">

                    <!--write tag here-->

            </LinearLayout>

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

    </android.support.design.widget.CoordinatorLayout>



回答4:


Just change your NestedScrollView to this:

<android.support.v4.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        android:fillViewport="true">



回答5:


Try this

   private void updateListViewHeight(ListView myListView) {

    ListAdapter myListAdapter = myListView.getAdapter();

    if (myListAdapter == null) {
        return;
    }
    //get ListView height
    int totalHeight = 0;
    int adapterCount = myListAdapter.getCount();
    for (int size = 0; size < adapterCount; size++) {
        View listItem = myListAdapter.getView(size, null, myListView);
        listItem.measure(0, 0);
        totalHeight += listItem.getMeasuredHeight();
    }
    //Change Height of ListView
    ViewGroup.LayoutParams params = myListView.getLayoutParams();
    params.height = totalHeight + (myListView.getDividerHeight() * (adapterCount - 1));
    myListView.setLayoutParams(params);

}

Pass your listview in the above method.




回答6:


For everyone's reference, since I just had the same problem. This is a known issue and has been fixed in com.google.android.material:material:1.3.0-alpha04

https://github.com/material-components/material-components-android/commit/a21a30026a33fc20cf7ad699d32d1298b84096c6



来源:https://stackoverflow.com/questions/53598676/not-be-able-to-fully-scroll-inside-a-nestedscrollview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!