[英]NestedScrollView and CoordinatorLayout. Issue on Scrolling
我對CoordinatorLayout
和NestedScrollView
(帶有設計支持庫22.2.0)有一個奇怪的問題
使用小於NestedScrollView
的內容,我應該具有固定的內容。 但是,嘗試上下滾動內容,我可以確定內容已被替換並且不再在自己的位置。
這里有一些示例:
這里的代碼:
<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.support.design.widget.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
app:layout_scrollFlags="scroll|enterAlways" />
</android.support.design.widget.AppBarLayout>
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<FrameLayout
android:paddingTop="24dp"
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="@dimen/padding">
</FrameLayout>
</android.support.v4.widget.NestedScrollView>
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab_action"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end|bottom"
android:layout_margin="16dp"
android:visibility="gone"
android:src="@drawable/ic_done" />
</android.support.design.widget.CoordinatorLayout>
在刪除明細片段中除一張卡片以外的所有卡片時,也可以在cheesesquare演示中看到這一點 。
我現在可以使用此類解決此問題: https : //gist.github.com/EmmanuelVinas/c598292f43713c75d18e
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="com.evs.demo.layout.FixedScrollingViewBehavior">
.....
</android.support.v4.widget.NestedScrollView>
我認為這不是支持庫中的錯誤,只需使用此
<android.support.v4.widget.NestedScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
android:layout_gravity="fill_vertical"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
android:layout_gravity="fill_vertical"
也為我工作。
我的答案可能遲到了,但是可以解決了。 我遇到了類似的問題,但是以上提到的解決方案都不適合我。 最后,我通過使用支持庫的版本23對其進行了修復。
...
compileSdkVersion 23
...
targetSdkVersion 23
...
compile 'com.android.support:appcompat-v7:23.1.0'
compile 'com.android.support:support-v4:23.1.0'
compile 'com.android.support:design:23.1.0'
在布局過程中,多次調用onMeasureChild()方法。 顯然,關鍵在於在此過程的早期階段獲得子高度的非零值。 ScrollingViewBehavior在以下情況下無法這樣做:
int scrollRange = appBar.getTotalScrollRange();
int height = parent.getHeight()
- appBar.getMeasuredHeight()
+ scrollRange;
FixedScrollingviewBehavior通過以下方式解決了此問題:
int height = parent.getHeight()
- appBar.getMeasuredHeight()
+ Math.min(scrollRange, parent.getHeight() - heightUsed);
很早就給height設置了-128的值,即應用欄的高度。
與原始版本接近的另一種選擇是:
int height = parent.getMeasuredHeight()
- appBar.getMeasuredHeight()
+ scrollRange;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.