factory, @Nullable Object[] extras) {
41 | final ParentStrategy strategy = getParentStrategy();
42 | ViewGroup realParent = strategy.findSuitableParent(mParent);
43 |
44 | final String factoryKey = factory.getKey();
45 | final boolean changed = mFactoryKey == null || !mFactoryKey.equals(factoryKey);
46 |
47 | //如果跟已有的parent不一样 或者样式发生变化 就先移除之前的View
48 | if (mRealParent != null && (mRealParent != realParent || changed)) {
49 | mRealParent.removeView(mView);
50 | }
51 |
52 | //如果还没有View 或者样式发生变化 就重新创建
53 | if (mView == null || changed) {
54 | this.mView = factory.onCreate(realParent);
55 | }
56 |
57 | factory.updateStatus(extras);
58 |
59 | this.mRealParent = realParent;
60 | //没有父级才添加
61 | if (mRealParent != null && mView != null && mView.getParent() == null) {
62 | mRealParent.addView(mView);
63 | this.mFactoryKey = factoryKey;
64 | }
65 | }
66 |
67 | @Override
68 | public void cancel() {
69 | if (mRealParent != null) {
70 | mRealParent.removeView(mView);
71 | }
72 | mParent = null;
73 | mRealParent = null;
74 | mView = null;
75 | mFactoryKey = null;
76 | }
77 |
78 | @Override
79 | public boolean isCanRecycled() {
80 | if (mRealParent != null) {
81 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
82 | return !mRealParent.isAttachedToWindow();
83 | } else {
84 | final Context context = mRealParent.getContext();
85 | if (context instanceof Activity) {
86 | return ((Activity) context).isFinishing();
87 | }
88 | }
89 | }
90 | return false;
91 | }
92 |
93 | protected ParentStrategy getParentStrategy() {
94 | if (mParentStrategy == null) {
95 | mParentStrategy = new CoverParentStrategy();
96 | }
97 | return mParentStrategy;
98 | }
99 | }
100 |
--------------------------------------------------------------------------------
/loadingbar/src/main/java/com/dyhdyh/widget/loadingbar2/factory/LoadingFactory.java:
--------------------------------------------------------------------------------
1 | package com.dyhdyh.widget.loadingbar2.factory;
2 |
3 | import android.support.annotation.Nullable;
4 |
5 | /**
6 | * Loading工厂 决定了loading的样式
7 | * @author dengyuhan
8 | * created 2019/3/14 15:51
9 | */
10 | public interface LoadingFactory {
11 | /**
12 | * 工厂类的标识符
13 | * 在cancel()之前多次调用show()时,当key相同时不会重新调用onCreate
14 | * @return
15 | */
16 | String getKey();
17 |
18 | L onCreate(P params);
19 |
20 | void updateStatus(@Nullable Object[] extras);
21 |
22 | }
23 |
--------------------------------------------------------------------------------
/loadingbar/src/main/java/com/dyhdyh/widget/loadingbar2/factory/MaterialDialogFactory.java:
--------------------------------------------------------------------------------
1 | package com.dyhdyh.widget.loadingbar2.factory;
2 |
3 | import android.app.Dialog;
4 | import android.content.Context;
5 | import android.support.annotation.Nullable;
6 | import android.support.v7.app.AlertDialog;
7 | import android.view.LayoutInflater;
8 | import android.view.View;
9 | import android.widget.TextView;
10 |
11 | import com.dyhdyh.widget.loadingbar2.R;
12 |
13 | /**
14 | * 创建Material Loading样式Dialog的工厂
15 | *
16 | * @author dengyuhan
17 | * created 2019/3/14 15:52
18 | */
19 | public class MaterialDialogFactory implements LoadingFactory {
20 | private TextView mMessageView;
21 |
22 | @Override
23 | public String getKey() {
24 | return getClass().getName();
25 | }
26 |
27 | @Override
28 | public Dialog onCreate(Context params) {
29 | final View view = LayoutInflater.from(params).inflate(R.layout.dialog_material_loading, null);
30 | mMessageView = view.findViewById(android.R.id.message);
31 | return new AlertDialog.Builder(params)
32 | .setView(view)
33 | .setCancelable(false)
34 | .create();
35 | }
36 |
37 | @Override
38 | public void updateStatus(@Nullable Object[] extras) {
39 | if (extras != null && extras.length > 0 && extras[0] instanceof CharSequence) {
40 | mMessageView.setVisibility(View.VISIBLE);
41 | mMessageView.setText((CharSequence) extras[0]);
42 | } else {
43 | mMessageView.setVisibility(View.GONE);
44 | }
45 | }
46 |
47 | }
48 |
--------------------------------------------------------------------------------
/loadingbar/src/main/java/com/dyhdyh/widget/loadingbar2/factory/MaterialViewFactory.java:
--------------------------------------------------------------------------------
1 | package com.dyhdyh.widget.loadingbar2.factory;
2 |
3 | import android.support.annotation.Nullable;
4 | import android.view.LayoutInflater;
5 | import android.view.View;
6 | import android.view.ViewGroup;
7 | import android.widget.TextView;
8 |
9 | import com.dyhdyh.widget.loadingbar2.R;
10 |
11 | /**
12 | * @author dengyuhan
13 | * created 2019/3/14 15:52
14 | */
15 | public class MaterialViewFactory implements LoadingFactory {
16 | private TextView mMessageView;
17 |
18 | @Override
19 | public String getKey() {
20 | return getClass().getName();
21 | }
22 |
23 | @Override
24 | public View onCreate(ViewGroup parent) {
25 | final View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.view_material_loading, parent, false);
26 | mMessageView = view.findViewById(android.R.id.message);
27 | return view;
28 | }
29 |
30 | @Override
31 | public void updateStatus(@Nullable Object[] extras) {
32 | if (extras != null && extras.length > 0 && extras[0] instanceof CharSequence) {
33 | mMessageView.setVisibility(View.VISIBLE);
34 | mMessageView.setText((CharSequence) extras[0]);
35 | } else {
36 | mMessageView.setVisibility(View.GONE);
37 | }
38 | }
39 | }
40 |
--------------------------------------------------------------------------------
/loadingbar/src/main/java/com/dyhdyh/widget/loadingbar2/strategy/CoverParentStrategy.java:
--------------------------------------------------------------------------------
1 | package com.dyhdyh.widget.loadingbar2.strategy;
2 |
3 | import android.support.annotation.NonNull;
4 | import android.support.annotation.Nullable;
5 | import android.view.View;
6 | import android.view.ViewGroup;
7 | import android.view.ViewParent;
8 | import android.widget.FrameLayout;
9 | import android.widget.RelativeLayout;
10 |
11 | /**
12 | * 一直向上寻找 直到找到可覆盖的布局
13 | *
14 | * @author dengyuhan
15 | * created 2019/3/14 11:30
16 | */
17 | public class CoverParentStrategy implements ParentStrategy {
18 |
19 | @Override
20 | public @Nullable
21 | ViewGroup findSuitableParent(@Nullable View parent) {
22 | //循环往上找到最合适的View
23 | View suitableParent = parent;
24 | while (suitableParent != null && !isSuitableParent(suitableParent)) {
25 | final ViewParent viewParent = suitableParent.getParent();
26 | suitableParent = viewParent instanceof View ? (View) viewParent : null;
27 | }
28 | return (ViewGroup) suitableParent;
29 | }
30 |
31 | /**
32 | * 判断是否可覆盖的布局
33 | *
34 | * @param parent
35 | * @return
36 | */
37 | protected boolean isSuitableParent(@NonNull View parent) {
38 | final String className = parent.getClass().getName();
39 | return parent instanceof FrameLayout || parent instanceof RelativeLayout
40 | || "android.support.constraint.ConstraintLayout".equals(className)
41 | || "android.support.v4.widget.DrawerLayout".equals(className)
42 | || "android.support.design.widget.CoordinatorLayout".equals(className)
43 | || "android.support.v7.widget.CardView".equals(className);
44 | }
45 | }
46 |
--------------------------------------------------------------------------------
/loadingbar/src/main/java/com/dyhdyh/widget/loadingbar2/strategy/ParentStrategy.java:
--------------------------------------------------------------------------------
1 | package com.dyhdyh.widget.loadingbar2.strategy;
2 |
3 | import android.view.View;
4 | import android.view.ViewGroup;
5 |
6 | /**
7 | * 选择Parent的策略
8 | *
9 | * @author dengyuhan
10 | * created 2019/3/14 11:28
11 | */
12 | public interface ParentStrategy {
13 | ViewGroup findSuitableParent(View parent);
14 | }
15 |
--------------------------------------------------------------------------------
/loadingbar/src/main/java/com/dyhdyh/widget/loadingbar2/strategy/SimpleParentStrategy.java:
--------------------------------------------------------------------------------
1 | package com.dyhdyh.widget.loadingbar2.strategy;
2 |
3 | import android.view.View;
4 | import android.view.ViewGroup;
5 |
6 | /**
7 | * 直接使用传进来的父布局
8 | *
9 | * @author dengyuhan
10 | * created 2019/3/14 11:30
11 | */
12 | public class SimpleParentStrategy implements ParentStrategy {
13 |
14 | @Override
15 | public ViewGroup findSuitableParent(View parent) {
16 | return parent instanceof ViewGroup ? (ViewGroup) parent : null;
17 | }
18 | }
19 |
--------------------------------------------------------------------------------
/loadingbar/src/main/res/drawable/loading_progressbar_vertical_material.xml:
--------------------------------------------------------------------------------
1 |
2 |
7 |
8 |
13 |
20 |
21 |
22 |
--------------------------------------------------------------------------------
/loadingbar/src/main/res/layout/dialog_material_loading.xml:
--------------------------------------------------------------------------------
1 |
2 |
10 |
11 |
19 |
20 |
27 |
28 |
29 |
--------------------------------------------------------------------------------
/loadingbar/src/main/res/layout/loading_progressbar_vertical_material.xml:
--------------------------------------------------------------------------------
1 |
2 |
6 |
7 |
12 |
--------------------------------------------------------------------------------
/loadingbar/src/main/res/layout/view_material_loading.xml:
--------------------------------------------------------------------------------
1 |
2 |
13 |
14 |
22 |
23 |
32 |
33 |
34 |
--------------------------------------------------------------------------------
/loadingbar/src/main/res/values-v21/loading_styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
7 |
8 |
--------------------------------------------------------------------------------
/loadingbar/src/main/res/values/loading_colors.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | #eeeeee
4 |
--------------------------------------------------------------------------------
/loadingbar/src/main/res/values/loading_strings.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | 正在提交
4 |
--------------------------------------------------------------------------------
/loadingbar/src/main/res/values/loading_styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
10 |
11 |
--------------------------------------------------------------------------------
/screenshots/download.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/xiandanin/LoadingBar/3913434edec709431f985b628668b998198f2d1d/screenshots/download.png
--------------------------------------------------------------------------------
/screenshots/loadingbar.gif:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/xiandanin/LoadingBar/3913434edec709431f985b628668b998198f2d1d/screenshots/loadingbar.gif
--------------------------------------------------------------------------------
/settings.gradle:
--------------------------------------------------------------------------------
1 | include ':example', ':loadingbar'
2 |
--------------------------------------------------------------------------------