2

I used Android-PullToRefresh from chrisbanes to show refresh progress in my app.

I load data in AsyncTask, so I start refreshing by calling mPullRefreshListView.setRefreshing();
in onPreExecute method

and stop refreshing by calling mPullRefreshListView.onRefreshComplete();
in onPostExecute method.

That works fine. But the problem happens when network down. Because I won`t use AsyncTask when network down any more, instead I load local cache.

I don`t want to show the PullToRefresh animations when refreshing if network down, but since it registered the setOnRefreshListener, every time when user pull down the ListView, it shows the refreshing animations, and never got disappear.

I tried to disable the animation by calling mPullRefreshListView.onRefreshComplete();, but it does`t help.

if (!Utils.isNetworkOn()) {
                if (mPullRefreshListView.isRefreshing())
                    mPullRefreshListView.onRefreshComplete();
            }

I feel very confused about this, anyone knows how to handle this?
Thanks in advance.

Logan Guo
  • 865
  • 4
  • 17
  • 35
  • You can probably hack it by setting the "theseChildsArePullable" (or whatever it's called) on the pullToRefresh to null when your network goes down and then re-instating it when the network is online. Haven't tried though. – kha Dec 19 '14 at 10:20

1 Answers1

1

It is better to use SwipeRefreshLayout since Chris Bane's project is no longer maintained:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <android.support.v4.widget.SwipeRefreshLayout
        android:id="@+id/srl_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <ListView
            android:id="@+id/list"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    </android.support.v4.widget.SwipeRefreshLayout>
</LinearLayout>

In fragment:
private void pullToRefreshForConfiguredList(View view) {

        mConfiguredSwipeRefreshLayout = (SwipeRefreshLayout) view
                .findViewById(R.id.srl_layout);
        mConfiguredSwipeRefreshLayout
        .setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {

            @Override
            public void onRefresh() {
                // call Method to be refreshed

if (mConfiguredSwipeRefreshLayout.isRefreshing())
                        mConfiguredSwipeRefreshLayout.setRefreshing(false);
            }
        });
        mConfiguredSwipeRefreshLayout.setColorScheme(android.R.color.black,
                R.color.red, android.R.color.black, R.color.red);
    }
Jamal
  • 763
  • 7
  • 22
  • 32
Niveditha
  • 148
  • 1
  • 8