Android - SwipeRefreshLayout with empty textview

后端 未结 15 1409
悲哀的现实
悲哀的现实 2020-12-05 02:06

I\'ve implemented SwipeRefreshLayout into my app but it can only hold one direct child which should be the listview. I\'m trying to figure out how to add an emp

15条回答
  •  抹茶落季
    2020-12-05 02:54

    Actually, the only think you are missing is having that empty TextView be wrapped with a scrollable container - for example ScrollView. For details, have a look at SwipeRefreshLayout.canChildScrollUp() method and its usage.

    Anyway, back to the point. Here is a successful implementation:

    activity_some.xml

    
    
    
        
    
            
    
            
    
        
    
    
    

    Where your empty.xml is basically anything you wish wrapped with a ScrollView.

    empty.xml

    
    
    
        
    
    
    

    Now in order to get rid of the famous SwipeRefreshLayout refresh-only-when-at-the-top issue, toggle the SwipeRefreshLayout when necessary (Fragment-specific):

    private ViewTreeObserver.OnScrollChangedListener mOnScrollChangedListener;
    
    @Override
    public void onStart() {
        super.onStart();
    
        mOnScrollChangedListener = new ViewTreeObserver.OnScrollChangedListener() {
            @Override
            public void onScrollChanged() {
                int scrollY = mWebView.getScrollY();
                if (scrollY == 0)
                    swipeLayout.setEnabled(true);
                else
                    swipeLayout.setEnabled(false);
    
            }
        };
        swipeLayout.getViewTreeObserver().addOnScrollChangedListener(mOnScrollChangedListener);
    }
    
    @Override
    public void onStop() {
        swipeLayout.getViewTreeObserver().removeOnScrollChangedListener(mOnScrollChangedListener);
        super.onStop();
    }
    

    That's it! Hope it helps! ;)

    Btw, why would you use SwipeRefreshLayout with FrameLayout this way? Because this way you can do smooth transition animations, like crossfade effects, and any of your state views can be swipeable (in case you want a unified fetch/refresh/retry mechanism).

提交回复
热议问题