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

后端 未结 8 1909
温柔的废话
温柔的废话 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:33

    Terminate SwipeRefresh whenever navigation menu item is clicked. It worked.

    private void selectItem(int position) {
    
           mSwipeRefreshLayout.setRefreshing(false);
    
           /*
            Other part of the function
           */
    }
    
    0 讨论(0)
  • 2020-11-28 04:35

    Well... After some struggling I eventually solved this problem by myself, in a tricky way...

    I just need to add these in onPause() :

    @Override
    public void onPause() {
        super.onPause();
        ...
    
        if (mSwipeRefreshLayout!=null) {
            mSwipeRefreshLayout.setRefreshing(false);
            mSwipeRefreshLayout.destroyDrawingCache();
            mSwipeRefreshLayout.clearAnimation();
        }
    }
    
    0 讨论(0)
  • 2020-11-28 04:35

    In addition to @zlm2012 answer, I noticed that this issue was reproduced under Support Library 21.0.0, but seems to be fixed right now at 21.0.3

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-11-28 04:40

    It works! Just remove the transition from your fragment replacement, in my case I removed the following from my code:

    .setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
    
    0 讨论(0)
  • 2020-11-28 04:49

    Set clickable="true" in top parent layout... It may solve the problem.

    0 讨论(0)
提交回复
热议问题