折叠工具栏和片段 [英] Collapsing Toolbar and Fragment

查看:119
本文介绍了折叠工具栏和片段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个像这样布局的应用程序,mainactivity托管2个标签,附带片段,第一个片段有一个有效的回收器视图,我试图在人向上或向下滚动时添加视差效果,我不确定是否应该将它添加到片段的xml或mainactivity的xml中,我将它添加到mainactivity的xml中并将片段作为我的recyclerview包含,不用说视差不起作用,现在应用程序没有打开,我现在得到一个NullPointerException,但我没有碰到代码。请指出我正确的方向。

I have an app that is laid out like this, the mainactivity hosts 2 tabs with accompanied fragments, the first fragment has a recycler view that works, I'm trying to add the parallax effect as the person scrolls up or down, wasn't sure if I should add it to the fragment's xml or the mainactivity's xml, I added it to the mainactivity's xml and included the fragment as my recyclerview, needless to say that the parallax didn't work and now the app doesn't open and I get a NullPointerException now but I didn't touch the code.Please point me in the correct direction.

XML下面

frag1.xml

frag1.xml

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

<android.support.v7.widget.RecyclerView
        android:id="@+id/my_recycler_view"
        android:scrollbars="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>


</RelativeLayout>

MainActivity.xml

MainActivity.xml

<android.support.design.widget.CoordinatorLayout
    android:id="@+id/main_layout"
    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"
    tools:context=".MainActivity">

    <android.support.design.widget.AppBarLayout
        android:layout_height="100dp"
        android:layout_width="match_parent"
        android:fitsSystemWindows="true"
        >

        <android.support.design.widget.CollapsingToolbarLayout
            app:layout_scrollFlags="scroll|exitUntilCollapsed"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fitsSystemWindows="true">

            <ImageView
                android:layout_height="match_parent"
                android:layout_width="match_parent"
                app:layout_collapseMode="parallax"
                />



            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentTop="true"
                android:background="@color/ColorPrimary"
                android:elevation="6dp"
                android:minHeight="?attr/actionBarSize"
                android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
                app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
                app:layout_collapseMode="pin"/>





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

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


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">


        <android.support.design.widget.TabLayout
            android:id="@+id/tab_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/toolbar"
            android:background="?attr/colorPrimary"
            android:elevation="6dp"
            android:minHeight="?attr/actionBarSize"
            android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
            app:layout_scrollFlags="enterAlways"
            />

        <android.support.v4.view.ViewPager
            android:id="@+id/pager"
            android:layout_width="match_parent"
            android:layout_height="fill_parent"
            android:layout_below="@id/tab_layout"
            app:layout_behavior="@string/appbar_scrolling_view_behavior"/>




        </LinearLayout>


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

崩溃日志摘录

07-08 19:43:54.671    2264-2264/com.example.dimple.test E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.dimple.test, PID: 2264
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.RecyclerView$LayoutManager.onMeasure(android.support.v7.widget.RecyclerView$Recycler, android.support.v7.widget.RecyclerView$State, int, int)' on a null object reference
        at android.support.v7.widget.RecyclerView.onMeasure(RecyclerView.java:1764)
        at android.view.View.measure(View.java:17430)
        at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:727)
        at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:463)
        at android.view.View.measure(View.java:17430)

可在此处找到完整日志< a href =http://pastebin.com/4cPGTx2z> http:// pastebin。 com / 4cPGTx2z

Full log can be found here http://pastebin.com/4cPGTx2z

推荐答案

你想设置这样的东西:

<android.support.design.widget.CoordinatorLayout>

    <android.support.design.widget.AppBarLayout>

    <android.support.design.widget.CollapsingToolbarLayout
        app:layout_scrollFlags="scroll|exitUntilCollapsed">

            <ImageView
                app:layout_collapseMode="parallax"/>

            <android.support.v7.widget.Toolbar />

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

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

    <LinearLayout
        app:layout_behavior="@string/appbar_scrolling_view_behavior" >

        <android.support.design.widget.TabLayout />
        <android.support.v4.view.ViewPager />

    </LinearLayout>

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

这将为您提供碎片的视差效果 ViewPager 内。

确保添加:


android:fitsSystemWindows =true

android:fitsSystemWindows="true"

两者的属性 AppBarLayout CollapsingToolbarLayout ,此属性为工具栏

Attribute to both AppBarLayout and CollapsingToolbarLayout, and this attribute to the Toolbar:


app:layout_collapseMode =pin

app:layout_collapseMode="pin"

如果你想添加另一个片段你可以像这样添加 FrameLayout

If you want to add another Fragment you can add a FrameLayout like so:

<android.support.design.widget.CoordinatorLayout>

    <android.support.design.widget.AppBarLayout>
        <!-- CollapsingToolbarLayout -->
    </android.support.design.widget.AppBarLayout>

    <LinearLayout >
        <android.support.design.widget.TabLayout />
        <android.support.v4.view.ViewPager />
    </LinearLayout>

    <FrameLayout />

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

这篇关于折叠工具栏和片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