簡體   English   中英

NestedScrollView 不會滾動所有孩子

[英]NestedScrollView doesn't scroll all the childs

我將 CollapsingToolbarLayout 與 NestedScrollView 一起使用,在 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>

上面這張照片在此處輸入圖片說明 而這個,當我向下滾動時,你可以看到它沒有顯示文本視圖,即使卡片視圖沒有完全顯示。 在此處輸入圖片說明

我找到了答案,我補充說

android:paddingBottom="?attr/actionBarSize" 

對於 NestedScrollingView 內的布局

當您使用嵌套滾動視圖時,您必須明確告訴應用程序在不同元素上使用哪個滾動。 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>

來源

使用CoordinatorLayout折疊工具欄時,您必須像這樣java 文件中將工具欄定義為操作

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

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

如果活動setSupportActionBar(toolbar);

並在工具欄app:popupTheme="@style/AppTheme.PopupOverlay" xml 中使用

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

還刪除任何邊距或最小高度。這解決了我的問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM