d
This commit is contained in:
280
app/src/main/java/com/fenghoo/seven/base/BasetwoFragment.java
Normal file
280
app/src/main/java/com/fenghoo/seven/base/BasetwoFragment.java
Normal file
@@ -0,0 +1,280 @@
|
||||
package com.fenghoo.seven.base;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.res.Configuration;
|
||||
import android.os.Bundle;
|
||||
import android.util.Log;
|
||||
import android.util.TypedValue;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.Toast;
|
||||
|
||||
import com.fenghoo.seven.R;
|
||||
import com.fenghoo.seven.dialog.LoadingDialog;
|
||||
import com.fenghoo.seven.widget.TitleBar;
|
||||
import com.hannesdorfmann.mosby3.mvp.MvpFragment;
|
||||
import com.hannesdorfmann.mosby3.mvp.MvpPresenter;
|
||||
import com.hannesdorfmann.mosby3.mvp.MvpView;
|
||||
|
||||
import androidx.annotation.IdRes;
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.annotation.StringRes;
|
||||
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
|
||||
|
||||
/**
|
||||
* BaseTreeFragment
|
||||
* (๑• . •๑)
|
||||
* 类描述: Fragment基类,使用第三方MVP
|
||||
* Created by LeiXiaoXing on 2017/3/14 09:57
|
||||
*/
|
||||
|
||||
public abstract class BasetwoFragment<V extends MvpView, P extends MvpPresenter<V>> extends MvpFragment<V, P> {
|
||||
|
||||
protected final String TAG = getClass().getSimpleName();
|
||||
protected Context mContext;
|
||||
protected Activity mActivity;
|
||||
private LoadingDialog mProgressDialog;
|
||||
/**
|
||||
* 视图是否已经初初始化
|
||||
*/
|
||||
protected boolean isInit = false;
|
||||
protected boolean isLoad = false;
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
|
||||
|
||||
return inflater.inflate(getFragmentLayoutId(), container, false);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
|
||||
super.onViewCreated(view, savedInstanceState);
|
||||
initView(view);
|
||||
initData();
|
||||
initEvent(view);
|
||||
isInit = true;
|
||||
isCanLoadData();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取Fragment布局文件id
|
||||
*
|
||||
* @return Fragment布局文件id
|
||||
*/
|
||||
protected abstract int getFragmentLayoutId();
|
||||
|
||||
/**
|
||||
* 初始化视图状态
|
||||
*/
|
||||
protected abstract void initView(View view);
|
||||
|
||||
/**
|
||||
* 初始化数据参数
|
||||
*/
|
||||
protected abstract void initData();
|
||||
|
||||
/**
|
||||
* 初始化操作事件
|
||||
*/
|
||||
protected abstract void initEvent(View view);
|
||||
|
||||
@Override
|
||||
public void onAttach(Context context) {
|
||||
super.onAttach(context);
|
||||
mContext = context;
|
||||
mActivity = getActivity();
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 视图是否已经对用户可见,系统的方法,setUserVisibleHint在onCreate之前调用
|
||||
* 1.第一次创建Fragment时,加载数据会报空指针异常,所以要加个判断是否已经初始化
|
||||
* 2.当Fragment已经初始化,再次可见时会直接调用此方法,此时可以直接加载数据
|
||||
* 所以要在两个地方调用加载数据
|
||||
*/
|
||||
@Override
|
||||
public void setUserVisibleHint(boolean isVisibleToUser) {
|
||||
super.setUserVisibleHint(isVisibleToUser);
|
||||
isCanLoadData();
|
||||
}
|
||||
|
||||
/**
|
||||
* 是否可以加载数据
|
||||
* 可以加载数据的条件:
|
||||
* 1.视图已经初始化
|
||||
* 2.视图对用户可见
|
||||
*/
|
||||
private void isCanLoadData() {
|
||||
if (!isInit) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (getUserVisibleHint()) {
|
||||
setUpView();
|
||||
isLoad = true;
|
||||
} else {
|
||||
if (isLoad) {
|
||||
stopLoad();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 当视图初始化并且对用户可见的时候去真正的加载数据
|
||||
*/
|
||||
protected void setUpView() {
|
||||
}
|
||||
|
||||
/**
|
||||
* 当视图已经对用户不可见并且加载过数据,如果需要在切换到其他页面时停止加载数据,可以调用此方法
|
||||
*/
|
||||
protected void stopLoad() {
|
||||
}
|
||||
|
||||
/**
|
||||
* toast
|
||||
*
|
||||
* @param msg 吐丝的内容
|
||||
*/
|
||||
public void toast(String msg) {
|
||||
Toast.makeText(mContext, msg, Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
|
||||
/**
|
||||
* toast
|
||||
*
|
||||
* @param ids 吐丝的内容的字符串ids
|
||||
*/
|
||||
public void toast(@StringRes int ids) {
|
||||
toast(getString(ids));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 线程安全的toast
|
||||
*
|
||||
* @param msg
|
||||
*/
|
||||
public void toastThreadSafe(final String msg) {
|
||||
if (null != mActivity) {
|
||||
mActivity.runOnUiThread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
toast(msg);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 线程安全的toast
|
||||
*
|
||||
* @param ids
|
||||
*/
|
||||
public void toastThreadSafe(@StringRes final int ids) {
|
||||
if (null != mActivity) {
|
||||
mActivity.runOnUiThread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
toast(ids);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 开启一个Activity
|
||||
*
|
||||
* @param clz 需要开启的Activity
|
||||
*/
|
||||
public void startActivity(Class<? extends Activity> clz) {
|
||||
if (null != mContext) {
|
||||
startActivity(new Intent(mContext, clz));
|
||||
// getActivity().overridePendingTransition(R.anim.trans_next_in,R.anim.trans_next_out);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 开启一个Activity,并finish掉当前的Activity
|
||||
*
|
||||
* @param clz
|
||||
*/
|
||||
public void startActivityAndFinishSelf(Class<? extends Activity> clz) {
|
||||
if (null != mActivity) {
|
||||
startActivity(clz);
|
||||
mActivity.finish();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onConfigurationChanged(Configuration newConfig) {
|
||||
super.onConfigurationChanged(newConfig);
|
||||
}
|
||||
|
||||
/**
|
||||
* 初始化进度对话框
|
||||
*/
|
||||
|
||||
public void showProgressDialog(String dialogMessage) {
|
||||
mProgressDialog = new LoadingDialog(mContext);
|
||||
mProgressDialog.setCanceledOnTouchOutside(false);
|
||||
// mProgressDialog.setMessage(dialogMessage);
|
||||
mProgressDialog.show();
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void onHiddenChanged(boolean hidden) {
|
||||
super.onHiddenChanged(hidden);
|
||||
Log.d(TAG, "onHiddenChanged");
|
||||
}
|
||||
|
||||
/**
|
||||
* 关闭进度对话框
|
||||
*/
|
||||
|
||||
public void dismissProgressDialog() {
|
||||
if (null != mProgressDialog && mProgressDialog.isShowing()) {
|
||||
mProgressDialog.dismiss();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 初始化刷新控件的参数
|
||||
*
|
||||
* @param ids 刷新控件的id
|
||||
*/
|
||||
protected SwipeRefreshLayout setSwipeRefreshLayout(@IdRes int ids) {
|
||||
SwipeRefreshLayout mRefreshLayout = (SwipeRefreshLayout) getView().findViewById(ids);
|
||||
mRefreshLayout.setProgressViewOffset(false, 0,
|
||||
(int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 24, getResources().getDisplayMetrics()));
|
||||
mRefreshLayout.setColorSchemeResources(R.color.colorPrimary);
|
||||
return mRefreshLayout;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置标题栏参数
|
||||
*
|
||||
* @param titleIds 标题栏的id
|
||||
* @param title 标题
|
||||
* @param goneLeftBtn 左边按钮是否显示
|
||||
* @param goneRightBtn 右边按钮是否显示
|
||||
* @param titleBarClickListener 左右边的按钮点击事件回调接口
|
||||
* @return 返回设置好的titlebar
|
||||
*/
|
||||
public TitleBar setTitleBar(@IdRes int titleIds, String title
|
||||
, boolean goneLeftBtn, boolean goneRightBtn, TitleBar.OnTitleBarClickListener titleBarClickListener) {
|
||||
TitleBar titleBar = (TitleBar) getView().findViewById(titleIds);
|
||||
titleBar.setTitle(title);
|
||||
titleBar.visibleIvLeftBtn(goneLeftBtn);
|
||||
titleBar.visibleIvRightBtn(goneRightBtn);
|
||||
titleBar.setOnTitleBarClickListener(titleBarClickListener);
|
||||
return titleBar;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user