【问题标题】:NestedScrollView doesn't scroll all the childsNestedScrollView 不会滚动所有的孩子
【发布时间】:2018-05-17 21:01:49
【问题描述】:

我正在使用带有 NestedScrollView 的 CollapsingToolbarLayout,在 NestedScrollView 内我有一个项目列表,但在执行过程中我无法滚动所有项目。

在我的示例中,我没有看到最后一个 TextView 和一些包含。

这是我的 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>

这是我的物品清单。

<?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>

这张照片在顶部 而这个,当我向下滚动时,你可以看到它没有显示文本视图,即使卡片视图没有完全显示。

【问题讨论】:

    标签: java material-design


    【解决方案1】:

    我找到了答案,我补充了

    android:paddingBottom="?attr/actionBarSize" 
    

    对于 NestedScrollingView 内的布局

    【讨论】:

    • 我挣扎了将近 3-4 个小时,这对我有用。谢谢
    【解决方案2】:

    当您使用嵌套滚动视图时,您必须明确告诉应用程序在不同元素上使用哪个滚动。 ParentLayout 的滚动默认是激活的。下面的代码帮助我在嵌套滚动视图中激活列表的滚动

    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;
                }
            });
    

    编辑

    我只是将静态数据放入XML文件中,我该如何使用你的功能?

    我认为你应该把你的代码放在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

    【讨论】:

    • 我没有使用 ListView,我只是将静态数据放入 XML 文件中,我该如何使用你的函数?我可以将它用于包含包含标签的 LinearLayout 吗?
    • 你是怎么尝试的?
    • 我试过这些方法:ScrollView > NestedScrollView >LinearLayout > Items --- NestedScrollView > ScrollView > LinearLayout > Items。
    • 我找到了答案检查我的评论
    【解决方案3】:

    使用 CoordinatorLayout折叠工具栏 时,您必须在 java 中将 toolbar 定义为 actionbar像这样的文件

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

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

    如果活动setSupportActionBar(toolbar);

    也可以在工具栏的xml中使用app:popupTheme="@style/AppTheme.PopupOverlay"

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

    还删除任何边距或最小高度。这解决了我的问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-12-23
      • 1970-01-01
      • 2020-06-01
      • 2020-04-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多