简体   繁体   English

NestedScrollView 不会滚动所有孩子

[英]NestedScrollView doesn't scroll all the childs

I am using CollapsingToolbarLayout with NestedScrollView, and inside the NestedScrollView I have a list of items, but in the execution I can't scroll all of them.我将 CollapsingToolbarLayout 与 NestedScrollView 一起使用,在 NestedScrollView 中我有一个项目列表,但在执行过程中我无法滚动所有项目。

In my example I'm not seeing the last TextView and some of the includes.在我的示例中,我没有看到最后一个 TextView 和一些包含。

This is my activity_main.xml这是我的activity_main.xml

<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/main_content"
    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="256dp"
        android:fitsSystemWindows="true"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

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

            <ImageView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:fitsSystemWindows="true"
                android:scaleType="centerCrop"
                android:src="@drawable/space"
                app:layout_collapseMode="pin"/>

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:elevation="7dp"/>

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

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

    <android.support.design.widget.FloatingActionButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="16dp"
        android:src="@drawable/ic_search"
        app:layout_anchor="@id/appbar"
        app:layout_anchorGravity="bottom|right"/>

    <!-- Your Scrollable View : Can be Nested Scroll View or Recycler View-->
    <android.support.v4.widget.NestedScrollView
        android:id="@+id/nestedScrollView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

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

            <include layout="@layout/item_nested_scrollview"/>
            <include layout="@layout/item_nested_scrollview"/>
            <include layout="@layout/item_nested_scrollview"/>
            <include layout="@layout/item_nested_scrollview"/>
            <include layout="@layout/item_nested_scrollview"/>
            <include layout="@layout/item_nested_scrollview"/>
            <include layout="@layout/item_nested_scrollview"/>
            <include layout="@layout/item_nested_scrollview"/>
            <include layout="@layout/item_nested_scrollview"/>
            <include layout="@layout/item_nested_scrollview"/>
            <include layout="@layout/item_nested_scrollview"/>
            <include layout="@layout/item_nested_scrollview"/>
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="LastOne"/>

        </LinearLayout>

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

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

And this my list of items.这是我的物品清单。

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="16dp"
    android:elevation="5dp">

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

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Smartherd"
            android:textColor="@color/colorPrimary"
            android:textSize="20sp"
            android:textStyle="bold"/>

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="5dp"
            android:text="@string/description"
            android:textColor="@android:color/black"
            android:textSize="15sp"/>

    </LinearLayout>

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

This photo at the top上面这张照片在此处输入图片说明 And this one, when I scrolled all down as you can see it's not showing the textview even the cardview is not showing completly.而这个,当我向下滚动时,你可以看到它没有显示文本视图,即使卡片视图没有完全显示。 在此处输入图片说明

I found the answer, I added我找到了答案,我补充说

android:paddingBottom="?attr/actionBarSize" 

for the Layout inside NestedScrollingView对于 NestedScrollingView 内的布局

When you are working with nested scroll views then you have to explicitly tell the app that which scroll to use on different elements.当您使用嵌套滚动视图时,您必须明确告诉应用程序在不同元素上使用哪个滚动。 ParentLayout's scroll is activated by default. ParentLayout 的滚动默认是激活的。 Below code helped me to activate my List's scroll inside a nested scroll View下面的代码帮助我在嵌套滚动视图中激活我的列表滚动

yourListView.setOnTouchListener(new View.OnTouchListener() {
            // Setting on Touch Listener for handling the touch inside ScrollView
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                // Disallow the touch request for parent scroll on touch of child view
                v.getParent().requestDisallowInterceptTouchEvent(true);
                return false;
            }
        });

EDIT编辑

I'm just putting static data in the XML file, how can I use your function?我只是将静态数据放在 XML 文件中,我该如何使用您的功能?

I think then you should put your code inside a ScrollView我想那么你应该把你的代码放在一个ScrollView

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

Source 来源

When using CoordinatorLayout and collapsing toolbar you have to define your toolbar as actionbar in java file like that使用CoordinatorLayout折叠工具栏时,您必须像这样java 文件中将工具栏定义为操作

toolbar=view.findViewById(R.id.toolbar);

if fragment ((AppCompatActivity)getActivity()).setSupportActionBar(toolbar);如果片段((AppCompatActivity)getActivity()).setSupportActionBar(toolbar);

if activity setSupportActionBar(toolbar);如果活动setSupportActionBar(toolbar);

And also use in xml of toolbar app:popupTheme="@style/AppTheme.PopupOverlay"并在工具栏app:popupTheme="@style/AppTheme.PopupOverlay" xml 中使用

<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />

also remove any margings or min heights .this solved my problem.还删除任何边距或最小高度。这解决了我的问题。

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

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