wiseindy
wiseindy

Reputation: 19655

How to use ChrisBanes' ActionBar-PullToRefresh library with Fragments having GridViews

I am trying to use Chris Banes' library Actionbar-PullToRefresh. It can be found here.

I am using Tabs + ViewPager + Fragments in my app.

The problem I'm facing is that my fragment has a GridView and I cannot figure out how to use this library to work with it.

I read through the sample code. He says that all you have to do is, wrap your refreshable view in a PullToRefreshLayout like this:

<uk.co.senab.actionbarpulltorefresh.library.PullToRefreshLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/ptr_layout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <!-- Your content, here we're using a ScrollView -->

    <ScrollView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
    </ScrollView>

</uk.co.senab.actionbarpulltorefresh.library.PullToRefreshLayout>

This works great for stuff like ListView, ScrollView, GridView, etc. However, apparently this will not work for Fragments (Tabs & ViewPagers). Now, in the sample code he has wrapped the refreshable fragment with a ScrollView INSTEAD of a PullToRefreshLayout.

I cannot do this because my Fragment 1 (under tab 1) has a GridView. Now I cannot add a GridView to a ScrollView because that just wouldn't make sense.

For example, if I put my GridView inside the ScrollView as shown below, it just doesn't make sense:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/ptr_scrollview"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:fillViewport="true"
    android:scrollbarStyle="outsideInset" >

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="#FFF000" >

        <!-- MY GRID VIEW -->

        <GridView
        ...
        />

    </RelativeLayout>
</ScrollView>

The above code works. Sort of. (I need to disable scrolling of my GridView and use an ExpandableGridView to get it working properly... which seems like overkill & I'm not even sure if that would work).

If I replace the ScrollView wrapper with anything else like PullToRefreshLayout or any other layout, the refreshing doesn't work.

What to do? How to get my layout to work with this library without wrapping a ScrollView around it?

I hope I was clear enough. Please let me know if you need any more info. I tried to explain it the best I could.

Thanks!

Upvotes: 1

Views: 3103

Answers (1)

DejanRistic
DejanRistic

Reputation: 2039

I had a similar issue trying to get it to work with a ListView that I had in one of my tabs.

I solved my issue by using the PullToRefreshAttacher instead of using the layout.

In your Activity that is controlling the ViewPager for the fragments, initialize a PullToRefreshAttacher in onCreate or an init method.

mPullToRefreshAttacher = PullToRefreshAttacher.get(this);

Next make a public method that allows access to the attacher that you just initialized.

public PullToRefreshAttacher getPullToRefreshAttacher() {
    return mPullToRefreshAttacher;
}

Then in the fragment you want the refresh functionality.

mPullToRefreshAttacher = ((MainTabActivity) getActivity())
            .getPullToRefreshAttacher();

mPullToRefreshAttacher.addRefreshableView(activeListView, this);

Except in your case activeListView would be the reference to your GridView instead of a ListView.

Then make sure your fragment implements OnRefreshListener so you can handle the Refresh.

I have not actually tested this with a GridView so let me know if it works.

Good Luck!

Upvotes: 3

Related Questions