【问题标题】:CollapsingTolbar work with fagment contain SwipeRefreshLayout and RecycleView折叠工具栏使用片段包含 SwipeRefreshLayout 和 RecycleView
【发布时间】:2016-08-22 15:53:23
【问题描述】:

您好,我正在处理 CollapsingToobarFramelayout。当有transaction 时,Fragment 将替换FrameLayoutfragment 包含 SwipeRefreshLayoutRecycleView 内部。但是每当我输入swipeRefreshLayout 时,RecycleView 就不会出现(它在没有SwipeRefreshLayout 的情况下正常工作)。

这是我的 xml 活动 activity_topstory_detail

<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"
    tools:context=".TopStoryDetailActivity"
    tools:ignore="MergeRootFrame">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/app_bar"
        android:layout_width="match_parent"
        android:layout_height="@dimen/app_bar_height"
        android:fitsSystemWindows="true"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/toolbar_layout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fitsSystemWindows="false"
            app:collapsedTitleTextAppearance="@style/TextAppearance.AppCompat.Title"
            app:contentScrim="?attr/colorPrimary"
            app:expandedTitleTextAppearance="@style/TextAppearance.AppCompat.Title"
            app:layout_scrollFlags="scroll|exitUntilCollapsed"
            app:toolbarId="@+id/toolbar">

            <android.support.v7.widget.Toolbar
                android:id="@+id/detail_toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                app:layout_collapseMode="parallax"
                app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

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

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


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

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

片段TopStoryDetailFragment的布局

<RelativeLayout

    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/topStory_detail"
    style="?android:attr/textAppearanceLarge"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="5dp"
    android:textIsSelectable="true"
    tools:context=".TopStoryDetailFragment">

    <ProgressBar
        android:id="@+id/progressBar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true" />

    <include layout="@layout/comment_list" />

</RelativeLayout>

还有我的comment_list 布局文件

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

    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"
    style="?android:attr/textAppearanceLarge"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v4.widget.SwipeRefreshLayout

        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:padding="10dp"
        android:id="@+id/swipeContainer"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <android.support.v7.widget.RecyclerView
            android:id="@+id/comment_list"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            tools:listitem="@layout/comment_list_content"/>

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

有什么方法可以让SwipeRefreshLayoutRecycleViewCollapsingToolbar 一起工作?非常感谢任何帮助。谢谢

