Toolbar not showing with swipe to refresh

牧云@^-^@ 提交于 2019-12-03 16:55:54

问题


I'm trying to implement collapsing tollbar with swipe to refresh and recyclerview. When I'm trying to scroll (when recyclerview has only one item) toolbar collapse, but when I'm trying to scroll down to show toolbar, it's impossible because swipe down causes swipe to refresh. When recyclerview has more item it works perfectly.

Link to gif with problem

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent">

<android.support.design.widget.AppBarLayout
    android:id="@+id/appbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:elevation="1dp"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        android:elevation="1dp"
        android:popupTheme="@style/ThemeOverlay.AppCompat.Light"
        app:layout_scrollFlags="scroll|enterAlways" />


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

    <android.support.v4.widget.SwipeRefreshLayout
        android:id="@+id/activity_main_swipe_refresh_layout"
        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/cities_list"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

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

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

<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="end|bottom"
    android:layout_margin="16dp"
    android:elevation="1dp"
    android:onClick="addCity"
    android:src="@drawable/ic_plus_white_36dp"
    app:borderWidth="0dp" />


回答1:


Update: This bug has been fixed in the version 23.1.1 of support library

You can set onOffsetChanged listener for your AppBarLayout and prevent to swipe refreshing until AppBarLayout layout offset 0.

This is good example : https://gist.github.com/blackcj/001a90c7775765ad5212




回答2:


I managed it by adding the following implementation of OnOffsetChangedListener in fragment:

@Override
public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
    if (collapsingToolbarLayout.getHeight() + verticalOffset < 2 * ViewCompat.getMinimumHeight(collapsingToolbarLayout)) {
        swipeRefreshLayout.setEnabled(false);
    } else {
        swipeRefreshLayout.setEnabled(true);
    }
}

@Override
public void onResume() {
    super.onResume();
    appBarLayout.addOnOffsetChangedListener(this);
}

@Override
public void onPause() {
    super.onPause();
    appBarLayout.removeOnOffsetChangedListener(this);
}


来源:https://stackoverflow.com/questions/30751875/toolbar-not-showing-with-swipe-to-refresh

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!