SwipeRefreshLayout采坑笔记

SwipeRefreshLayout有个bug(好吧,这边见仁见智了):

xml文件:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="alink.swiperefreshlayoutdemo.MainActivity">

    <android.support.v4.widget.SwipeRefreshLayout
        android:id="@+id/swiperefresh"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <ScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical">

                <Button
                    android:layout_width="match_parent"
                    android:layout_height="100dp"
                    android:text="GO" />

                <Button
                    android:layout_width="match_parent"
                    android:layout_height="100dp"
                    android:text="GO" />

                <Button
                    android:layout_width="match_parent"
                    android:layout_height="100dp"
                    android:text="GO" />

                <Button
                    android:layout_width="match_parent"
                    android:layout_height="100dp"
                    android:text="GO" />

                <Button
                    android:layout_width="match_parent"
                    android:layout_height="100dp"
                    android:text="GO" />

                <Button
                    android:layout_width="match_parent"
                    android:layout_height="100dp"
                    android:text="GO" />

                <Button
                    android:layout_width="match_parent"
                    android:layout_height="100dp"
                    android:text="GO" />

                <Button
                    android:layout_width="match_parent"
                    android:layout_height="100dp"
                    android:text="GO" />

                <Button
                    android:layout_width="match_parent"
                    android:layout_height="100dp"
                    android:text="GO" />

                <Button
                    android:layout_width="match_parent"
                    android:layout_height="100dp"
                    android:text="GO" />

            </LinearLayout>

        </ScrollView>

    </android.support.v4.widget.SwipeRefreshLayout>


</RelativeLayout>


Java文件:

package alink.swiperefreshlayoutdemo;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.view.ViewGroup;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final SwipeRefreshLayout swipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swiperefresh);

        swipeRefreshLayout.postDelayed(new Runnable() {
            @Override
            public void run() {
                swipeRefreshLayout.setRefreshing(true);
            }
        }, 1000);

        swipeRefreshLayout.postDelayed(new Runnable() {
            @Override
            public void run() {
                ViewGroup parent = (ViewGroup) swipeRefreshLayout.getParent();
                parent.removeAllViews();
            }
        }, 2000);

//        swipeRefreshLayout.postDelayed(new Runnable() {
//            @Override
//            public void run() {
//                swipeRefreshLayout.setRefreshing(false);
//            }
//        }, 3000);
    }
}


理论上,SwipeRefreshLayout被移除后,页面会变回一片空白,实际的情况是,虽然视图层级上已经没有了SwipeRefreshLayout,但是,SwipeRefreshLayout一直保持在界面上。


出现问题的原因在于:

ViewGroup中,对于remoreView的核心实现代码: removeViewInternal中,有这样一段代码:

        if (view.getAnimation() != null ||
                (mTransitioningViews != null && mTransitioningViews.contains(view))) {
            addDisappearingView(view);
        } else if (view.mAttachInfo != null) {
           view.dispatchDetachedFromWindow();
        }

因为刷新状态下,SwipeRefreshLayout中有动画存在,所以,SwipeRefreshLayout没有从Window上移除。


解决方案:

1. 在remove SwipeRefreshLayout之前,先setRefreshing(false);


2. SwipeRefreshLayout中,修改createProgressView函数如下:

    private void createProgressView() {
        mCircleView = new CircleImageView(getContext(), CIRCLE_BG_LIGHT, CIRCLE_DIAMETER/2);
        
        //mProgress = new MaterialProgressDrawable(getContext(), this);
        mProgress = new MaterialProgressDrawable(getContext(), mCircleView);

	mProgress.setBackgroundColor(CIRCLE_BG_LIGHT);        
        mCircleView.setImageDrawable(mProgress);        
        mCircleView.setVisibility(View.GONE);        
        addView(mCircleView);    
}
方法二能够解决问题,但是可能引入新风险,慎用




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值