增加导航模块

This commit is contained in:
zhangyuanzhen
2020-01-02 16:56:16 +08:00
parent 5762b771e3
commit c3df548d04
34 changed files with 3360 additions and 13 deletions

View File

@@ -0,0 +1,110 @@
package com.mogo.utils;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import androidx.annotation.Nullable;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class NetworkUtils {
private static final String TAG = NetworkUtils.class.getSimpleName();
/**
* Returns true if device is connected to wifi or mobile network, false
* otherwise.
*
* @param context
* @return
*/
public static boolean isConnected( Context context ) {
if ( context == null ) {
return false;
}
ConnectivityManager conMan = (ConnectivityManager) context
.getSystemService( Context.CONNECTIVITY_SERVICE );
if ( conMan == null ) {
return false;
}
NetworkInfo infoWifi = conMan.getNetworkInfo( ConnectivityManager.TYPE_WIFI );
if ( infoWifi != null ) {
NetworkInfo.State wifi = infoWifi.getState();
if ( wifi == NetworkInfo.State.CONNECTED ) {
return true;
}
}
NetworkInfo infoMobile = conMan.getNetworkInfo( ConnectivityManager.TYPE_MOBILE );
if ( infoMobile != null ) {
NetworkInfo.State mobile = infoMobile.getState();
if ( mobile == NetworkInfo.State.CONNECTED ) {
return true;
}
}
return false;
}
/**
* Check if there is any connectivity to a Wifi network
*
* @param context
* @return
*/
public static boolean isConnectedWifi( Context context ) {
if ( context == null ) {
return false;
}
NetworkInfo info = getNetworkInfo( context );
return ( info != null && info.isConnected() && info.getType() == ConnectivityManager.TYPE_WIFI );
}
/**
* Check if there is any connectivity to a mobile network
*
* @param context
* @return
*/
public static boolean isConnectedMobile( Context context ) {
if ( context == null ) {
return false;
}
NetworkInfo info = getNetworkInfo( context );
return ( info != null && info.isConnected() && info.getType() == ConnectivityManager.TYPE_MOBILE );
}
/**
* Get the network info
*
* @param context
* @return
*/
@Nullable
public static NetworkInfo getNetworkInfo( Context context ) {
if ( context == null ) {
return null;
}
ConnectivityManager cm = (ConnectivityManager) context.getSystemService( Context.CONNECTIVITY_SERVICE );
if ( cm != null ) {
return cm.getActiveNetworkInfo();
}
return null;
}
/**
* URL
*
* @param url
* @return true false
*/
public static boolean isNetworkUrl( String url ) {
String regex = "^(https?|ftp|file)://[-a-zA-Z0-9+&@#/%?=~_|!:,.;]*[-a-zA-Z0-9+&@#/%=~_|]";
Pattern patt = Pattern.compile( regex );
Matcher matcher = patt.matcher( url );
return matcher.matches();
}
}

View File

@@ -0,0 +1,12 @@
package com.mogo.utils;
/**
* @author congtaowang
* @since 2019-10-02
* <p>
* recyclerview item 点击回调
*/
public interface OnItemClickedListener< T > {
void onItemClicked(T obj);
}

View File

