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>
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);
}
}
出现问题的原因在于:
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);
}
方法二能够解决问题,但是可能引入新风险,慎用