崩溃的工具栏和片段布局无法一起使用

崩溃的工具栏和片段布局无法一起使用

本文介绍了崩溃的工具栏和片段布局无法一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我有两个片段和一个MainActivity.activity_main.xml包含一个折叠式工具栏,当片段为空时,该工具栏非常适合我.在我的第一个片段中,我从服务器获取json数据,并将其放入片段活动内的listview中.数据已正确获取并显示,但是listview占据了整个屏幕,而我折叠的工具栏似乎有点小虫子,就像有时它弹出并消失了,并且listview不再可滚动了.这两项活动根本无法协同工作.有没有办法让他们一起工作?我的activity_main.xml:

In my app I have two fragments and a MainActivity. The activity_main.xml contains a collapsing toolbar which works perfectly for me when the fragments are empty. In my first fragment I fetch json data from a server and put it into a listview inside the fragment activity. The data is fetched and shown correctly, but the listview takes the whole screen and my collapsing toolbar seems to be a little buggy, like sometimes it pops up and disappears and my listview is not scrollable anymore. The two activities aren’t working together at all. Is there a way to make them work together?My activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.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:fitsSystemWindows="true">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appBar"
        android:layout_width="match_parent"
        android:layout_height="150dp"
        android:fitsSystemWindows="true"
        android:theme="@style/AppTheme.AppBarOverlay">


        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/toolbar_layout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fitsSystemWindows="true"
            app:expandedTitleGravity="center|bottom"
            app:expandedTitleMarginBottom="56dp"
            app:layout_scrollFlags="scroll|exitUntilCollapsed"
            app:toolbarId="@+id/toolbar">


            <ImageView
                android:id="@+id/backg"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:fitsSystemWindows="true"
                android:scaleType="centerCrop"
                android:src="@drawable/backthree"
                app:layout_collapseMode="parallax" />


            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?android:attr/actionBarSize"
                android:layout_marginBottom="48dp"
                android:gravity="right"
                app:layout_collapseMode="pin"
                app:popupTheme="@style/AppTheme.PopupOverlay" />


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

    <FrameLayout
        android:id="@+id/frame_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/navigation"
        android:animateLayoutChanges="true">


        <TextView
            android:id="@+id/textView3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="@android:color/white"
            android:text="TextView" />

        <TextView
            android:id="@+id/textView4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="right"
            android:textColor="@android:color/white"
            android:text="TextView" />

    </FrameLayout>

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

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

        </RelativeLayout>

    </android.support.v4.widget.NestedScrollView>

    <android.support.design.widget.BottomNavigationView
        android:id="@+id/navigation"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_gravity="bottom"
        android:background="@android:color/white"
        app:itemIconTint="@android:color/darker_gray"
        app:itemTextColor="@android:color/darker_gray"
        app:menu="@menu/navigation" >

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

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

我的fragment_first.xml:

My fragment_first.xml:

<RelativeLayout android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/colorGrayHell"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:layout_marginTop="10dp">

        <android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:tools="http://schemas.android.com/tools"
            android:id="@+id/swipe_refresh_layout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            tools:context=".MainActivity">

            <ListView
                android:id="@+id/list"
                android:layout_marginLeft="7dp"
                android:layout_marginRight="7dp"
                android:layout_marginTop="10dp"
                android:layout_width="fill_parent"
                android:background="@android:color/white"
                android:dividerHeight="10dp"
                android:divider="@color/colorGrayHell"
                android:layout_height="wrap_content" />

        </android.support.v4.widget.SwipeRefreshLayout>

    </RelativeLayout>

</RelativeLayout>

推荐答案

您没有提到片段的附加位置-我假设在NestedScrollView中?如果没有,那就是他们应该去的地方.

You haven't mentioned where the fragments are attached - I assume within the NestedScrollView? If not, that's where they should go.

问题很可能是片段中的ListView-与RecyclerView不同,它没有实现折叠工具栏功能所需的NestedScrollingChild2和ScrollingView.

The problem is most likely the ListView within the fragment - unlike a RecyclerView, it does not implement NestedScrollingChild2 and ScrollingView, which are required for the collapsing toolbar functionality.

用RecyclerView替换ListView,并确保片段在NestedScrollView中,该片段具有 app:layout_behavior ="@ string/appbar_scrolling_view_behavior" ,并且可以正常工作.

Replace the ListView with a RecyclerView and make sure the fragments are within the NestedScrollView that has app:layout_behavior="@string/appbar_scrolling_view_behavior" and it should work.

这篇关于崩溃的工具栏和片段布局无法一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-04 22:22