Android: CollapsingToolbarLayout and SwipeRefreshLayout get stuck
Finally,
I found that SwipeRefreshLayout works without any "hacks" from Support Library version 23.1.1.
Simply use in your layout:
<android.support.v4.widget.SwipeRefreshLayout
android:id="@+id/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/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical" />
</android.support.v4.widget.SwipeRefreshLayout>
and in code:
SwipeRefreshLayout swipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipe_refresh_layout);
swipeRefreshLayout.setColorSchemeResources(R.color.green, R.color.red, R.color.yellow);
swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
//Your refresh code here
}
});
And don't forget to use:
swipeRefreshLayout.setRefreshing(false);
after using your code logic ;)
Toolbar not showing with swipe to refresh
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
Stack with swiperefreshlayout with collapsingtoolbarlayout
You can handle listener for AppBarLayout
public abstract class AppBarStateChangeListener implements AppBarLayout.OnOffsetChangedListener {
public enum State {
EXPANDED,
COLLAPSED,
IDLE
}
private State mCurrentState = State.IDLE;
@Override
public final void onOffsetChanged(AppBarLayout appBarLayout, int i) {
if (i == 0) {
if (mCurrentState != State.EXPANDED) {
onStateChanged(appBarLayout, State.EXPANDED);
}
mCurrentState = State.EXPANDED;
} else if (Math.abs(i) >= appBarLayout.getTotalScrollRange()) {
if (mCurrentState != State.COLLAPSED) {
onStateChanged(appBarLayout, State.COLLAPSED);
}
mCurrentState = State.COLLAPSED;
} else {
if (mCurrentState != State.IDLE) {
onStateChanged(appBarLayout, State.IDLE);
}
mCurrentState = State.IDLE;
}
}
public abstract void onStateChanged(AppBarLayout appBarLayout, State state);
}
Android: CoordinatorLayout and SwipeRefreshLayout
Set the scroll behavior to the SwipeRefreshLayout
not the RecyclerView
.
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
android:id="@+id/swipe_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/cardList"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical" />
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
Related Topics
Icon in Menu Not Showing in Android
How to Join Png with Alpha/Transparency in a Frame in Realtime
Firebase Admin Sdk for Android, Methods Not Found
How to Detect Power Connected State
Android 5.0 Material Design Style Navigation Drawer for Kitkat
Disable Logcat Output Completely in Release Android App
One Activity and All Other Fragments
How to Use Holo.Light Theme, and Fall Back to 'Light' on Pre-Honeycomb Devices
Adb Connection by Wifi Getting Killed When a New Usb Attached/Detached
Build Unsigned APK File with Android Studio
What Does It Mean to Inflate a View from an Xml File
How to Round an Image with Glide Library
Sign APK Without Putting Keystore Info in Build.Gradle
How to Make a Actionbar Like Google Play That Fades in When Scrolling
Android:Select Image from Gallery Then Crop That and Show in an Imageview
Android Room Persistent Library - How to Insert Class That Has a List Object Field