gpt4 book ai didi

java - 折叠工具栏和 fragment

转载 作者:搜寻专家 更新时间:2023-10-30 21:45:52 25 4
gpt4 key购买 nike

我有一个这样布局的应用程序,mainactivity 包含 2 个带有 fragment 的选项卡,第一个 fragment 有一个可用的回收 View ,我试图在用户向上或向下滚动时添加视差效果,不确定我是否应该将它添加到 fragment 的 xml 或 mainactivity 的 xml,我将它添加到 mainactivity 的 xml 并将 fragment 作为我的 recyclerview,不用说视差不起作用,现在应用程序不起作用打开,我现在得到一个 NullPointerException,但我没有触及代码。请指出正确的方向。

下面的 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>

主 Activity .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)

可以在这里找到完整的日志 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 内的 Fragments 的视差效果。

确保添加:

android:fitsSystemWindows="true"

AppBarLayoutCollapsingToolbarLayout 的属性,以及 Toolbar 的属性:

app:layout_collapseMode="pin"

如果你想添加另一个 Fragment,你可以像这样添加一个 FrameLayout:

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

关于java - 折叠工具栏和 fragment ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31302053/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com