How to trigger swiperefreshlayout in android?

℡╲_俬逩灬. 提交于 2020-01-02 04:35:13

问题


I want to trigger my SwipeRefreshLayout in the event onCreateView of my MainFragment. What I want to do is start downloading the information in the event onCreateView and at the same time I show the SwipeRefreshLayout refreshing. Any ideas?

Here is my code in my Java code in MainFragment

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_main, container, false);
        SwipeRefreshLayout swpRfrshLyt= (SwipeRefreshLayout) rootView.findViewById(R.id.swpRfrshLyt);

        swpRfrshLyt.setColorSchemeResources(R.color.holo_blue_light,
                R.color.holo_green_light,
                R.color.holo_orange_light,
                R.color.holo_red_light);

        swpRfrshLyt.setOnRefreshListener(this);
        swpRfrshLyt.setRefreshing(true);
        return rootView;
    }

Here is my fragment_main.xml

<android.support.v4.widget.SwipeRefreshLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/swpRfrshLyt"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ListView
        android:id="@+id/lstvw_items"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</android.support.v4.widget.SwipeRefreshLayout>

I am using Android Studio, here is my build gradle

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:+'
    compile 'com.android.support:support-v4:+'
    compile 'com.mcxiaoke.volley:library:1.0.+'
}

I have installed Android SDK Build Tools 21.0.2


回答1:


There is a better solution to achieve this by using the following:

mSwipeRefreshLayout.post(new Runnable() {
    @Override public void run() {
        mSwipeRefreshLayout.setRefreshing(true);
    }
});

Here mSwipeRefreshLayout is the SwipeRefreshLayout. If you simply call:

 mSwipeRefreshLayout.setRefreshing(true);

it wont trigger the circle to animate, so by adding the above line you just make a delay in the UI thread so that it shows the circle animation inside the UI thread.

By calling mSwipeRefreshLayout.setRefreshing(true) the OnRefreshListener will also get executed so after the task end just add mSwipeRefreshLayout.setRefreshing(false) to stop the circle animation.




回答2:


After calling swpRfrshLyt.setRefreshing(true); you need to actually call the function that does the refresh. It does not get called automatically. Better yet, create a function that does both for you.

UPDATE

DO NOT USE THIS SOLUTION - read below

The spinner is behind the action bar so you have to push it down.

Display display = getActivity().getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int height = size.y;
    getSwipeRefreshLayout().setProgressViewOffset(false, 200, height / 9);

UPDATE 2

I would no longer recommend my above solution. The better solution is:

getSwipeRefreshLayout().post(new Runnable() {
    @Override public void run() {
        getSwipeRefreshLayout().setRefreshing(true);
    }
});



回答3:


In addition to accepted answer use the following code as well:

swipeContainer.post(new Runnable() {
        @Override
        public void run() {
            swipeContainer.setRefreshing(false);
        }
    });

Explanation I implemented the solution (using fragments) but after the swipeReferesh started it never went away even on calling swipeContainer.setRefreshing(false); so had to implement the code given above which solved my problem. any more ideas why this happens are most welcome.

Regards,

P.S.: com.android.support:appcompat-v7:22.2.1



来源:https://stackoverflow.com/questions/26513315/how-to-trigger-swiperefreshlayout-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!