效果


使用
FragmentLoadingUtil.getInstance().startLoading(getContext(), root.findViewById(R.id.fl_loading), LOADING_TAG);
FragmentLoadingUtil
package com.coral3.common_module.utils;
import android.app.Activity;
import android.app.Fragment;
import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import com.coral3.common_module.R;
public class FragmentLoadingUtil {
private View loadingView;
private ViewGroup contentView;
public static FragmentLoadingUtil fragmentLoadingUtil;
private String tag;
public static FragmentLoadingUtil getInstance(){
if(null == fragmentLoadingUtil) fragmentLoadingUtil = new FragmentLoadingUtil();
return fragmentLoadingUtil;
}
public void startLoading(Context context, View root, String flag){
contentView = (ViewGroup)root;
tag = flag;
View lv = contentView.findViewWithTag(tag);
if(null == lv) {
loadingView = LayoutInflater.from(context).inflate(R.layout.view_loading, null, false);
loadingView.setTag(tag);
contentView.addView(loadingView);
}else {
loadingView = lv;
loadingView.setVisibility(View.VISIBLE);
}
}
public void startLoading(Context context, View root){
contentView = (ViewGroup)root;
tag = context.getPackageName() + context.getClass().toString();
View lv = contentView.findViewWithTag(tag);
if(null == lv) {
loadingView = LayoutInflater.from(context).inflate(R.layout.view_loading, null, false);
loadingView.setTag(tag);
contentView.addView(loadingView);
}else {
loadingView = lv;
loadingView.setVisibility(View.VISIBLE);
}
}
public void hideLoading(){
if (loadingView != null) {
loadingView.setVisibility(View.GONE);
}
}
public void startLoadingInUiThread(Context context, View contentView){
((Activity)context).runOnUiThread(new Runnable() {
@Override
public void run() {
startLoading(context, contentView, tag);
}
});
}
public void hideLoadingInUiThread(Activity activity){
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
hideLoading();
}
});
}
}