【问题讨论】:

    标签: android android-recyclerview android-collapsingtoolbarlayout swiperefreshlayout


    【解决方案1】:

    对于我目前正在开发的应用,我得到了一个使用 SwipeRefreshLayout、RecyclerView 和 CollpasingToolbar 的布局。

    这是主布局

    <android.support.v4.widget.DrawerLayout 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:id="@+id/drawer"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true">
    
        <android.support.design.widget.CoordinatorLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/coordinator">
    
            <include layout="@layout/toolbar"/>
    
            <android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:id="@+id/swipeContainer"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                app:layout_behavior="@string/appbar_scrolling_view_behavior">
    
    
                <android.support.v7.widget.RecyclerView
                    android:id="@+id/appList"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
               />
            </android.support.v4.widget.SwipeRefreshLayout>
        </android.support.design.widget.CoordinatorLayout>
    
        <android.support.design.widget.NavigationView
            android:id="@+id/navigation_view"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            app:itemIconTint="#2196f3"
            app:itemTextColor="#009688"
            app:headerLayout="@layout/header"
            app:menu="@menu/drawer" />
    </android.support.v4.widget.DrawerLayout>
    

    这是折叠工具栏布局:

    <?xml version="1.0" encoding="utf-8"?>
    
    <android.support.design.widget.AppBarLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/MyAppbar"
        android:layout_width="match_parent"
        android:layout_height="256dp"
        android:fitsSystemWindows="true"
        app:theme="@style/ThemeOverlay.AppCompat.Dark"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light" >
    
        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/collapse_toolbar"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@color/material_deep_teal_500"
            android:fitsSystemWindows="true"
            app:layout_scrollFlags="scroll|exitUntilCollapsed">
    
    
            <ImageView
                android:id="@+id/image"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal|center_vertical"
                android:adjustViewBounds="true"
                android:transitionName="@string/transition_app_icon"
                android:fitsSystemWindows="true"/>
    
            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                app:layout_collapseMode="pin"/>
    
        </android.support.design.widget.CollapsingToolbarLayout>
    
    </android.support.design.widget.AppBarLayout>
    

    也许有帮助

    【讨论】:

    • 嗨,非常感谢您的帮助。我正在使用一个包含 swipeRefreshLayout 和回收视图的片段。发现把swipeRefreshLayout放在main activity和nestedscroll view里面,一个FrameLayout放在嵌套scroll view里面。为 frameview 进行片段事务。在片段中调用父布局 swipeRefreshLayout 使其工作。不知道是不是这样的好办法
    【解决方案2】:

    我认为你可以使用 SwipeRefreshLayout、RecycleView 和 CollapsingToolbar。我查看了您的代码,问题是您将 NestedScrollView 与 recyclerview 一起使用。 只需删除 NestedScrollview

    【讨论】:

    • 认为我们需要保留嵌套滚动视图,否则折叠工具栏将无法正常工作
    • 我认为解决问题必须删除滚动视图,collapibletoolbar 工具栏将起作用,如果没有,您可以通过编程方式进行管理
    【解决方案3】:

    对于任何需要片段来使用折叠工具栏、SwipeRefreshLayout、RecycleView 的人。我找到了一种将 SwipeRefreshLayout 放在主要活动上的方法,里面有一个nestedScrollView,一个 FrameLayout 里面有嵌套的 ScrollView。在 Fragment OnCreatedView 中,调用活动 swipeRefreshLayout。不知道这是否是一个好方法,但它有效

    我的活动布局:

    <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"
        tools:context=".TopStoryDetailActivity"
        tools:ignore="MergeRootFrame">
    
        <android.support.design.widget.AppBarLayout
            android:id="@+id/app_bar"
            android:layout_width="match_parent"
            android:layout_height="@dimen/app_bar_height"
            android:fitsSystemWindows="true"
            android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
    
            <android.support.design.widget.CollapsingToolbarLayout
                android:id="@+id/toolbar_layout"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:fitsSystemWindows="true"
                app:collapsedTitleTextAppearance="@style/TextAppearance.AppCompat.Title"
                app:contentScrim="?attr/colorPrimary"
                app:expandedTitleTextAppearance="@style/TextAppearance.AppCompat.Title"
                app:layout_scrollFlags="scroll|exitUntilCollapsed"
                app:toolbarId="@+id/toolbar">
    
                <android.support.v7.widget.Toolbar
                    android:id="@+id/detail_toolbar"
                    android:layout_width="match_parent"
                    android:layout_height="?attr/actionBarSize"
                    app:layout_collapseMode="pin"
                    app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
    
            </android.support.design.widget.CollapsingToolbarLayout>
    
        </android.support.design.widget.AppBarLayout>
    
        <android.support.v4.widget.SwipeRefreshLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:padding="10dp"
            android:id="@+id/swipeContainer"
            app:layout_behavior="@string/appbar_scrolling_view_behavior">
        <android.support.v4.widget.NestedScrollView
            android:id="@+id/topStory_detail_nested_scrollview"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_behavior="@string/appbar_scrolling_view_behavior">
            <FrameLayout
                android:id="@+id/topStory_detail_container"
                android:layout_width="match_parent"
                android:layout_height="match_parent">
            </FrameLayout>
        </android.support.v4.widget.NestedScrollView>
        </android.support.v4.widget.SwipeRefreshLayout>
    
    </android.support.design.widget.CoordinatorLayout>
    

    我的片段布局

    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/topStory_detail"
    style="?android:attr/textAppearanceLarge"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="5dp"
    android:textIsSelectable="true"
    tools:context=".TopStoryDetailFragment">
    
    <ProgressBar
        android:id="@+id/progressBar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true" />
    
    <include layout="@layout/comment_list" />
    

    我的评论列表布局

    <RelativeLayout
    
        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"
        style="?android:attr/textAppearanceLarge"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
            <android.support.v7.widget.RecyclerView
                android:id="@+id/comment_list"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                tools:listitem="@layout/comment_list_content"/>
    
    </RelativeLayout>
    

    我通过调用 swipeRefreshLayout

    @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.topstory_detail, container, false);
    
            // Lookup the swipe container view
            swipeContainer = (SwipeRefreshLayout) getActivity().findViewById(R.id.swipeContainer);
    

    【讨论】:

      猜你喜欢
      • 2015-09-26
      • 2018-09-24
      • 1970-01-01
      • 2018-08-21
      • 1970-01-01
      • 2016-11-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多