1、整个工具类:
public class LoadingPage extends ConstraintLayout {
public static final int MODE_ONLY_SHOW_LOGING = 1;
public static final int MODE_SHOW_BUTTON_AND_TEXT = 2;
private ProgressBar mPbLoading;
private Button mBtnAction;
private TextView mTvMsg;
private OnReloadCallBack mOnReloadCallBack;
public LoadingPage(Context context) {
super(context);
}
public LoadingPage(Context context, AttributeSet attrs) {
super(context, attrs);
}
public LoadingPage(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
protected void onFinishInflate() {
super.onFinishInflate();
mPbLoading = findViewById(R.id.loading_pb_loading_view);
mBtnAction = findViewById(R.id.loading_btn_reload);
mTvMsg = findViewById(R.id.loading_tv_msg);
mBtnAction.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if(mOnReloadCallBack != null){
mOnReloadCallBack.reload();
startLoading(MODE_SHOW_BUTTON_AND_TEXT);
}
}
});
startLoading();
}
@Override
public boolean onTouchEvent(MotionEvent event) {
super.onTouchEvent(event);
return true;
}
public void startLoading(){
startLoading(MODE_ONLY_SHOW_LOGING);
}
public void startLoading(int mode){
if(mode == MODE_ONLY_SHOW_LOGING){
mBtnAction.setVisibility(GONE);
mTvMsg.setVisibility(GONE);
mPbLoading.setVisibility(VISIBLE);
setBackgroundColor(Color.TRANSPARENT);
}else if(mode == MODE_SHOW_BUTTON_AND_TEXT){
mBtnAction.setVisibility(GONE);
mPbLoading.setVisibility(VISIBLE);
mTvMsg.setVisibility(VISIBLE);
mTvMsg.setText(R.string.text_loading_loading);
setBackgroundColor(Color.WHITE);
}
}
public void onError(){
onError(getContext().getString(R.string.text_loading_error));
}
public void onError(String msg){
onError(null, msg);
}
public void onError(OnReloadCallBack callBack,String msg){
mTvMsg.setText(msg);
mTvMsg.setVisibility(VISIBLE);
mBtnAction.setVisibility(VISIBLE);
mPbLoading.clearAnimation();
mPbLoading.setVisibility(GONE);
mOnReloadCallBack = callBack;
}
@Override
protected void onDetachedFromWindow() {
super.onDetachedFromWindow();
mPbLoading.clearAnimation();
}
public interface OnReloadCallBack{
void reload();
}
}
2、布局文件:layout_laoding_page.xml
<?xml version="1.0" encoding="utf-8"?>
<com.jy.small.training.widget.view.LoadingPage xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ProgressBar
android:id="@+id/loading_pb_loading_view"
style="?android:attr/progressBarStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="8dp"
android:visibility="visible"
app:layout_constraintBottom_toTopOf="@+id/guideline"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<TextView
android:id="@+id/loading_tv_msg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:text="TextView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@+id/guideline" />
<android.support.constraint.Guideline
android:id="@+id/guideline"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.5" />
<Button
android:id="@+id/loading_btn_reload"
android:layout_width="wrap_content"
android:layout_height="32dp"
android:layout_marginStart="32dp"
android:layout_marginLeft="32dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="32dp"
android:layout_marginRight="32dp"
android:background="@drawable/button_yellow_round_bg_selector"
android:text="@string/text_loading_reload"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/loading_tv_msg" />
</com.jy.small.training.widget.view.LoadingPage>
3、布局文件对应的String 字符串
<string name="text_loading_reload">重新加载</string>
<string name="text_loading_loading">正在加载...</string>
<string name="text_loading_error">加载失败,请重试。</string>
4、布局文件对应的color 颜色
<color name="color_a">#FFC946</color>
<!-- 多用于按钮按下颜色-->
<color name="color_b">#E5B53F</color>
5、四个动画文件:
(1)common_page_left_in.xml
<?xml version="1.0" encoding="utf-8"?>
<set>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:fromXDelta="-100%"
android:toXDelta="0"
android:interpolator="@android:anim/decelerate_interpolator"
android:duration="500"/>
</set>
(2)common_page_left_out.xml
<?xml version="1.0" encoding="utf-8"?>
<set>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:fromXDelta="0"
android:toXDelta="-100%"
android:interpolator="@android:anim/decelerate_interpolator"
android:duration="500"/>
</set>
(3)common_page_right_in.xml
<?xml version="1.0" encoding="utf-8"?>
<set>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:fromXDelta="100%"
android:toXDelta="0"
android:interpolator="@android:anim/decelerate_interpolator"
android:duration="500"/>
</set>
(4)common_page_right_out.xml
<?xml version="1.0" encoding="utf-8"?>
<set>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:fromXDelta="0"
android:toXDelta="100%"
android:interpolator="@android:anim/decelerate_interpolator"
android:duration="500"/>
</set>