Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
SwipeRefreshLayout with StickyListHeaders
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v4.widget.SwipeRefreshLayout
android:id="@+id/swipe_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true">
<se.emilsjolander.stickylistheaders.StickyListHeadersListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</android.support.v4.widget.SwipeRefreshLayout>
</RelativeLayout>

Trying to find a solution

StickyListHeaders

SwipeRefreshLayout

I had a problem getting the new fancy SwipeRefreshLayout from the appcompat lib to work with a custom listview, in this case the StickyListHeaders. Since the First child of the SwipeRefreshLayout should be either a ScrollView or a pure List, some workaround had to be done.

Hope this helps.

package com.test.application;
import android.app.Fragment;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.ArrayAdapter;
import com.test.application.R;
import se.emilsjolander.stickylistheaders.StickyListHeadersAdapter;
import se.emilsjolander.stickylistheaders.StickyListHeadersListView;
public class ThatListFragment extends Fragment implements SwipeRefreshLayout.OnRefreshListener {
private StickyListHeadersListView mStickyList;
private SwipeRefreshLayout mSwipeLayout;
/**
* Mandatory empty constructor for the fragment manager to instantiate the
* fragment (e.g. upon screen orientation changes).
*/
public ThatListFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_that_list, container, false);
// Find the StickyListHeadersListView
mStickyList = (StickyListHeadersListView) view.findViewById(android.R.id.list);
// Find the SwipeRefreshLayout
mSwipeLayout = (SwipeRefreshLayout) view.findViewById(R.id.swipe_container);
// This will not work, you have to create your own StickyListHeadersAdapter
ArrayAdapter mListAdapter = new StickyListHeadersAdapter(USE_YOUR_OWN);
// Set the adapter
mStickyList.setAdapter(mListAdapter);
// The fix to get the SwipeRefreshLayout to work properly with a sectioned header
mStickyList.setOnScrollListener(new AbsListView.OnScrollListener() {
@Override
public void onScrollStateChanged(AbsListView absListView, int i) {
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
// The magic lays here. Get the listwrappers top point.
View childView = mStickyList.getWrappedList().getChildAt(0);
int top = (childView == null) ? 0 : childView.getTop();
// If at the absolute top then enable the SwipeRefreshLayout
if (top >= 0) {
mSwipeLayout.setEnabled(true);
} else {
mSwipeLayout.setEnabled(false);
}
}
});
// INIT SWIPE REFRESH LAYOUT
mSwipeLayout.setOnRefreshListener(this);
mSwipeLayout.setColorScheme(android.R.color.holo_blue_bright,
android.R.color.holo_green_light,
android.R.color.holo_orange_light,
android.R.color.holo_red_light);
return view;
}
// SWIPEREFRESHLAYOUT
@Override
public void onRefresh() {
// Do the refreshing of the list.
// Disable the refreshing animation
mSwipeLayout.setRefreshing(false);
}
}
@KarthickCk

This comment has been minimized.

Copy link

commented Apr 9, 2014

Swipe refresh is not working when i add empty view for that list view

@Frikish

This comment has been minimized.

Copy link
Owner Author

commented Apr 9, 2014

I'm sorry, but what do you mean by "adding an empty view for that list view"?

Did you set up the StickyListHeaders correctly? Do the list work correctly when there is no SwipeRefreshLayout?

@dvdrlee

This comment has been minimized.

Copy link

commented Apr 21, 2014

Thanks for this!

@aeatho

This comment has been minimized.

Copy link

commented Apr 23, 2014

the swpierefreshlayout isnot woking in frament!

@zokipirlo

This comment has been minimized.

Copy link

commented May 6, 2014

Thanks for that. You should also check if firstVisibleItem is 0, because childAt(0) returns first visible item. I'm using that with normal ListView.
if (firstVisibleItem == 0 && top >= 0)

@jgnt32

This comment has been minimized.

Copy link

commented Jul 7, 2014

public class StickyRefreshLayout extends SwipeRefreshLayout {
....
public boolean canChildScrollUp() {
    if(mListView == null){
        mListView =  ((StickyListHeadersListView) getChildAt(0)).getWrappedList();
    }

    if(mListView.getFirstVisiblePosition() == 0){
        View childView = mListView.getChildAt(0);
        int top = (childView == null) ? 0 : childView.getTop();
        return top < 0;
    } else {
        return true;
    }

}

thats better

@Twibit

This comment has been minimized.

Copy link

commented Sep 25, 2014

Thanks, that solve my problem.

@ruslancpp

This comment has been minimized.

Copy link

commented Jul 24, 2015

Many Thanks.

@FireZenk

This comment has been minimized.

Copy link

commented Mar 8, 2016

Thank you @jgnt32

@JianxunRao

This comment has been minimized.

Copy link

commented Feb 23, 2017

Thanks.This helps me a lot.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.