How to run swiperefreshlayout in Android?

I want to run my SwipeRefreshLayout in the onCreateView event of my MainFragment . I want to start loading information in the onCreateView event, and at the same time I will show you the SwipeRefreshLayout update. 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 gradle build

 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 installed the Android SDK Build Tools 21.0.2

+5
android android-studio appcompat swiperefreshlayout
Oct 22 '14 at 17:19
source share
3 answers

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

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

Here mSwipeRefreshLayout is the SwipeRefreshLayout . If you just call:

  mSwipeRefreshLayout.setRefreshing(true); 

this will not cause the circle to be animated, so by adding the above line, you just delay the user interface stream so that it shows the animation of the environment inside the user interface stream.

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

+18
Jan 17 '15 at 17:43
source share

After calling swpRfrshLyt.setRefreshing(true); you need to actually call a function that updates. It is not called automatically. Better yet, create a function that does this for you.

UPDATE

DO NOT USE THIS SOLUTION - read below

The rotator is located behind the action bar, so you need to click it.

 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 solution above. The best decision:

 getSwipeRefreshLayout().post(new Runnable() { @Override public void run() { getSwipeRefreshLayout().setRefreshing(true); } }); 
+5
29 Oct '14 at 3:46
source share

In addition to the accepted answer, use the following code:

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

Explanation I implemented the solution (using fragments), but after running swipeReferesh it never disappeared even when swipeContainer.setRefreshing(false); called swipeContainer.setRefreshing(false); so I had to implement the code above that solved my problem. more ideas why this is happening are welcome.

Hello,

PS: com.android.support:appcompat-v7:22.2.1

+2
Nov 01 '15 at 7:16
source share



All Articles