|
@@ -1,5 +1,6 @@
|
|
|
package com.miekir.mvp.base;
|
|
package com.miekir.mvp.base;
|
|
|
|
|
|
|
|
|
|
+import android.app.Activity;
|
|
|
import android.os.Bundle;
|
|
import android.os.Bundle;
|
|
|
import android.os.Looper;
|
|
import android.os.Looper;
|
|
|
import android.view.LayoutInflater;
|
|
import android.view.LayoutInflater;
|
|
@@ -17,22 +18,19 @@ import com.miekir.mvp.widget.LoadingView;
|
|
|
|
|
|
|
|
public abstract class BaseFragment extends Fragment {
|
|
public abstract class BaseFragment extends Fragment {
|
|
|
public FragmentActivity activity;
|
|
public FragmentActivity activity;
|
|
|
- //private Unbinder mBinder;
|
|
|
|
|
private LoadingView mLoadingDialog;
|
|
private LoadingView mLoadingDialog;
|
|
|
protected View rootView;
|
|
protected View rootView;
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
* 设置布局layout
|
|
* 设置布局layout
|
|
|
- *
|
|
|
|
|
* @return 布局文件id
|
|
* @return 布局文件id
|
|
|
*/
|
|
*/
|
|
|
- public abstract @LayoutRes
|
|
|
|
|
- int getLayoutResId();
|
|
|
|
|
|
|
+ public abstract @LayoutRes int getLayoutId();
|
|
|
|
|
|
|
|
@Nullable
|
|
@Nullable
|
|
|
@Override
|
|
@Override
|
|
|
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
|
|
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
|
|
|
- rootView = inflater.inflate(getLayoutResId(), container, false);
|
|
|
|
|
|
|
+ rootView = inflater.inflate(getLayoutId(), container, false);
|
|
|
activity = getActivity();
|
|
activity = getActivity();
|
|
|
return rootView;
|
|
return rootView;
|
|
|
}
|
|
}
|
|
@@ -40,22 +38,36 @@ public abstract class BaseFragment extends Fragment {
|
|
|
@Override
|
|
@Override
|
|
|
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
|
|
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
|
|
|
super.onViewCreated(view, savedInstanceState);
|
|
super.onViewCreated(view, savedInstanceState);
|
|
|
- //mBinder = ButterKnife.bind(this, view);
|
|
|
|
|
|
|
+
|
|
|
mLoadingDialog = new LoadingView(getActivity());
|
|
mLoadingDialog = new LoadingView(getActivity());
|
|
|
mLoadingDialog.setCancelListener(new LoadingView.OnLoadingCancelListener() {
|
|
mLoadingDialog.setCancelListener(new LoadingView.OnLoadingCancelListener() {
|
|
|
@Override
|
|
@Override
|
|
|
public void onLoadingCancel() {
|
|
public void onLoadingCancel() {
|
|
|
- onTaskCancel();
|
|
|
|
|
|
|
+ mLoadingTime = 0;
|
|
|
|
|
+ onProgressTaskCancel();
|
|
|
}
|
|
}
|
|
|
});
|
|
});
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- public abstract void onTaskCancel();
|
|
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public void onDestroyView() {
|
|
|
|
|
+ super.onDestroyView();
|
|
|
|
|
+ hideLoading();
|
|
|
|
|
+ activity = null;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 可见任务被取消
|
|
|
|
|
+ */
|
|
|
|
|
+ public abstract void onProgressTaskCancel();
|
|
|
|
|
|
|
|
private boolean isMainThread() {
|
|
private boolean isMainThread() {
|
|
|
return Looper.myLooper() == Looper.getMainLooper();
|
|
return Looper.myLooper() == Looper.getMainLooper();
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 加载框相关
|
|
|
|
|
+ */
|
|
|
public void showLoading() {
|
|
public void showLoading() {
|
|
|
showLoading(true);
|
|
showLoading(true);
|
|
|
}
|
|
}
|
|
@@ -68,7 +80,33 @@ public abstract class BaseFragment extends Fragment {
|
|
|
showLoading(msg, true);
|
|
showLoading(msg, true);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ private volatile int mLoadingTime = 0;
|
|
|
|
|
+ private synchronized void onLoadTimeChange(boolean isShow) {
|
|
|
|
|
+ if (isShow) {
|
|
|
|
|
+ mLoadingTime++;
|
|
|
|
|
+ } else {
|
|
|
|
|
+ mLoadingTime--;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ if (mLoadingTime <= 0 && mLoadingDialog != null) {
|
|
|
|
|
+ mLoadingTime = 0;
|
|
|
|
|
+ if (isMainThread()) {
|
|
|
|
|
+ mLoadingDialog.close();
|
|
|
|
|
+ } else {
|
|
|
|
|
+ Activity activity = getActivity();
|
|
|
|
|
+ if (activity != null) {
|
|
|
|
|
+ activity.runOnUiThread(new Runnable() {
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public void run() {
|
|
|
|
|
+ mLoadingDialog.close();
|
|
|
|
|
+ }
|
|
|
|
|
+ });
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
public void showLoading(final String msg, final boolean cancelable) {
|
|
public void showLoading(final String msg, final boolean cancelable) {
|
|
|
|
|
+ onLoadTimeChange(true);
|
|
|
if (mLoadingDialog == null) {
|
|
if (mLoadingDialog == null) {
|
|
|
return;
|
|
return;
|
|
|
}
|
|
}
|
|
@@ -76,35 +114,21 @@ public abstract class BaseFragment extends Fragment {
|
|
|
if (isMainThread()) {
|
|
if (isMainThread()) {
|
|
|
mLoadingDialog.show(msg);
|
|
mLoadingDialog.show(msg);
|
|
|
} else {
|
|
} else {
|
|
|
- activity.runOnUiThread(new Runnable() {
|
|
|
|
|
- @Override
|
|
|
|
|
- public void run() {
|
|
|
|
|
- mLoadingDialog.show(msg, cancelable);
|
|
|
|
|
- }
|
|
|
|
|
- });
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- public void hideLoading() {
|
|
|
|
|
- if (mLoadingDialog != null) {
|
|
|
|
|
- if (isMainThread()) {
|
|
|
|
|
- mLoadingDialog.close();
|
|
|
|
|
- } else {
|
|
|
|
|
|
|
+ Activity activity = getActivity();
|
|
|
|
|
+ if (activity != null) {
|
|
|
activity.runOnUiThread(new Runnable() {
|
|
activity.runOnUiThread(new Runnable() {
|
|
|
@Override
|
|
@Override
|
|
|
public void run() {
|
|
public void run() {
|
|
|
- mLoadingDialog.close();
|
|
|
|
|
|
|
+ mLoadingDialog.show(msg, cancelable);
|
|
|
}
|
|
}
|
|
|
});
|
|
});
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- @Override
|
|
|
|
|
- public void onDestroyView() {
|
|
|
|
|
- super.onDestroyView();
|
|
|
|
|
- //mBinder.unbind();
|
|
|
|
|
- hideLoading();
|
|
|
|
|
- activity = null;
|
|
|
|
|
|
|
+ public void hideLoading() {
|
|
|
|
|
+ onLoadTimeChange(false);
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
}
|
|
}
|