Android - SwipeRefreshLayout with empty textview

后端 未结 15 1433
悲哀的现实
悲哀的现实 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:51

    Based on some answers here and the source code of SwipeRefreshLayout, I have subclassed the view to specifically handle having a RecyclerView (or ListView) and also an "empty" view inside a container which is the child.

    It expects a layout such as

    
      
        
        
      
    
    

    The code is:

    public class SwipeRefreshLayoutWithEmpty extends SwipeRefreshLayout {
        private ViewGroup container;
    
        public SwipeRefreshLayoutWithEmpty(Context context) {
            super(context);
        }
    
        public SwipeRefreshLayoutWithEmpty(Context context, AttributeSet attrs) {
            super(context, attrs);
        }
    
        @Override
        public boolean canChildScrollUp() {
            // The swipe refresh layout has 2 children; the circle refresh indicator
            // and the view container. The container is needed here
            ViewGroup container = getContainer();
            if (container == null) {
                return false;
            }
    
            // The container has 2 children; the empty view and the scrollable view.
            // Use whichever one is visible and test that it can scroll
            View view = container.getChildAt(0);
            if (view.getVisibility() != View.VISIBLE) {
                view = container.getChildAt(1);
            }
    
            return ViewCompat.canScrollVertically(view, -1);
        }
    
        private ViewGroup getContainer() {
            // Cache this view
            if (container != null) {
                return container;
            }
    
            // The container may not be the first view. Need to iterate to find it
            for (int i=0; i

    Full gist: https://gist.github.com/grennis/16cb2b0c7f798418284dd2d754499b43

提交回复
热议问题