AppBarLayout中的ViewPager不起作用?

时间:2015-10-24 17:46:15

标签: android android-layout android-design-library

将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)

1 个答案:

答案 0 :(得分:1)

我不确定您将ViewPager放在AppBarLayout内的原因。它应该在AppBarLayout之外但在CoordinatorLayout之内。通过这个项目了解如何:https://github.com/chrisbanes/cheesesquare

查看那里的布局:https://github.com/chrisbanes/cheesesquare/blob/master/app/src/main/res/layout/include_list_viewpager.xml