dev
This commit is contained in:
@@ -0,0 +1,106 @@
|
||||
package com.mogo.commons.mvp;
|
||||
|
||||
import android.content.Context;
|
||||
import android.os.Bundle;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.fragment.app.DialogFragment;
|
||||
import androidx.fragment.app.Fragment;
|
||||
|
||||
/**
|
||||
* @author congtaowang
|
||||
* @since 2019-12-23
|
||||
* <p>
|
||||
* mvp fragment
|
||||
*/
|
||||
public abstract class MvpDialogFragment< V extends IView, P extends Presenter< V > > extends DialogFragment implements IView {
|
||||
|
||||
private Context mContext;
|
||||
protected P mPresenter;
|
||||
protected View mRootView;
|
||||
|
||||
@Override
|
||||
public void onAttach( Context context ) {
|
||||
super.onAttach( context );
|
||||
mContext = context;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public View onCreateView( @NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState ) {
|
||||
if ( mRootView == null ) {
|
||||
mRootView = inflater.inflate( getLayoutId(), container, false );
|
||||
} else {
|
||||
ViewGroup viewGroup = ( ViewGroup ) mRootView.getParent();
|
||||
if ( viewGroup != null )
|
||||
viewGroup.removeView( mRootView );
|
||||
}
|
||||
return mRootView;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onViewCreated( @NonNull View view, @Nullable Bundle savedInstanceState ) {
|
||||
super.onViewCreated( view, savedInstanceState );
|
||||
}
|
||||
|
||||
/**
|
||||
* 布局资源
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
protected abstract int getLayoutId();
|
||||
|
||||
@Override
|
||||
public void onActivityCreated( @Nullable Bundle savedInstanceState ) {
|
||||
super.onActivityCreated( savedInstanceState );
|
||||
initViews();
|
||||
mPresenter = createPresenter();
|
||||
getViewLifecycleOwner().getLifecycle().addObserver( mPresenter );
|
||||
}
|
||||
|
||||
/**
|
||||
* 初始化控件,必须在初始化完成之后才可以实例化presenter,避免
|
||||
* presenter 生命周期错乱
|
||||
*/
|
||||
protected abstract void initViews();
|
||||
|
||||
/**
|
||||
* 创建 presenter 实例
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
@NonNull
|
||||
protected abstract P createPresenter();
|
||||
|
||||
@Nullable
|
||||
protected < T extends View > T findViewById( int id ) {
|
||||
if ( mRootView != null ) {
|
||||
return ( T ) mRootView.findViewById( id );
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public Context getContext() {
|
||||
if ( mContext == null ) {
|
||||
mContext = super.getContext();
|
||||
}
|
||||
return mContext;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDestroyView() {
|
||||
super.onDestroyView();
|
||||
if ( mPresenter != null ) {
|
||||
getViewLifecycleOwner().getLifecycle().removeObserver( mPresenter );
|
||||
}
|
||||
mPresenter = null;
|
||||
mRootView = null;
|
||||
mContext = null;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user