折叠工具栏 ImageView 与 WebView 一起滚动而不是折叠

问题描述

我在我的 Activity 中使用折叠工具栏布局,带有 ImageViewWebView。我希望 ImageView 保持不变,而 WebView 滚动它。但目前两个视图一起滚动。如何达到我需要的效果

布局:

<androidx.coordinatorlayout.widget.CoordinatorLayout 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:animateLayoutChanges="true"
    android:fitsSystemWindows="true"
    app:layout_collapseMode="parallax"
    tools:context=".RecipeView">

    <com.google.android.material.appbar.AppBarLayout
        android:id="@+id/app_bar_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <com.google.android.material.appbar.CollapsingToolbarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_scrollFlags="scroll|snap|exitUntilCollapsed"
            app:statusBarScrim="@color/white">

            <ImageView
                android:id="@+id/recipeImage"
                android:layout_width="match_parent"
                android:layout_height="325dp"
                android:layout_marginBottom="-50dp"
                android:background="@color/greenPrimary"
                android:scaleType="centerCrop" />

        </com.google.android.material.appbar.CollapsingToolbarLayout>

    </com.google.android.material.appbar.AppBarLayout>

    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginHorizontal="32dp"
        android:contentDescription="save recipe"
        android:src="@drawable/ic_save"
        android:tint="@color/white"
        app:backgroundTint="@android:color/holo_green_dark"
        app:layout_anchor="@id/app_bar_layout"
        app:layout_anchorGravity="bottom|end" />

    <androidx.core.widget.nestedScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        android:fillViewport="true">

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

            <ProgressBar
                android:id="@+id/progressBar"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginVertical="16dp"/>

            <WebView
                android:id="@+id/webView"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />
        </LinearLayout>
    </androidx.core.widget.nestedScrollView>

</androidx.coordinatorlayout.widget.CoordinatorLayout>

活动代码

public class RecipeView extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recipe_view);

        ImageView imRecipeImage = findViewById(R.id.recipeImage);
        FirebaseStorage storage = FirebaseStorage.getInstance();
        StorageReference storageRef = storage.getReference();
        StorageReference pathRef = storageRef.child("images/image"+getIntent().getIntExtra("imageUrlId",0)+".jpg");
        GlideApp.with(this)
                .load(pathRef)
                .placeholder(R.drawable.ic_placeholder)
                .into(imRecipeImage);

        String url = getIntent().getStringExtra("contentUrl");
        WebView webView = findViewById(R.id.webView);
        WebViewClient webViewClient = new MyWebViewClient();
        webView.setWebViewClient(webViewClient);
        webView.loadUrl(url);
    }

    class MyWebViewClient extends WebViewClient {
        @Override
        public void onPageFinished(WebView view,String url) {
            super.onPageFinished(view,url);
            findViewById(R.id.progressBar).setVisibility(View.GONE);
        }
    }
}

我已经尝试删除以下属性并尝试了很多 StackOverflow 答案但没有帮助。

android:fitsSystemWindows="true"
app:layout_collapseMode="parallax"
android:fillViewport="true"

另外,我没有使用工具栏,因为我想完全隐藏 ImageView。

解决方法

找到了解决方案。当我将此行添加到`ImageView'时它起作用了,

app:layout_collapseMode="parallax"