When switch fragment with SwipeRefreshLayout during refreshing, fragment freezes but actually still work

后端 未结 8 1908
温柔的废话
温柔的废话 2020-11-28 04:31

UPDATE: I thought it worked correctly. But after some test trouble still exists *sniff*

Then I made a simpler version to see what exactly happen and

8条回答
  •  一整个雨季
    2020-11-28 04:39

    For the time being, you can avoid the issue by:

    public class FixedRefreshLayout extends SwipeRefreshLayout {
    
        private static final String TAG = "RefreshTag";
        private boolean selfCancelled = false;
    
        public FixedRefreshLayout(Context context) {
            super(context);
        }
    
        public FixedRefreshLayout(Context context, AttributeSet attrs) {
            super(context, attrs);
        }
    
        @Override
        protected Parcelable onSaveInstanceState()
        {
            if(isRefreshing()) {
                clearAnimation();
                setRefreshing(false);
                selfCancelled = true;
                Log.d(TAG, "For hide refreshing");
            }
            return super.onSaveInstanceState();
        }
    
        @Override
        public void setRefreshing(boolean refreshing) {
            super.setRefreshing(refreshing);
            selfCancelled = false;
        }
    
        @Override
        public void onWindowFocusChanged(boolean hasWindowFocus) {
            super.onWindowFocusChanged(hasWindowFocus);
            if(hasWindowFocus && selfCancelled) {
                setRefreshing(true);
                Log.d(TAG, "Force show refreshing");
            }
        }
    }
    

    This has the added advantage of resuming the animation incase you decide to not switch the fragment (coming from some menu). It also ties in with the internal animation to make sure that the refreshing animation does not return if the network refresh has already completed.

提交回复
热议问题