@@ -0,0 +1,177 @@
package com.mogo.utils.statusbar;
import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.os.Build;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import androidx.core.view.ViewCompat;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
public class Eyes {
public static void setStatusBarColor( Activity activity, int statusColor ) {
if ( Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP ) {
EyesLollipop.setStatusBarColor( activity, statusColor );
} else if ( Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT ) {
EyesKitKat.setStatusBarColor( activity, statusColor );
}
}
public static void translucentStatusBar( Activity activity ) {
translucentStatusBar( activity, false );
}
public static void translucentStatusBar( Activity activity, boolean hideStatusBarBackground ) {
if ( Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP ) {
EyesLollipop.translucentStatusBar( activity, hideStatusBarBackground );
} else if ( Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT ) {
EyesKitKat.translucentStatusBar( activity );
}
}
//
// public static void setStatusBarColorForCollapsingToolbar(@NonNull Activity activity, AppBarLayout appBarLayout, CollapsingToolbarLayout collapsingToolbarLayout,
// Toolbar toolbar, @ColorInt int statusColor) {
// if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
// EyesLollipop.setStatusBarColorForCollapsingToolbar(activity, appBarLayout, collapsingToolbarLayout, toolbar, statusColor);
// } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
// EyesKitKat.setStatusBarColorForCollapsingToolbar(activity, appBarLayout, collapsingToolbarLayout, toolbar, statusColor);
// }
// }
public static void setStatusBarLightMode( Activity activity, int color ) {
if ( Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT ) {
//判断是否为小米或魅族手机,如果是则将状态栏文字改为黑色
if ( MIUISetStatusBarLightMode( activity, true ) || FlymeSetStatusBarLightMode( activity, true ) ) {
//设置状态栏为指定颜色
if ( Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP ) {//5.0
activity.getWindow().setStatusBarColor( color );
} else {//4.4
//调用修改状态栏颜色的方法
setStatusBarColor( activity, color );
}
} else if ( Build.VERSION.SDK_INT >= Build.VERSION_CODES.M ) {
//如果是6.0以上将状态栏文字改为黑色,并设置状态栏颜色
activity.getWindow().setBackgroundDrawableResource( android.R.color.transparent );
activity.getWindow().getDecorView().setSystemUiVisibility( View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR );
activity.getWindow().setStatusBarColor( color );
//fitsSystemWindow 为 false, 不预留系统栏位置.
ViewGroup mContentView = activity.getWindow().findViewById( Window.ID_ANDROID_CONTENT );
View mChildView = mContentView.getChildAt( 0 );
if ( mChildView != null ) {
mChildView.setFitsSystemWindows( true );
ViewCompat.requestApplyInsets( mChildView );
}
}
}
}
//
// public static void setStatusBarLightForCollapsingToolbar(Activity activity, AppBarLayout appBarLayout,
// CollapsingToolbarLayout collapsingToolbarLayout, Toolbar toolbar, int statusBarColor) {
// if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
// EyesLollipop.setStatusBarWhiteForCollapsingToolbar(activity, appBarLayout, collapsingToolbarLayout, toolbar, statusBarColor);
// } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
// EyesKitKat.setStatusBarWhiteForCollapsingToolbar(activity, appBarLayout, collapsingToolbarLayout, toolbar, statusBarColor);
// }
// }
/**
* MIUI的沉浸支持透明白色字体和透明黑色字体
* https://dev.mi.com/console/doc/detail?pId=1159
*/
static boolean MIUISetStatusBarLightMode( Activity activity, boolean darkmode ) {
try {
Class< ? > layoutParams = Class.forName( "android.view.MiuiWindowManager$LayoutParams" );
Window window = activity.getWindow();
window.addFlags( WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS );
window.clearFlags( WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS );
window.getDecorView().setSystemUiVisibility( View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR );
Class< ? extends Window> clazz = activity.getWindow().getClass();
Field field = layoutParams.getField( "EXTRA_FLAG_STATUS_BAR_DARK_MODE" );
int darkModeFlag = field.getInt( layoutParams );
Method extraFlagField = clazz.getMethod( "setExtraFlags", int.class, int.class );
extraFlagField.invoke( activity.getWindow(), darkmode ? darkModeFlag : 0, darkModeFlag );
return true;
} catch ( Exception e ) {
}
return false;
}
/**
* 设置状态栏图标为深色和魅族特定的文字风格Flyme4.0以上
*/
static boolean FlymeSetStatusBarLightMode( Activity activity, boolean darkmode ) {
try {
WindowManager.LayoutParams lp = activity.getWindow().getAttributes();
Field darkFlag = WindowManager.LayoutParams.class
.getDeclaredField( "MEIZU_FLAG_DARK_STATUS_BAR_ICON" );
Field meizuFlags = WindowManager.LayoutParams.class
.getDeclaredField( "meizuFlags" );
darkFlag.setAccessible( true );
meizuFlags.setAccessible( true );
int bit = darkFlag.getInt( null );
int value = meizuFlags.getInt( lp );
if ( darkmode ) {
value |= bit;
} else {
value &= ~bit;
}
meizuFlags.setInt( lp, value );
activity.getWindow().setAttributes( lp );
return true;
} catch ( Exception e ) {
}
return false;
}
static void setContentTopPadding( Activity activity, int padding ) {
ViewGroup mContentView = activity.getWindow().findViewById( Window.ID_ANDROID_CONTENT );
mContentView.setPadding( 0, padding, 0, 0 );
}
static int getPxFromDp( Context context, float dp ) {
return ( int ) TypedValue.applyDimension( TypedValue.COMPLEX_UNIT_DIP, dp, context.getResources().getDisplayMetrics() );
}
/**
* 设置状态栏颜色
*
* @param activity
* @param isBlackText true黑色 false 白字
*/
public static void setStatusBarStyle( Activity activity, boolean isBlackText, boolean isFitSystem ) {
setStatusBarStyle( activity, isBlackText, isBlackText ? Color.WHITE : Color.TRANSPARENT, isFitSystem );
}
public static void setStatusBarStyle( Activity activity, boolean isBlackText, int statusBarBgColor, boolean isFitSystem ) {
if ( Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP ) {
activity.getWindow().setBackgroundDrawableResource( android.R.color.transparent );
if ( isBlackText ) {
activity.getWindow().getDecorView().setSystemUiVisibility( ( isFitSystem ? View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN : View.SYSTEM_UI_FLAG_VISIBLE ) | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR );
} else {
activity.getWindow().getDecorView().setSystemUiVisibility( ( isFitSystem ? View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN : View.SYSTEM_UI_FLAG_VISIBLE ) );
}
activity.getWindow().setStatusBarColor( statusBarBgColor );
}
}
public static void setMessageFragmentStatus( Activity activity ) {
if ( Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP ) {
activity.getWindow().setBackgroundDrawableResource( android.R.color.transparent );
activity.getWindow().getDecorView().setSystemUiVisibility( View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR );
}
}
}

