简体   繁体   中英

Set pull to refresh as refreshing on start or resume fragment

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


}

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM