Skip to content

Commit

Permalink
see 03/27 log
Browse files Browse the repository at this point in the history
  • Loading branch information
Blankj committed Mar 27, 2018
1 parent 2d0895f commit f8fd701
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 79 deletions.
63 changes: 26 additions & 37 deletions app/src/main/java/com/blankj/androidutilcode/base/BaseFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,17 @@
public abstract class BaseFragment extends Fragment
implements IBaseView {

private static final String TAG = "BaseFragment";

private static final String TAG = "BaseFragment";
private static final String STATE_SAVE_IS_HIDDEN = "STATE_SAVE_IS_HIDDEN";
/**
* 当前 Activity 渲染的视图 View
*/
protected View contentView;
/**
* 上次点击时间
*/
private long lastClick = 0;

protected View contentView;
protected BaseActivity mActivity;

private long lastClick = 0;

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
Log.d(TAG, "onCreate: ");
super.onCreate(savedInstanceState);
if (savedInstanceState != null) {
boolean isSupportHidden = savedInstanceState.getBoolean(STATE_SAVE_IS_HIDDEN);
Expand All @@ -45,74 +40,68 @@ public void onCreate(@Nullable Bundle savedInstanceState) {
} else {
ft.show(this);
}
ft.commit();
ft.commitAllowingStateLoss();
}
Log.d(TAG, "onCreate: ");
}

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
setRetainInstance(true);
contentView = inflater.inflate(bindLayout(), null);
Log.d(TAG, "onCreateView: ");
contentView = inflater.inflate(bindLayout(), null);
return contentView;
}

@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
Log.d(TAG, "onViewCreated: ");
super.onViewCreated(view, savedInstanceState);
Bundle bundle = getArguments();
initData(bundle);
Log.d(TAG, "onViewCreated: ");
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
Log.d(TAG, "onActivityCreated: ");
super.onActivityCreated(savedInstanceState);
mActivity = (BaseActivity) getActivity();
initView(savedInstanceState, contentView);
doBusiness();
Log.d(TAG, "onActivityCreated: ");
}

/**
* 判断是否快速点击
*
* @return {@code true}: 是<br>{@code false}: 否
*/
private boolean isFastClick() {
long now = System.currentTimeMillis();
if (now - lastClick >= 200) {
lastClick = now;
return false;
}
return true;
}

@Override
public void onClick(View view) {
if (!isFastClick()) onWidgetClick(view);
}

@Override
public void onDestroyView() {
Log.d(TAG, "onDestroyView: ");
if (contentView != null) {
((ViewGroup) contentView.getParent()).removeView(contentView);
}
super.onDestroyView();
Log.d(TAG, "onDestroyView: ");
}

@Override
public void onDestroy() {
super.onDestroy();
Log.d(TAG, "onDestroy: ");
super.onDestroy();
}

@Override
public void onSaveInstanceState(Bundle outState) {
Log.d(TAG, "onSaveInstanceState: ");
super.onSaveInstanceState(outState);
outState.putBoolean(STATE_SAVE_IS_HIDDEN, isHidden());
}

private boolean isFastClick() {
long now = System.currentTimeMillis();
if (now - lastClick >= 200) {
lastClick = now;
return false;
}
return true;
}

@Override
public void onClick(View view) {
if (!isFastClick()) onWidgetClick(view);
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
package com.blankj.androidutilcode.base;

import android.app.Fragment;
import android.os.Bundle;
import android.view.View;

import com.blankj.utilcode.util.LogUtils;
import android.util.Log;

/**
* <pre>
Expand All @@ -14,39 +10,29 @@
* desc :
* </pre>
*/
public abstract class BaseLazyFragment extends Fragment
implements IBaseView {
public abstract class BaseLazyFragment extends BaseFragment {

private static final String TAG = "BaseLazyFragment";

private boolean isDataLoaded;

private long lastClick = 0;
public abstract void doLazyBusiness();

@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
Log.d(TAG, "setUserVisibleHint: " + isVisibleToUser);
super.setUserVisibleHint(isVisibleToUser);
LogUtils.d("setUserVisibleHint() called with: isVisibleToUser = [" + isVisibleToUser + "]");;
}

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LogUtils.d("onCreate() called VisibleHint: " + getUserVisibleHint());
}

/**
* 判断是否快速点击
*
* @return {@code true}: 是<br>{@code false}: 否
*/
private boolean isFastClick() {
long now = System.currentTimeMillis();
if (now - lastClick >= 200) {
lastClick = now;
return false;
if (isVisibleToUser && contentView != null && !isDataLoaded) {
doLazyBusiness();
isDataLoaded = true;
}
return true;
}

@Override
public void onClick(View view) {
if (!isFastClick()) onWidgetClick(view);
public void doBusiness() {
if (getUserVisibleHint()) {
doLazyBusiness();
isDataLoaded = true;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@
import android.widget.TextView;

import com.blankj.androidutilcode.R;
import com.blankj.androidutilcode.base.BaseFragment;
import com.blankj.androidutilcode.base.BaseLazyFragment;
import com.blankj.utilcode.util.BarUtils;
import com.blankj.utilcode.util.LogUtils;

/**
* <pre>
Expand All @@ -17,7 +18,7 @@
* desc : Bar 工具类 Demo
* </pre>
*/
public class BarStatusAlphaFragment extends BaseFragment {
public class BarStatusAlphaFragment extends BaseLazyFragment {

private int mAlpha;

Expand Down Expand Up @@ -53,8 +54,8 @@ public void initView(Bundle savedInstanceState, View view) {
}

@Override
public void doBusiness() {

public void doLazyBusiness() {
LogUtils.d("doLazyBusiness() called");
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@

import com.blankj.androidutilcode.R;
import com.blankj.androidutilcode.UtilsApp;
import com.blankj.androidutilcode.base.BaseFragment;
import com.blankj.androidutilcode.base.BaseLazyFragment;
import com.blankj.utilcode.util.BarUtils;
import com.blankj.utilcode.util.LogUtils;

import java.util.Random;

Expand All @@ -21,7 +22,7 @@
* desc : Bar 工具类 Demo
* </pre>
*/
public class BarStatusColorFragment extends BaseFragment {
public class BarStatusColorFragment extends BaseLazyFragment {

private Random mRandom;
private int mColor;
Expand Down Expand Up @@ -62,8 +63,8 @@ public void initView(Bundle savedInstanceState, View view) {


@Override
public void doBusiness() {

public void doLazyBusiness() {
LogUtils.d("doLazyBusiness() called");
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@
import android.widget.TextView;

import com.blankj.androidutilcode.R;
import com.blankj.androidutilcode.base.BaseFragment;
import com.blankj.androidutilcode.base.BaseLazyFragment;
import com.blankj.utilcode.util.BarUtils;
import com.blankj.utilcode.util.LogUtils;

/**
* <pre>
Expand All @@ -17,7 +18,7 @@
* desc : Bar 工具类 Demo
* </pre>
*/
public class BarStatusImageViewFragment extends BaseFragment {
public class BarStatusImageViewFragment extends BaseLazyFragment {

private int mAlpha;

Expand Down Expand Up @@ -52,8 +53,8 @@ public void initView(Bundle savedInstanceState, View view) {
}

@Override
public void doBusiness() {

public void doLazyBusiness() {
LogUtils.d("doLazyBusiness() called");
}

@Override
Expand Down

0 comments on commit f8fd701

Please sign in to comment.