1

I currently have a SwipeRefreshLayout that isn't calling its OnRefresh method whenever I pull down. Is something wrong with my code?

public class MainActivity extends AppCompatActivity {
private SwipeRefreshLayout swipe_view;

@Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    swipe_view = (SwipeRefreshLayout) findViewById(R.id.this_will_work);

    swipe_view.setOnRefreshListener(new OnRefreshListener()
    {
        @Override
        public void onRefresh()
        {
            Toast.makeText(getApplicationContext(), "OnRefresh() test", Toast.LENGTH_SHORT).show();
        }
    });

1 Answer 1

2

You should have a service or something that will be updated like below :

private void refresh() {
    startService(new Intent(this, UpdaterService.class));
}

then Refresh :

mSwipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipe_refresh_layout);
    mSwipeRefreshLayout.setOnRefreshListener(this);
    mSwipeRefreshLayout.post(new Runnable() {
                                @Override
                                public void run() {
                                    mSwipeRefreshLayout.setRefreshing(true);
                                    refresh();
                                }});

//here refreshed Item
   //getLoaderManager().initLoader(0, null, this);

    if (savedInstanceState == null) {
        refresh();
    }

and your onRefresh() should contain the same method refresh().

7
  • this throws an error saying: Wrong 3rd argument type. Found: 'my.app.name.MainActivity', required: 'android.app.LoaderManager.LoaderCallbacks<java.lang.Object>'
    – Pike D.
    Mar 12, 2017 at 6:57
  • 1
    you may hash this line if you are not using onLoadFinish getLoaderManager().initLoader(0, null, this);
    – user6490462
    Mar 12, 2017 at 6:58
  • Still, whenever I am pulling down to refresh none of my methods are called. Also setColorSchemeResources() doesn't change colors when called, so at this point, I think something is broken with my app. +1 Though for trying to help me
    – Pike D.
    Mar 12, 2017 at 7:03
  • 1
    you should put your refreshed item instead of comment i wrote, and if you are retrieve data online or some thing the updaterservice will update it.
    – user6490462
    Mar 12, 2017 at 7:05
  • 1
    above this line put your refreshed item if (savedInstanceState == null)
    – user6490462
    Mar 12, 2017 at 7:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.