Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set pull to refresh as refreshing on start or resume fragment

Tags:

android

I am using this library Android-PullToRefresh: https://github.com/chrisbanes/Android-PullToRefresh

I need to implement automatic 'pull to refresh' on activity start, that is having the same visual and functional effect as pulling down the fragment just triggered automatically instead of user pull gestures. Do you know if I can do this ?

EDIT: My code is as below

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup group, Bundle saved)
{
    View view = inflater.inflate(R.layout.layout_frg_summarized, group, false);
    mPullToRefreshLayout = (PullToRefreshLayout) view.findViewById(R.id.summary_layout);
    ActionBarPullToRefresh.from(getActivity())
    .allChildrenArePullable()
    .listener(this)
    .setup(mPullToRefreshLayout);   

    return view;

}


@Override
public void onActivityCreated (Bundle savedInstanceState)
{
    super.onActivityCreated(savedInstanceState);

    //some code here

    mPullToRefreshLayout.isRefreshing();

}

@Override
public void onResume(){
    super.onResume();

    if (mPullToRefreshLayout.isRefreshing()) {
     mPullToRefreshLayout.setRefreshing(true);
    } 

}


@Override
public void onPause(){
    super.onPause();
    mPullToRefreshLayout.setRefreshing(false);
    if (mPullToRefreshLayout.isRefreshing()) {
        mPullToRefreshLayout.setRefreshComplete();
    } 


}
like image 891
James Wahome Avatar asked Jan 08 '14 21:01

James Wahome


1 Answers

Yes, you can.

Similar to having the ability to call onRefreshComplete(), there is a setRefreshing() method:

myRefreshableView.setRefreshing();

Edit:

It looks like onRefreshStarted is called when a user-initiated refresh is started. This means if you want to initiate your own refresh from code, you have to update the UI and start the refresh yourself. I don't see your code for starting the download, but you'd have to pull the contents of that method out to another method, something like this:

@Override
public void onRefreshStarted(View view) {
    startDownload();
}

Then, when you want to start a download in code (for example in onResume):

@Override
public void onResume(){
    super.onResume();
    mPullToRefreshLayout.setRefreshing(true);
    startDownload();
}

Your call to isRefreshing() in onActivityCreated does nothing - that's a check to confirm whether the UI is currently in 'refreshing' state or not. Additionally, you're using that check in your current onResume code to perform a call that doesn't actually do anything - if the layout is refreshing, you set it to be refreshing.

like image 139
Adam S Avatar answered Sep 18 '22 12:09

Adam S