View File

@@ -0,0 +1,277 @@
package com.mogo.utils.statusbar;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.os.Build;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.FrameLayout;
import com.mogo.utils.R;
@TargetApi( Build.VERSION_CODES.KITKAT )
class EyesKitKat {
private static final String TAG_FAKE_STATUS_BAR_VIEW = "statusBarView";
private static final String TAG_MARGIN_ADDED = "marginAdded";
static void setStatusBarColor( Activity activity, int statusColor ) {
Window window = activity.getWindow();
//设置Window为全透明
window.addFlags( WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS );
ViewGroup mContentView = window.findViewById( Window.ID_ANDROID_CONTENT );
//获取父布局
View mContentChild = mContentView.getChildAt( 0 );
//获取状态栏高度
int statusBarHeight = getStatusBarHeight( activity );
//如果已经存在假状态栏则移除,防止重复添加
removeFakeStatusBarViewIfExist( activity );
//添加一个View来作为状态栏的填充
addFakeStatusBarView( activity, statusColor, statusBarHeight );
//设置子控件到状态栏的间距
addMarginTopToContentChild( mContentChild, statusBarHeight );
//不预留系统栏位置
if ( mContentChild != null ) {
mContentChild.setFitsSystemWindows( false );
}
//如果在Activity中使用了ActionBar则需要再将布局与状态栏的高度跳高一个ActionBar的高度否则内容会被ActionBar遮挡
int action_bar_id = activity.getResources().getIdentifier( "action_bar", "id", activity.getPackageName() );
View view = activity.findViewById( action_bar_id );
if ( view != null ) {
TypedValue typedValue = new TypedValue();
if ( activity.getTheme().resolveAttribute( R.attr.actionBarSize, typedValue, true ) ) {
int actionBarHeight = TypedValue.complexToDimensionPixelSize( typedValue.data, activity.getResources().getDisplayMetrics() );
Eyes.setContentTopPadding( activity, actionBarHeight );
}
}
}
static void translucentStatusBar( Activity activity ) {
Window window = activity.getWindow();
//设置Window为透明
window.addFlags( WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS );
ViewGroup mContentView = activity.findViewById( Window.ID_ANDROID_CONTENT );
View mContentChild = mContentView.getChildAt( 0 );
//移除已经存在假状态栏则,并且取消它的Margin间距
removeFakeStatusBarViewIfExist( activity );
removeMarginTopOfContentChild( mContentChild, getStatusBarHeight( activity ) );
if ( mContentChild != null ) {
//fitsSystemWindow 为 false, 不预留系统栏位置.
mContentChild.setFitsSystemWindows( false );
}
}
// static void setStatusBarColorForCollapsingToolbar( final Activity activity, final AppBarLayout appBarLayout, final CollapsingToolbarLayout collapsingToolbarLayout,
// Toolbar toolbar, int statusColor ) {
// Window window = activity.getWindow();
// //设置Window为全透明
// window.addFlags( WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS );
// ViewGroup mContentView = window.findViewById( Window.ID_ANDROID_CONTENT );
//
// //AppBarLayout,CollapsingToolbarLayout,ToolBar,ImageView的fitsSystemWindow统一改为false, 不预留系统栏位置.
// View mContentChild = mContentView.getChildAt( 0 );
// mContentChild.setFitsSystemWindows( false );
// ( ( View ) appBarLayout.getParent() ).setFitsSystemWindows( false );
// appBarLayout.setFitsSystemWindows( false );
// collapsingToolbarLayout.setFitsSystemWindows( false );
// collapsingToolbarLayout.getChildAt( 0 ).setFitsSystemWindows( false );
//
// toolbar.setFitsSystemWindows( false );
// //为Toolbar添加一个状态栏的高度, 同时为Toolbar添加paddingTop,使Toolbar覆盖状态栏ToolBar的title可以正常显示.
// if ( toolbar.getTag() == null ) {
// CollapsingToolbarLayout.LayoutParams lp = ( CollapsingToolbarLayout.LayoutParams ) toolbar.getLayoutParams();
// int statusBarHeight = getStatusBarHeight( activity );
// lp.height += statusBarHeight;
// toolbar.setLayoutParams( lp );
// toolbar.setPadding( toolbar.getPaddingLeft(), toolbar.getPaddingTop() + statusBarHeight, toolbar.getPaddingRight(), toolbar.getPaddingBottom() );
// toolbar.setTag( true );
// }
// //移除已经存在假状态栏则,并且取消它的Margin间距
// int statusBarHeight = getStatusBarHeight( activity );
// removeFakeStatusBarViewIfExist( activity );
// removeMarginTopOfContentChild( mContentChild, statusBarHeight );
// //添加一个View来作为状态栏的填充
// final View statusView = addFakeStatusBarView( activity, statusColor, statusBarHeight );
//
// CoordinatorLayout.Behavior behavior = ( ( CoordinatorLayout.LayoutParams ) appBarLayout.getLayoutParams() ).getBehavior();
// if ( behavior != null && behavior instanceof AppBarLayout.Behavior ) {
// int verticalOffset = ( ( AppBarLayout.Behavior ) behavior ).getTopAndBottomOffset();
// if ( Math.abs( verticalOffset ) > appBarLayout.getHeight() - collapsingToolbarLayout.getScrimVisibleHeightTrigger() ) {
// statusView.setAlpha( 1f );
// } else {
// statusView.setAlpha( 0f );
// }
// } else {
// statusView.setAlpha( 0f );
// }
// appBarLayout.addOnOffsetChangedListener( new AppBarLayout.OnOffsetChangedListener() {
// @Override
// public void onOffsetChanged( AppBarLayout appBarLayout, int verticalOffset ) {
// if ( Math.abs( verticalOffset ) > appBarLayout.getHeight() - collapsingToolbarLayout.getScrimVisibleHeightTrigger() ) {
// //toolbar被折叠时显示状态栏
// if ( statusView.getAlpha() == 0 ) {
// statusView.animate().cancel();
// statusView.animate().alpha( 1f ).setDuration( collapsingToolbarLayout.getScrimAnimationDuration() ).start();
// }
// } else {
// //toolbar展开时显示状态栏
// if ( statusView.getAlpha() == 1 ) {
// statusView.animate().cancel();
// statusView.animate().alpha( 0f ).setDuration( collapsingToolbarLayout.getScrimAnimationDuration() ).start();
// }
// }
// }
// } );
// }
//
// static void setStatusBarWhiteForCollapsingToolbar( final Activity activity, AppBarLayout appBarLayout, final CollapsingToolbarLayout collapsingToolbarLayout, Toolbar toolbar, final int statusBarColor ) {
// final Window window = activity.getWindow();
// window.addFlags( WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS );
//
// ViewGroup mContentView = window.findViewById( Window.ID_ANDROID_CONTENT );
// View mContentChild = mContentView.getChildAt( 0 );
// mContentChild.setFitsSystemWindows( false );
// ( ( View ) appBarLayout.getParent() ).setFitsSystemWindows( false );
// appBarLayout.setFitsSystemWindows( false );
// collapsingToolbarLayout.setFitsSystemWindows( false );
// collapsingToolbarLayout.getChildAt( 0 ).setFitsSystemWindows( false );
// toolbar.setFitsSystemWindows( false );
//
// if ( toolbar.getTag() == null ) {
// CollapsingToolbarLayout.LayoutParams lp = ( CollapsingToolbarLayout.LayoutParams ) toolbar.getLayoutParams();
// int statusBarHeight = getStatusBarHeight( activity );
// lp.height += statusBarHeight;
// toolbar.setLayoutParams( lp );
// toolbar.setPadding( toolbar.getPaddingLeft(), toolbar.getPaddingTop() + statusBarHeight, toolbar.getPaddingRight(), toolbar.getPaddingBottom() );
// toolbar.setTag( true );
// }
//
// int statusBarHeight = getStatusBarHeight( activity );
// int color = Color.BLACK;
// try {
// Class< ? > layoutParams = Class.forName( "android.view.MiuiWindowManager$LayoutParams" );
// color = statusBarColor;
// } catch ( ClassNotFoundException e ) {
// e.printStackTrace();
// }
// try {
// Field darkFlag = WindowManager.LayoutParams.class.getDeclaredField( "MEIZU_FLAG_DARK_STATUS_BAR_ICON" );
// color = statusBarColor;
// } catch ( NoSuchFieldException e ) {
// e.printStackTrace();
// }
// final View statusView = addFakeStatusBarView( activity, color, statusBarHeight );
// CoordinatorLayout.Behavior behavior = ( ( CoordinatorLayout.LayoutParams ) appBarLayout.getLayoutParams() ).getBehavior();
// if ( behavior != null && behavior instanceof AppBarLayout.Behavior ) {
// int verticalOffset = ( ( AppBarLayout.Behavior ) behavior ).getTopAndBottomOffset();
// if ( Math.abs( verticalOffset ) > appBarLayout.getHeight() - collapsingToolbarLayout.getScrimVisibleHeightTrigger() ) {
// statusView.setAlpha( 1f );
// } else {
// statusView.setAlpha( 0f );
// }
// } else {
// statusView.setAlpha( 0f );
// }
//
// appBarLayout.addOnOffsetChangedListener( new AppBarLayout.OnOffsetChangedListener() {
// private final static int EXPANDED = 0;
// private final static int COLLAPSED = 1;
// private int appBarLayoutState;
//
// @Override
// public void onOffsetChanged( AppBarLayout appBarLayout, int verticalOffset ) {
// if ( Math.abs( verticalOffset ) >= ( appBarLayout.getTotalScrollRange() - Eyes.getPxFromDp( activity, 56 ) ) ) {
// if ( appBarLayoutState != COLLAPSED ) {
// appBarLayoutState = COLLAPSED;
//
// if ( Eyes.MIUISetStatusBarLightMode( activity, true ) || Eyes.FlymeSetStatusBarLightMode( activity, true ) ) {
// }
// if ( statusView.getAlpha() == 0 ) {
// statusView.animate().cancel();
// statusView.animate().alpha( 1f ).setDuration( collapsingToolbarLayout.getScrimAnimationDuration() ).start();
// }
// }
// } else {
// if ( appBarLayoutState != EXPANDED ) {
// appBarLayoutState = EXPANDED;
//
// if ( Eyes.MIUISetStatusBarLightMode( activity, false ) || Eyes.FlymeSetStatusBarLightMode( activity, false ) ) {
// }
// if ( statusView.getAlpha() == 1 ) {
// statusView.animate().cancel();
// statusView.animate().alpha( 0f ).setDuration( collapsingToolbarLayout.getScrimAnimationDuration() ).start();
// }
// translucentStatusBar( activity );
// }
// }
// }
// } );
// }
private static void removeFakeStatusBarViewIfExist( Activity activity ) {
Window window = activity.getWindow();
ViewGroup mDecorView = (ViewGroup) window.getDecorView();
View fakeView = mDecorView.findViewWithTag( TAG_FAKE_STATUS_BAR_VIEW );
if ( fakeView != null ) {
mDecorView.removeView( fakeView );
}
}
private static View addFakeStatusBarView( Activity activity, int statusBarColor, int statusBarHeight ) {
Window window = activity.getWindow();
ViewGroup mDecorView = (ViewGroup) window.getDecorView();
View mStatusBarView = new View( activity );
FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams( ViewGroup.LayoutParams.MATCH_PARENT, statusBarHeight );
layoutParams.gravity = Gravity.TOP;
mStatusBarView.setLayoutParams( layoutParams );
mStatusBarView.setBackgroundColor( statusBarColor );
mStatusBarView.setTag( TAG_FAKE_STATUS_BAR_VIEW );
mDecorView.addView( mStatusBarView );
return mStatusBarView;
}
private static void addMarginTopToContentChild( View mContentChild, int statusBarHeight ) {
if ( mContentChild == null ) {
return;
}
if ( !TAG_MARGIN_ADDED.equals( mContentChild.getTag() ) ) {
FrameLayout.LayoutParams lp = ( FrameLayout.LayoutParams ) mContentChild.getLayoutParams();
lp.topMargin += statusBarHeight;
mContentChild.setLayoutParams( lp );
mContentChild.setTag( TAG_MARGIN_ADDED );
}
}
private static int getStatusBarHeight( Context context ) {
int result = 0;
int resId = context.getResources().getIdentifier( "status_bar_height", "dimen", "android" );
if ( resId > 0 ) {
result = context.getResources().getDimensionPixelOffset( resId );
}
return result;
}
private static void removeMarginTopOfContentChild( View mContentChild, int statusBarHeight ) {
if ( mContentChild == null ) {
return;
}
if ( TAG_MARGIN_ADDED.equals( mContentChild.getTag() ) ) {
FrameLayout.LayoutParams lp = ( FrameLayout.LayoutParams ) mContentChild.getLayoutParams();
lp.topMargin -= statusBarHeight;
mContentChild.setLayoutParams( lp );
mContentChild.setTag( null );
}
}
}

