当前位置: 代码迷 >> Android >> AppBarLayout中的ViewPager不起作用?
  详细解决方案

AppBarLayout中的ViewPager不起作用?

热度:103   发布时间:2023-08-04 11:55:02.0

将ViewPager放在AppBayLayout中后,我在FrameLayout.LayoutParams和CoordinatorLayout.LayoutParams之间得到了强制转换异常。 如果我在以下xml中切换出带有ImageView的ViewPager,则效果很好。 关于什么可行的任何建议? 我想嵌入一个图像滑块,当滚动Webview时,该滑块消失。 我试过将ViewPager放在其他布局中,例如LinearLayout和FrameLayout,但是存在相同的问题。

<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/fragmentListing2"
    android:fitsSystemWindows="true">

    <android.support.v4.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/rvToDoList"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <WebView
            android:layout_width="match_parent"
            android:layout_height="fill_parent"
            app:layout_behavior="@string/appbar_scrolling_view_behavior"
            android:id="@+id/lstWebviewDescription">
        </WebView>
    </android.support.v4.widget.NestedScrollView>
    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/lstAppBarLayout"
        android:fitsSystemWindows="true"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

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

            <android.support.v4.view.ViewPager
            android:layout_width="match_parent"
            android:layout_height="200dp"
            android:id="@+id/lstImageAds"
            app:layout_collapseMode="parallax">
            </android.support.v4.view.ViewPager>
                <include
                android:id="@+id/lstToolbar"
                layout="@layout/toolbar"
                android:layout_height="?attr/actionBarSize"
                app:layout_collapseMode="pin"
                android:foregroundGravity="top"
                android:layout_width="match_parent"/>
        </android.support.design.widget.CollapsingToolbarLayout>


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

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

这是堆栈跟踪。 如您所见,原因是在AppBarLayout的深处,并且具有误导性。 我最初认为这是由于NestedScrollView导致的,并且继续进行研究,但是经过大量实验,结果证明它是ViewPager。 另外,请忽略堆栈跟踪的intellij部分。 在Android手机上运行时,顶部的堆栈基本相同。

java.lang.ClassCastException: android.widget.FrameLayout$LayoutParams cannot be cast to android.support.design.widget.CoordinatorLayout$LayoutParams
    at android.support.design.widget.AppBarLayout$ScrollingViewBehavior.updateOffset(AppBarLayout.java:1155)
    at android.support.design.widget.AppBarLayout$ScrollingViewBehavior.onDependentViewChanged(AppBarLayout.java:1150)
    at android.support.design.widget.CoordinatorLayout.offsetChildToAnchor(CoordinatorLayout.java:1341)
    at android.support.design.widget.CoordinatorLayout.dispatchOnDependentViewChanged(CoordinatorLayout.java:1109)
    at android.support.design.widget.CoordinatorLayout$OnPreDrawListener.onPreDraw(CoordinatorLayout.java:1581)
    at android.view.ViewTreeObserver.dispatchOnPreDraw(ViewTreeObserver.java:944)
    at android.view.AttachInfo_Accessor.dispatchOnPreDraw(AttachInfo_Accessor.java:46)
    at com.android.layoutlib.bridge.impl.RenderSessionImpl.render(RenderSessionImpl.java:371)
    at com.android.layoutlib.bridge.Bridge.createSession(Bridge.java:428)
    at com.android.ide.common.rendering.LayoutLibrary.createSession(LayoutLibrary.java:350)
    at com.android.tools.idea.rendering.RenderTask$2.compute(RenderTask.java:510)
    at com.android.tools.idea.rendering.RenderTask$2.compute(RenderTask.java:498)
    at com.intellij.openapi.application.impl.ApplicationImpl.runReadAction(ApplicationImpl.java:888)
    at com.android.tools.idea.rendering.RenderTask.createRenderSession(RenderTask.java:498)
    at com.android.tools.idea.rendering.RenderTask.access$600(RenderTask.java:72)
    at com.android.tools.idea.rendering.RenderTask$3.call(RenderTask.java:610)
    at com.android.tools.idea.rendering.RenderTask$3.call(RenderTask.java:607)
    at com.android.tools.idea.rendering.RenderService.runRenderAction(RenderService.java:366)
    at com.android.tools.idea.rendering.RenderTask.render(RenderTask.java:607)
    at com.android.tools.idea.rendering.RenderTask.render(RenderTask.java:629)
    at org.jetbrains.android.uipreview.AndroidLayoutPreviewToolWindowManager.doRender(AndroidLayoutPreviewToolWindowManager.java:652)
    at org.jetbrains.android.uipreview.AndroidLayoutPreviewToolWindowManager.access$1700(AndroidLayoutPreviewToolWindowManager.java:80)
    at org.jetbrains.android.uipreview.AndroidLayoutPreviewToolWindowManager$7$1.run(AndroidLayoutPreviewToolWindowManager.java:594)
    at com.intellij.openapi.progress.impl.CoreProgressManager$2.run(CoreProgressManager.java:152)
    at com.intellij.openapi.progress.impl.CoreProgressManager.registerIndicatorAndRun(CoreProgressManager.java:452)
    at com.intellij.openapi.progress.impl.CoreProgressManager.executeProcessUnderProgress(CoreProgressManager.java:402)
    at com.intellij.openapi.progress.impl.ProgressManagerImpl.executeProcessUnderProgress(ProgressManagerImpl.java:54)
    at com.intellij.openapi.progress.impl.CoreProgressManager.runProcess(CoreProgressManager.java:137)
    at org.jetbrains.android.uipreview.AndroidLayoutPreviewToolWindowManager$7.run(AndroidLayoutPreviewToolWindowManager.java:589)
    at com.intellij.util.ui.update.MergingUpdateQueue.execute(MergingUpdateQueue.java:320)
    at com.intellij.util.ui.update.MergingUpdateQueue.execute(MergingUpdateQueue.java:310)
    at com.intellij.util.ui.update.MergingUpdateQueue$2.run(MergingUpdateQueue.java:254)
    at com.intellij.util.ui.update.MergingUpdateQueue.flush(MergingUpdateQueue.java:269)
    at com.intellij.util.ui.update.MergingUpdateQueue.flush(MergingUpdateQueue.java:227)
    at com.intellij.util.ui.update.MergingUpdateQueue.run(MergingUpdateQueue.java:217)
    at com.intellij.util.concurrency.QueueProcessor.runSafely(QueueProcessor.java:238)
    at com.intellij.util.Alarm$Request$1.run(Alarm.java:351)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)

我不确定为什么要将ViewPager放在AppBarLayout 它应该在AppBarLayout外部,但在CoordinatorLayout内部。 通过该项目了解如何: :

检查那里的布局: :

  相关解决方案