This commit is contained in:
unknown
2020-03-31 16:41:18 +08:00
parent 6fc9985ad2
commit 91dbfe1862
14 changed files with 190 additions and 91 deletions

View File

@@ -10,41 +10,43 @@ import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import com.mogo.utils.logger.Logger;
/**
* @author congtaowang
* @since 2019-12-23
* <p>
* mvp fragment
*/
public abstract class MvpFragment< V extends IView, P extends Presenter< V > > extends Fragment implements IView {
public abstract class MvpFragment<V extends IView, P extends Presenter<V>> extends Fragment implements IView {
private Context mContext;
protected P mPresenter;
protected View mRootView;
@Override
public void onAttach( Context context ) {
super.onAttach( context );
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 );
// }
mRootView = inflater.inflate( getLayoutId(), container, false );
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 );
}
mRootView = inflater.inflate(getLayoutId(), container, false);
return mRootView;
}
@Override
public void onViewCreated( @NonNull View view, @Nullable Bundle savedInstanceState ) {
super.onViewCreated( view, savedInstanceState );
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
initViews(savedInstanceState);
}
@@ -56,11 +58,11 @@ public abstract class MvpFragment< V extends IView, P extends Presenter< V > > e
protected abstract int getLayoutId();
@Override
public void onActivityCreated( @Nullable Bundle savedInstanceState ) {
super.onActivityCreated( savedInstanceState );
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
initViews();
mPresenter = createPresenter();
getViewLifecycleOwner().getLifecycle().addObserver( mPresenter );
getViewLifecycleOwner().getLifecycle().addObserver(mPresenter);
}
/**
@@ -68,7 +70,9 @@ public abstract class MvpFragment< V extends IView, P extends Presenter< V > > e
* presenter 生命周期错乱
*/
protected abstract void initViews();
protected void initViews(Bundle savedInstanceState){}
protected void initViews(Bundle savedInstanceState) {
}
/**
* 创建 presenter 实例
@@ -79,9 +83,12 @@ public abstract class MvpFragment< V extends IView, P extends Presenter< V > > e
protected abstract P createPresenter();
@Nullable
protected < T extends View > T findViewById( int id ) {
if ( mRootView != null ) {
return ( T ) mRootView.findViewById( id );
protected <T extends View> T findViewById(int id) {
if (mRootView == null) {
mRootView = getView().findViewById(id);
}
if (mRootView != null) {
return (T) mRootView.findViewById(id);
}
return null;
}
@@ -89,7 +96,7 @@ public abstract class MvpFragment< V extends IView, P extends Presenter< V > > e
@Nullable
@Override
public Context getContext() {
if ( mContext == null ) {
if (mContext == null) {
mContext = super.getContext();
}
return mContext;
@@ -98,8 +105,8 @@ public abstract class MvpFragment< V extends IView, P extends Presenter< V > > e
@Override
public void onDestroyView() {
super.onDestroyView();
if ( mPresenter != null ) {
getViewLifecycleOwner().getLifecycle().removeObserver( mPresenter );
if (mPresenter != null) {
getViewLifecycleOwner().getLifecycle().removeObserver(mPresenter);
}
mPresenter = null;
mRootView = null;

View File

@@ -78,7 +78,6 @@ public class AIAssist implements VoiceClient.VoiceCmdCallBack {
mHasFlush = isVoiceServiceReady( context );
}
@VoiceTrack
@Override
public void onCmdSelected( String cmd ) {
if ( !mCmdMap.containsKey( cmd ) ) {
@@ -95,7 +94,6 @@ public class AIAssist implements VoiceClient.VoiceCmdCallBack {
}
}
@VoiceTrack
@Override
public void onCmdAction( String speakText ) {
if ( !TextUtils.isEmpty( mLastQAndASpeakText ) ) {
@@ -106,7 +104,6 @@ public class AIAssist implements VoiceClient.VoiceCmdCallBack {
}
}
@VoiceTrack
@Override
public void onCmdCancel( String speakText ) {
if ( !TextUtils.isEmpty( mLastQAndASpeakText ) ) {
@@ -117,7 +114,6 @@ public class AIAssist implements VoiceClient.VoiceCmdCallBack {
}
}
@VoiceTrack
@Override
public void onSpeakEnd( String speakText ) {
if ( mQAndAMap.containsKey( speakText ) ) {
@@ -134,7 +130,6 @@ public class AIAssist implements VoiceClient.VoiceCmdCallBack {
}
}
@VoiceTrack
@Override
public void onSpeakSelectTimeOut( String speakText ) {
if ( mQAndAMap.containsKey( speakText ) ) {