Logan Guo
Logan Guo

Reputation: 886

how to stop PullToRefresh animation of ListView

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.

Upvotes: 2

Views: 712

Answers (1)

Niveditha
Niveditha

Reputation: 148

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);
    }

Upvotes: 1

Related Questions