View File

@@ -0,0 +1,219 @@
package com.mogo.utils.statusbar;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.os.Build;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import androidx.core.view.ViewCompat;
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
class EyesLollipop {
static void setStatusBarColor(Activity activity, int statusColor) {
Window window = activity.getWindow();
//取消状态栏透明
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
//添加Flag把状态栏设为可绘制模式
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
//设置状态栏颜色
window.setStatusBarColor(statusColor);
//设置系统状态栏处于可见状态
window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE);
//让view不根据系统窗口来调整自己的布局
ViewGroup mContentView = window.findViewById(Window.ID_ANDROID_CONTENT);
View mChildView = mContentView.getChildAt(0);
if (mChildView != null) {
mChildView.setFitsSystemWindows(false);
ViewCompat.requestApplyInsets(mChildView);
}
}
static void translucentStatusBar(Activity activity, boolean hideStatusBarBackground) {
Window window = activity.getWindow();
//添加Flag把状态栏设为可绘制模式
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
if (hideStatusBarBackground) {
//如果为全透明模式取消设置Window半透明的Flag
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
//设置状态栏为透明
window.setStatusBarColor(Color.TRANSPARENT);
//设置window的状态栏不可见
window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
} else {
//如果为半透明模式添加设置Window半透明的Flag
window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
//设置系统状态栏处于可见状态
window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE);
}
//view不根据系统窗口来调整自己的布局
ViewGroup mContentView = window.findViewById(Window.ID_ANDROID_CONTENT);
View mChildView = mContentView.getChildAt(0);
if (mChildView != null) {
mChildView.setFitsSystemWindows(false);
ViewCompat.requestApplyInsets(mChildView);
}
}
// static void setStatusBarColorForCollapsingToolbar(final Activity activity, final AppBarLayout appBarLayout, final CollapsingToolbarLayout collapsingToolbarLayout,
// Toolbar toolbar, final int statusColor) {
// final Window window = activity.getWindow();
// //取消设置Window半透明的Flag
// window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
// ////添加Flag把状态栏设为可绘制模式
// window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
// //设置状态栏为透明
// window.setStatusBarColor(Color.TRANSPARENT);
// //设置系统状态栏处于可见状态
// window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE);
// //通过OnApplyWindowInsetsListener()使Layout在绘制过程中将View向下偏移了,使collapsingToolbarLayout可以占据状态栏
// ViewCompat.setOnApplyWindowInsetsListener(collapsingToolbarLayout, new OnApplyWindowInsetsListener() {
// @Override
// public WindowInsetsCompat onApplyWindowInsets(View v, WindowInsetsCompat insets) {
// return insets;
// }
// });
//
// ViewGroup mContentView = window.findViewById(Window.ID_ANDROID_CONTENT);
// View mChildView = mContentView.getChildAt(0);
// //view不根据系统窗口来调整自己的布局
// if (mChildView != null) {
// mChildView.setFitsSystemWindows(false);
// ViewCompat.requestApplyInsets(mChildView);
// }
//
// ((View) appBarLayout.getParent()).setFitsSystemWindows(false);
// appBarLayout.setFitsSystemWindows(false);
// collapsingToolbarLayout.setFitsSystemWindows(false);
// collapsingToolbarLayout.getChildAt(0).setFitsSystemWindows(false);
// //设置状态栏的颜色
// collapsingToolbarLayout.setStatusBarScrimColor(statusColor);
// toolbar.setFitsSystemWindows(false);
// //为Toolbar添加一个状态栏的高度, 同时为Toolbar添加paddingTop,使Toolbar覆盖状态栏ToolBar的title可以正常显示.
// if (toolbar.getTag() == null) {
// CollapsingToolbarLayout.LayoutParams lp = (CollapsingToolbarLayout.LayoutParams) toolbar.getLayoutParams();
// int statusBarHeight = getStatusBarHeight(activity);
// lp.height += statusBarHeight;
// toolbar.setLayoutParams(lp);
// toolbar.setPadding(toolbar.getPaddingLeft(), toolbar.getPaddingTop() + statusBarHeight, toolbar.getPaddingRight(), toolbar.getPaddingBottom());
// toolbar.setTag(true);
// }
//
// appBarLayout.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {
// private final static int EXPANDED = 0;
// private final static int COLLAPSED = 1;
// private int appBarLayoutState;
//
// @Override
// public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
// //toolbar被折叠时显示状态栏
// if (Math.abs(verticalOffset) > collapsingToolbarLayout.getScrimVisibleHeightTrigger()) {
// if (appBarLayoutState != COLLAPSED) {
// appBarLayoutState = COLLAPSED;//修改状态标记为折叠
// setStatusBarColor(activity, statusColor);
// }
// } else {
// //toolbar显示时同时显示状态栏
// if (appBarLayoutState != EXPANDED) {
// appBarLayoutState = EXPANDED;//修改状态标记为展开
// translucentStatusBar(activity, true);
// }
// }
// }
// });
// }
//
// static void setStatusBarWhiteForCollapsingToolbar(final Activity activity, final AppBarLayout appBarLayout, final CollapsingToolbarLayout collapsingToolbarLayout, final Toolbar toolbar, final int statusBarColor) {
// final Window window = activity.getWindow();
// window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
// window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
// window.setStatusBarColor(Color.TRANSPARENT);
// window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE);
//
// ViewCompat.setOnApplyWindowInsetsListener(collapsingToolbarLayout, new OnApplyWindowInsetsListener() {
// @Override
// public WindowInsetsCompat onApplyWindowInsets(View v, WindowInsetsCompat insets) {
// return insets;
// }
// });
//
// ViewGroup mContentView = window.findViewById(Window.ID_ANDROID_CONTENT);
// View mChildView = mContentView.getChildAt(0);
// if (mChildView != null) {
// mChildView.setFitsSystemWindows(false);
// ViewCompat.requestApplyInsets(mChildView);
// }
//
// ((View) appBarLayout.getParent()).setFitsSystemWindows(false);
// appBarLayout.setFitsSystemWindows(false);
// toolbar.setFitsSystemWindows(false);
// if (toolbar.getTag() == null) {
// CollapsingToolbarLayout.LayoutParams lp = (CollapsingToolbarLayout.LayoutParams) toolbar.getLayoutParams();
// int statusBarHeight = getStatusBarHeight(activity);
// lp.height += statusBarHeight;
// toolbar.setLayoutParams(lp);
// toolbar.setPadding(toolbar.getPaddingLeft(), toolbar.getPaddingTop() + statusBarHeight, toolbar.getPaddingRight(), toolbar.getPaddingBottom());
// toolbar.setTag(true);
// }
//
// collapsingToolbarLayout.setFitsSystemWindows(false);
// collapsingToolbarLayout.getChildAt(0).setFitsSystemWindows(false);
// collapsingToolbarLayout.setStatusBarScrimColor(statusBarColor);
// appBarLayout.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {
// private final static int EXPANDED = 0;
// private final static int COLLAPSED = 1;
// private int appBarLayoutState;
//
// @Override
// public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
// //toolbar被折叠时显示状态栏
// if (Math.abs(verticalOffset) > collapsingToolbarLayout.getScrimVisibleHeightTrigger()) {
// if (appBarLayoutState != COLLAPSED) {
// appBarLayoutState = COLLAPSED;//修改状态标记为折叠
//
// //先判断是否为小米设备设置状态栏不成功判断是否为6.0以上设备不是6.0以上设备再判断是否为魅族设备,不是魅族设备就只设置状态栏颜色
// if (Eyes.MIUISetStatusBarLightMode(activity, true)) {
// return;
// }
// if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
// activity.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
// activity.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
// activity.getWindow().setStatusBarColor(statusBarColor);
// } else if (!Eyes.FlymeSetStatusBarLightMode(activity, true)) {
// setStatusBarColor(activity, statusBarColor);
// }
// }
// } else {
// //toolbar显示时同时显示状态栏
// if (appBarLayoutState != EXPANDED) {
// appBarLayoutState = EXPANDED;//修改状态标记为展开
//
// if (Eyes.MIUISetStatusBarLightMode(activity, false)) {
// translucentStatusBar(activity, true);
// return;
// }
// if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
// activity.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
// } else if (Eyes.FlymeSetStatusBarLightMode(activity, true)) {
// }
// translucentStatusBar(activity, true);
// }
// }
// }
// });
// }
private static int getStatusBarHeight(Context context) {
int result = 0;
int resId = context.getResources().getIdentifier("status_bar_height", "dimen", "android");
if (resId > 0) {
result = context.getResources().getDimensionPixelOffset(resId);
}
return result;
}
}