diff --git a/README.md b/README.md index 30fdb35..0496916 100644 --- a/README.md +++ b/README.md @@ -7,18 +7,18 @@ > android studio ```groovy - compile 'com.gyf.barlibrary:barlibrary:2.2.1' + compile 'com.gyf.barlibrary:barlibrary:2.2.2' ``` >eclipse -[barlibrary-2.2.1.jar](https://github.com/gyf-dev/ImmersionBar/blob/master/jar/barlibrary-2.2.1.jar) +[barlibrary-2.2.2.jar](https://github.com/gyf-dev/ImmersionBar/blob/master/jar/barlibrary-2.2.2.jar) ## 版本说明 ### [点我](https://github.com/gyf-dev/ImmersionBar/wiki) ## 下载demo -### [下载](https://github.com/gyf-dev/ImmersionBar/blob/master/apk/sample-2.2.1.apk) +### [下载](https://github.com/gyf-dev/ImmersionBar/blob/master/apk/sample-2.2.2.apk) ## 用法 ### 初始化 @@ -46,6 +46,7 @@ .hideBar(BarHide.FLAG_HIDE_BAR) //隐藏状态栏或导航栏或两者,不写默认不隐藏 .setViewSupportTransformColor(toolbar) //设置支持view变色,支持一个view,不指定颜色,默认和状态栏同色,还有两个重载方法 .addViewSupportTransformColor(toolbar) //设置支持view变色,可以添加多个view,不指定颜色,默认和状态栏同色,还有两个重载方法 + .titleBar(toolBar) //解决状态栏和布局重叠问题 .statusBarView(view) //解决状态栏和布局重叠问题 .fitsSystemWindows(false) //解决状态栏和布局重叠问题,默认为false,当为true时一定要指定statusBarColor(),不然状态栏为透明色 .statusBarColorTransform(R.color.orange) //状态栏变色后的颜色 @@ -140,7 +141,7 @@ } @Override protected void immersionInit() { - ImmersionBar.with(getActivity()) + ImmersionBar.with(this) .statusBarDarkFont(false) .navigationBarColor(R.color.btn4) .init(); @@ -148,7 +149,7 @@ } ``` -## 状态栏与布局顶部重叠解决方案,四种方案任选其一 +## 状态栏与布局顶部重叠解决方案,五种方案任选其一 - ① 使用dimen自定义状态栏高度 在values-v19/dimens.xml文件下 @@ -240,7 +241,13 @@ .statusBarView(view) .init(); ``` - +- ⑤ 使用ImmersionBar的titleBar(View view)方法 + ```java + ImmersionBar.with(this) + .titleBar(view) //指定标题栏view + .init(); + ``` + ## 解决EditText和软键盘的问题 ```java KeyboardPatch.patch(this, linearLayout).enable(); //解决底部EditText和软键盘的问题,linearLayout指的是当前布局的根节点 @@ -250,13 +257,11 @@ ## 当白色背景状态栏遇到不能改变状态栏字体为深色的设备时,解决方案 ```java - if(ImmersionBar.isSupportStatusBarDarkFont()){ //判断当前设备支不支持状态栏字体变色 - //处理状态栏字体为黑色 - }else { - //处理状态栏有透明度 - } + ImmersionBar.with(this) + .statusBarDarkFont(true, 0.2f) + .init(); ``` - + ## 状态栏和导航栏其它方法 @@ -304,6 +309,6 @@ * 感谢[zhangzhen92](https://github.com/zhangzhen92)远程协助测试 ## 联系我 ## -- QQ 969565471 +- QQ群 314360549(沉浸式交流) - WeChat(微信) \ No newline at end of file diff --git a/apk/sample-2.2.1.apk b/apk/sample-2.2.1.apk deleted file mode 100644 index 2171eeb..0000000 Binary files a/apk/sample-2.2.1.apk and /dev/null differ diff --git a/apk/sample-2.2.2.apk b/apk/sample-2.2.2.apk new file mode 100644 index 0000000..c6cc1cb Binary files /dev/null and b/apk/sample-2.2.2.apk differ diff --git a/barlibrary/build.gradle b/barlibrary/build.gradle index 85e84a4..4e215cd 100644 --- a/barlibrary/build.gradle +++ b/barlibrary/build.gradle @@ -38,11 +38,11 @@ dependencies { } task makeJar(type: Copy) { - delete 'build/libs/barlibrary-2.2.0.jar' - from('build/intermediates/bundles/release/') + delete 'build/libs/barlibrary-2.2.1.jar' + from('build/intermediates/bundles/default/') into('build/libs/') include('classes.jar') - rename('classes.jar', 'barlibrary-2.2.1.jar') + rename('classes.jar', 'barlibrary-2.2.2.jar') } makeJar.dependsOn(build) @@ -51,7 +51,7 @@ publish { userOrg = 'magic-dev' groupId = 'com.gyf.barlibrary' artifactId = 'barlibrary' - publishVersion = '2.2.1' + publishVersion = '2.2.2' desc = 'Android bar management' website = 'https://github.com/gyf-dev/ImmersionBar' } diff --git a/barlibrary/src/main/AndroidManifest.xml b/barlibrary/src/main/AndroidManifest.xml index f0b7cf7..c9db85d 100644 --- a/barlibrary/src/main/AndroidManifest.xml +++ b/barlibrary/src/main/AndroidManifest.xml @@ -6,7 +6,6 @@ android:allowBackup="true" android:label="@string/app_name" android:supportsRtl="true"> - diff --git a/barlibrary/src/main/java/com/gyf/barlibrary/BarParams.java b/barlibrary/src/main/java/com/gyf/barlibrary/BarParams.java index acc5d6a..8eab63b 100644 --- a/barlibrary/src/main/java/com/gyf/barlibrary/BarParams.java +++ b/barlibrary/src/main/java/com/gyf/barlibrary/BarParams.java @@ -11,9 +11,10 @@ import java.util.Map; /** + * 沉浸式参数信息 * Created by geyifeng on 2017/5/9. */ -public class BarParams { +public class BarParams implements Cloneable { @ColorInt public int statusBarColor = Color.TRANSPARENT; //状态栏颜色 @ColorInt @@ -26,6 +27,7 @@ public class BarParams { public boolean fullScreenTemp = fullScreen; public BarHide barHide = BarHide.FLAG_SHOW_BAR; //隐藏Bar public boolean darkFont = false; //状态栏字体深色与亮色标志位 + public boolean statusBarFlag = false; //是否可以修改状态栏颜色 @ColorInt public int statusBarColorTransform = Color.BLACK; //状态栏变换后的颜色 @ColorInt @@ -36,6 +38,8 @@ public class BarParams { public int viewColorBeforeTransform = statusBarColor; //view变色前的颜色 @ColorInt public int viewColorAfterTransform = statusBarColorTransform; //view变色后的颜色 + @FloatRange(from = 0f, to = 1f) + public float viewAlpha = 0.0f; public boolean fits = false; //解决标题栏与状态栏重叠问题 public int navigationBarColorTem = navigationBarColor; public View statusBarView; //4.4自定义一个状态栏 @@ -43,4 +47,18 @@ public class BarParams { public View statusBarViewByHeight; //解决标题栏与状态栏重叠问题 @ColorInt public int flymeOSStatusBarFontColor; //flymeOS状态栏字体变色 + public boolean isSupportActionBar = false; //结合actionBar使用 + public View titleBarView; //标题栏view + public int titleBarHeight; //标题栏的高度 + + @Override + protected BarParams clone() { + BarParams barParams = null; + try { + barParams = (BarParams) super.clone(); + } catch (CloneNotSupportedException e) { + e.printStackTrace(); + } + return barParams; + } } diff --git a/barlibrary/src/main/java/com/gyf/barlibrary/ImmersionBar.java b/barlibrary/src/main/java/com/gyf/barlibrary/ImmersionBar.java index 075c1c0..62d86da 100644 --- a/barlibrary/src/main/java/com/gyf/barlibrary/ImmersionBar.java +++ b/barlibrary/src/main/java/com/gyf/barlibrary/ImmersionBar.java @@ -9,6 +9,7 @@ import android.support.annotation.FloatRange; import android.support.annotation.IdRes; import android.support.annotation.RequiresApi; +import android.support.v4.app.Fragment; import android.support.v4.content.ContextCompat; import android.support.v4.graphics.ColorUtils; import android.view.Gravity; @@ -20,7 +21,9 @@ import java.lang.reflect.Field; import java.lang.reflect.Method; +import java.util.ArrayList; import java.util.HashMap; +import java.util.List; import java.util.Map; import java.util.Set; @@ -32,6 +35,9 @@ public class ImmersionBar { private static Map mMap = new HashMap<>(); + private static Map mTagMap = new HashMap<>(); + private static Map> mTagKeyMap = new HashMap<>(); + private static List mFragmentList = new ArrayList<>(); private Activity mActivity; private Window mWindow; private ViewGroup mViewGroup; @@ -39,29 +45,66 @@ public class ImmersionBar { private ViewGroup mContentView; private BarConfig mConfig; - + private String mActivityName; + private String mFragmentName; private ImmersionBar() { } + /** + * Instantiates a new Immersion bar. + * + * @param fragment the fragment + */ + private ImmersionBar(Fragment fragment) { + mActivityName = fragment.getActivity().getClass().getName(); + mFragmentName = mActivityName + "_and_" + fragment.getClass().getName(); + if (!mFragmentList.contains(mFragmentName)) + mFragmentList.add(mFragmentName); + initParams(fragment.getActivity(), mFragmentName); + } + + /** + * Instantiates a new Immersion bar. + * + * @param activity the activity + */ private ImmersionBar(Activity activity) { + mActivityName = activity.getClass().getName(); + initParams(activity, mActivityName); + } + /** + * 初始化沉浸式默认参数 + * Init params. + * + * @param activity the activity + * @param name the name + */ + private void initParams(Activity activity, String name) { mActivity = activity; - mWindow = activity.getWindow(); + mWindow = mActivity.getWindow(); mViewGroup = (ViewGroup) mWindow.getDecorView(); mContentView = (ViewGroup) mActivity.findViewById(android.R.id.content); mConfig = new BarConfig(activity); - - if (!mMap.isEmpty()) { - if (mMap.get(mActivity.getClass().getName()) == null) { + if (!mMap.isEmpty() && !name.isEmpty()) { + if (mMap.get(name) == null) { mBarParams = new BarParams(); - mMap.put(mActivity.getClass().getName(), mBarParams); + if (mFragmentName != null) { //保证一个activity页面有同一个状态栏view和导航栏view + mBarParams.statusBarView = mMap.get(mActivityName).statusBarView; + mBarParams.navigationBarView = mMap.get(mActivityName).navigationBarView; + } + mMap.put(name, mBarParams); } else { - mBarParams = mMap.get(mActivity.getClass().getName()); + mBarParams = mMap.get(name); } } else { mBarParams = new BarParams(); - mMap.put(mActivity.getClass().getName(), mBarParams); + if (mFragmentName != null) { //保证一个activity页面有同一个状态栏view和导航栏view + mBarParams.statusBarView = mMap.get(mActivityName).statusBarView; + mBarParams.navigationBarView = mMap.get(mActivityName).navigationBarView; + } + mMap.put(name, mBarParams); } } @@ -75,6 +118,16 @@ public static ImmersionBar with(Activity activity) { return new ImmersionBar(activity); } + /** + * With immersion bar. + * + * @param fragment the fragment + * @return the immersion bar + */ + public static ImmersionBar with(Fragment fragment) { + return new ImmersionBar(fragment); + } + /** * 透明状态栏,默认透明 * @@ -750,6 +803,11 @@ public ImmersionBar addViewSupportTransformColorInt(View view, @ColorInt int vie return this; } + public ImmersionBar viewAlpha(@FloatRange(from = 0f, to = 1f) float viewAlpha) { + mBarParams.viewAlpha = viewAlpha; + return this; + } + /** * 移除view支持变身 * @@ -845,6 +903,27 @@ public ImmersionBar statusBarDarkFont(boolean isDarkFont) { mBarParams.darkFont = isDarkFont; if (!isDarkFont) mBarParams.flymeOSStatusBarFontColor = 0; + mBarParams.statusBarAlpha = 0; + return this; + } + + /** + * 状态栏字体深色或亮色,判断设备支不支持状态栏变色来设置状态栏透明度 + * Status bar dark font immersion bar. + * + * @param isDarkFont the is dark font + * @param statusAlpha the status alpha 如果不支持状态栏字体变色可以使用statusAlpha来指定状态栏透明度,比如白色状态栏的时候可以用到 + * @return the immersion bar + */ + public ImmersionBar statusBarDarkFont(boolean isDarkFont, @FloatRange(from = 0f, to = 1f) float statusAlpha) { + mBarParams.darkFont = isDarkFont; + if (!isDarkFont) + mBarParams.flymeOSStatusBarFontColor = 0; + if (!isSupportStatusBarDarkFont()) { + mBarParams.statusBarAlpha = statusAlpha; + } else { + mBarParams.statusBarAlpha = 0; + } return this; } @@ -928,13 +1007,147 @@ public ImmersionBar statusBarView(View view) { } /** - * 通过状态栏高度动态设置状态栏布局,只支持在activity里设置 + * 通过状态栏高度动态设置状态栏布局 * * @param viewId the view id * @return the immersion bar */ public ImmersionBar statusBarView(@IdRes int viewId) { - mBarParams.statusBarViewByHeight = mActivity.findViewById(viewId); + return statusBarView(mActivity.findViewById(viewId)); + } + + /** + * 通过状态栏高度动态设置状态栏布局 + * Status bar view immersion bar. + * + * @param viewId the view id + * @param rootView the root view + * @return the immersion bar + */ + public ImmersionBar statusBarView(@IdRes int viewId, View rootView) { + return statusBarView(rootView.findViewById(viewId)); + } + + /** + * 支持有actionBar的界面,调用该方法,布局讲从actionBar下面开始绘制 + * Support action bar immersion bar. + * + * @param isSupportActionBar the is support action bar + * @return the immersion bar + */ + public ImmersionBar supportActionBar(boolean isSupportActionBar) { + mBarParams.isSupportActionBar = isSupportActionBar; + return this; + } + + /** + * 解决状态栏与布局顶部重叠又多了种方法 + * Title bar immersion bar. + * + * @param view the view + * @return the immersion bar + */ + public ImmersionBar titleBar(View view) { + return titleBar(view, false); + } + + /** + * 解决状态栏与布局顶部重叠又多了种方法 + * Title bar immersion bar. + * + * @param view the view + * @param statusBarFlag the status bar flag false表示状态栏不支持变色,true表示状态栏支持变色 + * @return the immersion bar + */ + public ImmersionBar titleBar(View view, boolean statusBarFlag) { + mBarParams.titleBarView = view; + mBarParams.statusBarFlag = statusBarFlag; + setTitleBar(); + return this; + } + + /** + * 解决状态栏与布局顶部重叠又多了种方法,只支持Activity + * Title bar immersion bar. + * + * @param viewId the view id + * @return the immersion bar + */ + public ImmersionBar titleBar(@IdRes int viewId) { + View view = mActivity.findViewById(viewId); + return titleBar(view, false); + } + + /** + * 解决状态栏与布局顶部重叠又多了种方法,支持任何view + * Title bar immersion bar. + * + * @param viewId the view id + * @param rootView the root view + * @param statusBarFlag the status bar flag false表示状态栏不支持变色,true表示状态栏支持变色 + * @return the immersion bar + */ + public ImmersionBar titleBar(@IdRes int viewId, View rootView, boolean statusBarFlag) { + View view = rootView.findViewById(viewId); + return titleBar(view, statusBarFlag); + } + + /** + * 一键重置所有参数 + * Reset immersion bar. + * + * @return the immersion bar + */ + public ImmersionBar reset() { + BarParams barParamsTemp = mBarParams; + mBarParams = new BarParams(); + if (Build.VERSION.SDK_INT == Build.VERSION_CODES.KITKAT || OSUtils.isEMUI3_1()) { + mBarParams.statusBarView = barParamsTemp.statusBarView; + mBarParams.navigationBarView = barParamsTemp.navigationBarView; + } + mMap.put(mActivityName, mBarParams); + return this; + } + + /** + * 给某个页面设置tag来标识这页bar的属性. + * Add tag bar tag. + * + * @param tag the tag + * @return the bar tag + */ + public ImmersionBar addTag(String tag) { + tag = mActivityName + "_TAG_" + tag; + if (!isEmpty(tag)) { + BarParams barParams = mBarParams.clone(); + mTagMap.put(tag, barParams); + ArrayList tagList = mTagKeyMap.get(mActivityName); + if (tagList != null) { + if (!tagList.contains(tag)) + tagList.add(tag); + } else { + tagList = new ArrayList<>(); + tagList.add(tag); + } + mTagKeyMap.put(mActivityName, tagList); + } + return this; + } + + /** + * 根据tag恢复到某次调用时的参数 + * Recover immersion bar. + * + * @param tag the tag + * @return the immersion bar + */ + public ImmersionBar getTag(String tag) { + if (!isEmpty(tag)) { + BarParams barParams = mTagMap.get(mActivityName + "_TAG_" + tag); + if (barParams != null) { + mBarParams = barParams.clone(); + } + } return this; } @@ -942,9 +1155,11 @@ public ImmersionBar statusBarView(@IdRes int viewId) { * 通过上面配置后初始化后方可成功调用 */ public void init() { - mMap.put(mActivity.getClass().getName(), mBarParams); - initBar(); //初始化沉浸式 - setStatusBarView(); //通过状态栏高度动态设置状态栏布局 + mMap.put(mActivityName, mBarParams); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { + initBar(); //初始化沉浸式 + setStatusBarView(); //通过状态栏高度动态设置状态栏布局 + } transformView(); //变色view } @@ -952,12 +1167,24 @@ public void init() { * 当Activity关闭的时候,在onDestroy方法中调用 */ public void destroy() { - String key = mActivity.getClass().getName(); - if (key != null) { + if (mActivityName != null) { if (mBarParams != null) { mBarParams = null; } - mMap.remove(key); + ArrayList tagList = mTagKeyMap.get(mActivityName); + if (tagList != null && tagList.size() > 0) { + for (String tag : tagList) { + mTagMap.remove(tag); + } + mTagKeyMap.remove(mActivityName); + } + if (mFragmentList.size() > 0) { + for (String name : mFragmentList) { + if (name.contains(mActivityName)) + mMap.remove(name); + } + } + mMap.remove(mActivityName); } } @@ -969,7 +1196,8 @@ private void initBar() { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && !OSUtils.isEMUI3_1()) { uiFlags = initBarAboveLOLLIPOP(uiFlags); //初始化5.0以上,包含5.0 - fitsSystemWindows(); //android 5.0以上解决状态栏和布局重叠问题 + supportActionBar(); + //fitsSystemWindows(); //android 5.0以上解决状态栏和布局重叠问题 } else { initBarBelowLOLLIPOP(); //初始化5.0以下,4.4以上沉浸式 solveNavigation(); //解决android4.4有导航栏的情况下,activity底部被导航栏遮挡的问题和android 5.0以下解决状态栏和布局重叠问题 @@ -1002,8 +1230,12 @@ private int initBarAboveLOLLIPOP(int uiFlags) { uiFlags |= View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION; //Activity全屏显示,但导航栏不会被隐藏覆盖,导航栏依然可见,Activity底部布局部分会被导航栏遮住。 } mWindow.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS); //需要设置这个才能设置状态栏颜色 - mWindow.setStatusBarColor(ColorUtils.blendARGB(mBarParams.statusBarColor, - mBarParams.statusBarColorTransform, mBarParams.statusBarAlpha)); //设置状态栏颜色 + if (mBarParams.titleBarView == null) + mWindow.setStatusBarColor(ColorUtils.blendARGB(mBarParams.statusBarColor, + mBarParams.statusBarColorTransform, mBarParams.statusBarAlpha)); //设置状态栏颜色 + else + mWindow.setStatusBarColor(ColorUtils.blendARGB(mBarParams.statusBarColor, + Color.TRANSPARENT, mBarParams.statusBarAlpha)); //设置状态栏颜色 mWindow.setNavigationBarColor(ColorUtils.blendARGB(mBarParams.navigationBarColor, mBarParams.navigationBarColorTransform, mBarParams.navigationBarAlpha)); //设置导航栏颜色 return uiFlags; @@ -1036,21 +1268,40 @@ private void solveNavigation() { // 解决android4.4有导航栏的情况下,activity底部被导航栏遮挡的问题 if (mConfig.hasNavigtionBar() && !mBarParams.fullScreenTemp && !mBarParams.fullScreen) { if (mConfig.isNavigationAtBottom()) { //判断导航栏是否在底部 - if (mBarParams.fits) - mContentView.setPadding(0, getStatusBarHeight(mActivity), 0, mConfig.getNavigationBarHeight()); //有导航栏,获得rootView的根节点,然后设置距离底部的padding值为导航栏的高度值 - else - mContentView.setPadding(0, 0, 0, mConfig.getNavigationBarHeight()); + if (!mBarParams.isSupportActionBar) { //判断是否支持actionBar + if (mBarParams.fits) + mContentView.setPadding(0, mConfig.getStatusBarHeight(), + 0, mConfig.getNavigationBarHeight()); //有导航栏,获得rootView的根节点,然后设置距离底部的padding值为导航栏的高度值 + else + mContentView.setPadding(0, 0, 0, mConfig.getNavigationBarHeight()); + } else { + //支持有actionBar的界面 + mContentView.setPadding(0, mConfig.getStatusBarHeight() + + mConfig.getActionBarHeight() + 10, 0, mConfig.getNavigationBarHeight()); + } } else { + if (!mBarParams.isSupportActionBar) { + if (mBarParams.fits) + mContentView.setPadding(0, mConfig.getStatusBarHeight(), + mConfig.getNavigationBarWidth(), 0); //不在底部,设置距离右边的padding值为导航栏的宽度值 + else + mContentView.setPadding(0, 0, mConfig.getNavigationBarWidth(), 0); + } else { + //支持有actionBar的界面 + mContentView.setPadding(0, mConfig.getStatusBarHeight() + + mConfig.getActionBarHeight() + 10, mConfig.getNavigationBarWidth(), 0); + } + } + } else { + if (!mBarParams.isSupportActionBar) { if (mBarParams.fits) - mContentView.setPadding(0, getStatusBarHeight(mActivity), mConfig.getNavigationBarWidth(), 0); //不在底部,设置距离右边的padding值为导航栏的宽度值 + mContentView.setPadding(0, mConfig.getStatusBarHeight(), 0, 0); else - mContentView.setPadding(0, 0, mConfig.getNavigationBarWidth(), 0); + mContentView.setPadding(0, 0, 0, 0); + } else { + //支持有actionBar的界面 + mContentView.setPadding(0, mConfig.getStatusBarHeight() + mConfig.getActionBarHeight() + 10, 0, 0); } - } else { - if (mBarParams.fits) - mContentView.setPadding(0, getStatusBarHeight(mActivity), 0, 0); - else - mContentView.setPadding(0, 0, 0, 0); } } } @@ -1130,8 +1381,12 @@ private void MIUISetStatusBarLightMode() { */ private void transformView() { if (mBarParams.view != null) { - mBarParams.view.setBackgroundColor(ColorUtils.blendARGB(mBarParams.viewColorBeforeTransform, - mBarParams.viewColorAfterTransform, mBarParams.statusBarAlpha)); + if (Math.abs(mBarParams.viewAlpha - 0.0f) == 0) + mBarParams.view.setBackgroundColor(ColorUtils.blendARGB(mBarParams.viewColorBeforeTransform, + mBarParams.viewColorAfterTransform, mBarParams.statusBarAlpha)); + else + mBarParams.view.setBackgroundColor(ColorUtils.blendARGB(mBarParams.viewColorBeforeTransform, + mBarParams.viewColorAfterTransform, mBarParams.viewAlpha)); } if (mBarParams.viewMap.size() != 0) { Set>> entrySet = mBarParams.viewMap.entrySet(); @@ -1144,8 +1399,13 @@ private void transformView() { colorBefore = integerEntry.getKey(); colorAfter = integerEntry.getValue(); } - if (view != null) - view.setBackgroundColor(ColorUtils.blendARGB(colorBefore, colorAfter, mBarParams.statusBarAlpha)); + if (view != null) { + if (Math.abs(mBarParams.viewAlpha - 0.0f) == 0) + view.setBackgroundColor(ColorUtils.blendARGB(colorBefore, colorAfter, mBarParams.statusBarAlpha)); + else + view.setBackgroundColor(ColorUtils.blendARGB(colorBefore, colorAfter, mBarParams.viewAlpha)); + } + } } @@ -1161,19 +1421,55 @@ private void setStatusBarView() { params.height = mConfig.getStatusBarHeight(); mBarParams.statusBarViewByHeight.setLayoutParams(params); } + } + + /** + * 重新绘制标题栏高度,解决状态栏与顶部重叠问题 + * Sets title bar. + */ + private void setTitleBar() { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT && mBarParams.titleBarView != null) { + ViewGroup.LayoutParams layoutParams = mBarParams.titleBarView.getLayoutParams(); + if (mBarParams.titleBarHeight == 0) + mBarParams.titleBarHeight = layoutParams.height + mConfig.getStatusBarHeight(); + layoutParams.height = mBarParams.titleBarHeight; + mBarParams.titleBarView.setPadding(0, mConfig.getStatusBarHeight(), 0, 0); + mBarParams.titleBarView.setLayoutParams(layoutParams); + } + } + + /** + * 有actionBar的界面,通过状态栏高度和actionBAr的高度动态设置view的高度,使得布局在actionBar下面 + */ + private void setStatusBarViewWithActionBar() { + if (mBarParams.statusBarViewByHeight != null) { + ViewGroup.LayoutParams params = mBarParams.statusBarViewByHeight.getLayoutParams(); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { + if (Build.VERSION.SDK_INT == Build.VERSION_CODES.KITKAT || OSUtils.isEMUI3_1()) + params.height = mConfig.getStatusBarHeight() + mConfig.getActionBarHeight() + 10; + else + params.height = mConfig.getStatusBarHeight() + mConfig.getActionBarHeight(); + } else { + params.height = mConfig.getActionBarHeight() + 10; + } + mBarParams.statusBarViewByHeight.setLayoutParams(params); + } mBarParams.statusBarViewByHeight = null; } /** - * 解决状态栏和布局重叠问题 - * Fits system windows. + * 支持actionBar的界面 + * Support action bar. */ - private void fitsSystemWindows() { + private void supportActionBar() { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && !OSUtils.isEMUI3_1()) { - if (mBarParams.fits) { - mContentView.setPadding(0, getStatusBarHeight(mActivity), 0, 0); + if (mBarParams.isSupportActionBar) { + mContentView.setPadding(0, mConfig.getStatusBarHeight() + mConfig.getActionBarHeight(), 0, 0); } else { - mContentView.setPadding(0, 0, 0, 0); + if (mBarParams.fits) + mContentView.setPadding(0, mConfig.getStatusBarHeight(), 0, 0); + else + mContentView.setPadding(0, 0, 0, 0); } } } @@ -1185,14 +1481,18 @@ private void setupStatusBarView() { if (mBarParams.statusBarView == null) { mBarParams.statusBarView = new View(mActivity); } - FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, getStatusBarHeight(mActivity)); + FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, mConfig.getStatusBarHeight()); params.gravity = Gravity.TOP; - if (!isNavigationAtBottom(mActivity)) { - params.rightMargin = getNavigationBarWidth(mActivity); + if (!mConfig.isNavigationAtBottom()) { + params.rightMargin = mConfig.getNavigationBarWidth(); } mBarParams.statusBarView.setLayoutParams(params); - mBarParams.statusBarView.setBackgroundColor(ColorUtils.blendARGB(mBarParams.statusBarColor, - mBarParams.statusBarColorTransform, mBarParams.statusBarAlpha)); + if (mBarParams.titleBarView == null || mBarParams.statusBarFlag) + mBarParams.statusBarView.setBackgroundColor(ColorUtils.blendARGB(mBarParams.statusBarColor, + mBarParams.statusBarColorTransform, mBarParams.statusBarAlpha)); + else + mBarParams.statusBarView.setBackgroundColor(ColorUtils.blendARGB(mBarParams.statusBarColor, + Color.TRANSPARENT, mBarParams.statusBarAlpha)); mBarParams.statusBarView.setVisibility(View.VISIBLE); ViewGroup viewGroup = (ViewGroup) mBarParams.statusBarView.getParent(); if (viewGroup != null) @@ -1208,11 +1508,11 @@ private void setupNavBarView() { mBarParams.navigationBarView = new View(mActivity); } FrameLayout.LayoutParams params; - if (isNavigationAtBottom(mActivity)) { - params = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, getNavigationBarHeight(mActivity)); + if (mConfig.isNavigationAtBottom()) { + params = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, mConfig.getNavigationBarHeight()); params.gravity = Gravity.BOTTOM; } else { - params = new FrameLayout.LayoutParams(getNavigationBarWidth(mActivity), FrameLayout.LayoutParams.MATCH_PARENT); + params = new FrameLayout.LayoutParams(mConfig.getNavigationBarWidth(), FrameLayout.LayoutParams.MATCH_PARENT); params.gravity = Gravity.END; } mBarParams.navigationBarView.setLayoutParams(params); @@ -1330,4 +1630,8 @@ public static boolean isSupportStatusBarDarkFont() { public BarParams getBarParams() { return mBarParams; } + + private static boolean isEmpty(String str) { + return str == null || str.trim().length() == 0; + } } diff --git a/barlibrary/src/main/java/com/gyf/barlibrary/OSUtils.java b/barlibrary/src/main/java/com/gyf/barlibrary/OSUtils.java index f6b2fe4..c2d3b7c 100644 --- a/barlibrary/src/main/java/com/gyf/barlibrary/OSUtils.java +++ b/barlibrary/src/main/java/com/gyf/barlibrary/OSUtils.java @@ -17,6 +17,7 @@ public class OSUtils { /** * 判断是否为miui * Is miui boolean. + * * @return the boolean */ public static boolean isMIUI() { @@ -101,8 +102,16 @@ public static boolean isFlymeOS() { */ public static boolean isFlymeOS4More() { String version = getFlymeOSVersion(); - if (!version.isEmpty() && (Integer.valueOf(version.substring(6, 7)) >= 4)) { - return true; + int num; + if (!version.isEmpty()) { + if (version.toLowerCase().contains("os")) { + num = Integer.valueOf(version.substring(9, 10)); + } else { + num = Integer.valueOf(version.substring(6, 7)); + } + if (num >= 4) { + return true; + } } return false; } diff --git a/build.gradle b/build.gradle index c22cf8b..d523e9e 100644 --- a/build.gradle +++ b/build.gradle @@ -5,7 +5,7 @@ buildscript { jcenter() } dependencies { - classpath 'com.android.tools.build:gradle:2.2.1' + classpath 'com.android.tools.build:gradle:2.3.2' classpath 'com.novoda:bintray-release:0.3.4' // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files @@ -15,6 +15,7 @@ buildscript { allprojects { repositories { jcenter() + maven { url "https://jitpack.io" } } tasks.withType(Javadoc) { options{ encoding "UTF-8" diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index e6ca335..9ec1b2b 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,4 +1,4 @@ -#Tue Apr 18 15:19:48 CST 2017 +#Thu Jun 01 14:21:23 CST 2017 distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists zipStoreBase=GRADLE_USER_HOME diff --git a/jar/barlibrary-2.2.2.jar b/jar/barlibrary-2.2.2.jar new file mode 100644 index 0000000..db4a5eb Binary files /dev/null and b/jar/barlibrary-2.2.2.jar differ diff --git a/sample/build.gradle b/sample/build.gradle index 38155e2..cacff65 100644 --- a/sample/build.gradle +++ b/sample/build.gradle @@ -39,4 +39,9 @@ dependencies { compile 'me.imid.swipebacklayout.lib:library:1.0.0' compile 'com.apkfuns.logutils:library:1.4.2.2' compile 'com.android.support:design:25.3.1' + compile 'com.youth.banner:banner:1.4.9' + compile 'com.github.CymChad:BaseRecyclerViewAdapterHelper:2.9.18' + compile 'com.github.bumptech.glide:glide:3.7.0' + compile 'com.blankj:utilcode:1.6.4' + compile 'com.lcodecorex:tkrefreshlayout:1.0.7' } diff --git a/sample/src/main/AndroidManifest.xml b/sample/src/main/AndroidManifest.xml index d57b80e..72e4b15 100644 --- a/sample/src/main/AndroidManifest.xml +++ b/sample/src/main/AndroidManifest.xml @@ -23,15 +23,21 @@ - + + + + \ No newline at end of file diff --git a/sample/src/main/java/com/gyf/immersionbar/activity/ActionBarActivity.java b/sample/src/main/java/com/gyf/immersionbar/activity/ActionBarActivity.java new file mode 100644 index 0000000..746c293 --- /dev/null +++ b/sample/src/main/java/com/gyf/immersionbar/activity/ActionBarActivity.java @@ -0,0 +1,51 @@ +package com.gyf.immersionbar.activity; + +import android.os.Bundle; +import android.support.annotation.Nullable; +import android.support.v7.app.ActionBar; +import android.view.View; +import android.widget.Button; +import android.widget.TextView; + +import com.gyf.barlibrary.ImmersionBar; +import com.gyf.immersionbar.R; + +import butterknife.BindView; +import butterknife.ButterKnife; + +/** + * Created by geyifeng on 2017/6/2. + */ + +public class ActionBarActivity extends BaseActivity { + + @BindView(R.id.btn) + Button btn; + @BindView(R.id.text) + TextView text; + + @Override + protected void onCreate(@Nullable Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_action_bar); + ButterKnife.bind(this); + ActionBar actionBar = getSupportActionBar(); + if (actionBar != null) { + actionBar.setTitle("结合actionBar使用"); + } + text.setText("上面图片被actionBar遮挡住了,我想使布局从actionBar下面开始绘制,怎么办?"); + btn.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + ImmersionBar.with(ActionBarActivity.this) + .statusBarColor(R.color.colorPrimary) + .supportActionBar(true) + .init(); + text.setText("哈哈哈!解决啦!就问你惊不惊喜,意不意外,刺不刺激!!!" + + "重点是这个方法supportActionBar(true),实现原理,当为true时,布局距离顶部的" + + "padding值为状态栏的高度+ActionBar的高度"); + btn.setText("解决啦"); + } + }); + } +} diff --git a/sample/src/main/java/com/gyf/immersionbar/activity/BackActivity.java b/sample/src/main/java/com/gyf/immersionbar/activity/BackActivity.java index 21b3f3a..87e6f59 100644 --- a/sample/src/main/java/com/gyf/immersionbar/activity/BackActivity.java +++ b/sample/src/main/java/com/gyf/immersionbar/activity/BackActivity.java @@ -21,6 +21,7 @@ protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_swipe_back); ImmersionBar.with(this) + .titleBar(R.id.toolbar) .navigationBarColor(R.color.colorPrimary) .init(); } diff --git a/sample/src/main/java/com/gyf/immersionbar/activity/ColorActivity.java b/sample/src/main/java/com/gyf/immersionbar/activity/ColorActivity.java index e250300..efcdaef 100644 --- a/sample/src/main/java/com/gyf/immersionbar/activity/ColorActivity.java +++ b/sample/src/main/java/com/gyf/immersionbar/activity/ColorActivity.java @@ -46,16 +46,12 @@ protected void onCreate(@Nullable Bundle savedInstanceState) { .statusBarView(R.id.top_view) .statusBarColor(R.color.colorPrimary) .navigationBarColor(R.color.btn8) - .fullScreen(true) .init(); seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { @Override public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { float alpha = (float) progress / 100; textView.setText("透明度:" + alpha + "f"); - linearLayout.setBackgroundColor(ColorUtils.blendARGB( - ContextCompat.getColor(ColorActivity.this, R.color.darker_gray), - ContextCompat.getColor(ColorActivity.this, R.color.btn5), alpha)); ImmersionBar.with(ColorActivity.this) .barAlpha(alpha) .statusBarColorTransform(R.color.btn14) @@ -64,6 +60,7 @@ public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { .addViewSupportTransformColor(btn1, R.color.btn1, R.color.btn4) .addViewSupportTransformColor(btn2, R.color.btn3, R.color.btn12) .addViewSupportTransformColor(btn3, R.color.btn5, R.color.btn10) + .addViewSupportTransformColor(linearLayout, R.color.darker_gray, R.color.btn5) .init(); } diff --git a/sample/src/main/java/com/gyf/immersionbar/activity/CoordinatorActivity.java b/sample/src/main/java/com/gyf/immersionbar/activity/CoordinatorActivity.java index baf4e1d..0a97e1a 100644 --- a/sample/src/main/java/com/gyf/immersionbar/activity/CoordinatorActivity.java +++ b/sample/src/main/java/com/gyf/immersionbar/activity/CoordinatorActivity.java @@ -2,14 +2,17 @@ import android.os.Bundle; import android.support.annotation.Nullable; +import android.support.design.widget.CollapsingToolbarLayout; import android.support.design.widget.FloatingActionButton; import android.support.design.widget.Snackbar; import android.support.v7.app.ActionBar; import android.support.v7.widget.Toolbar; import android.view.View; import android.view.WindowManager; +import android.widget.LinearLayout; import android.widget.TextView; +import com.gyf.barlibrary.ImmersionBar; import com.gyf.immersionbar.R; import butterknife.BindView; @@ -32,6 +35,7 @@ protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_coordinator); ButterKnife.bind(this); + ImmersionBar.with(this).titleBar(toolbar).init(); setSupportActionBar(toolbar); ActionBar actionBar = getSupportActionBar(); if (actionBar != null) { diff --git a/sample/src/main/java/com/gyf/immersionbar/activity/FragmentActivity.java b/sample/src/main/java/com/gyf/immersionbar/activity/FragmentActivity.java index 4a9a2bb..64a3eab 100644 --- a/sample/src/main/java/com/gyf/immersionbar/activity/FragmentActivity.java +++ b/sample/src/main/java/com/gyf/immersionbar/activity/FragmentActivity.java @@ -13,6 +13,7 @@ import com.gyf.immersionbar.fragment.OneFragment; import com.gyf.immersionbar.fragment.ThreeFragment; import com.gyf.immersionbar.fragment.TwoFragment; +import com.gyf.immersionbar.view.CustomViewPager; import com.lzy.widget.AlphaIndicator; import java.util.ArrayList; @@ -25,65 +26,69 @@ public class FragmentActivity extends BaseActivity { private ArrayList mFragments; + private OneFragment oneFragment; + private TwoFragment twoFragment; + private ThreeFragment threeFragment; + private FourFragment fourFragment; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_fragment); - ImmersionBar.with(this).statusBarDarkFont(false) - .navigationBarColor(R.color.btn4) - .init(); initData(); - ViewPager viewPager = (ViewPager) findViewById(R.id.viewPager); + CustomViewPager viewPager = (CustomViewPager) findViewById(R.id.viewPager); viewPager.setAdapter(new MyAdapter(getSupportFragmentManager())); AlphaIndicator alphaIndicator = (AlphaIndicator) findViewById(R.id.alphaIndicator); alphaIndicator.setViewPager(viewPager); viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() { @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { - - } - - @Override - public void onPageSelected(int position) { -// ImmersionBar immersionBar = ImmersionBar.with(FragmentActivity.this); // switch (position) { // case 0: -// immersionBar.statusBarDarkFont(false) +// ImmersionBar.with(oneFragment) // .navigationBarColor(R.color.btn4) // .init(); // break; // case 1: -// immersionBar.statusBarDarkFont(true) +// ImmersionBar.with(twoFragment) +// .statusBarDarkFont(true) // .navigationBarColor(R.color.btn3) // .init(); // break; // case 2: -// immersionBar.statusBarDarkFont(false) +// ImmersionBar.with(threeFragment) // .navigationBarColor(R.color.btn13) // .init(); // break; // case 3: -// immersionBar.statusBarDarkFont(true) +// ImmersionBar.with(fourFragment) +// .statusBarDarkFont(true) // .navigationBarColor(R.color.btn1) // .init(); // break; // } } + @Override + public void onPageSelected(int position) { + + } + @Override public void onPageScrollStateChanged(int state) { } }); + viewPager.setOffscreenPageLimit(3); + viewPager.setScroll(false); } private void initData() { mFragments = new ArrayList<>(); - OneFragment oneFragment = new OneFragment(); - TwoFragment twoFragment = new TwoFragment(); - ThreeFragment threeFragment = new ThreeFragment(); - FourFragment fourFragment = new FourFragment(); + oneFragment = new OneFragment(); + twoFragment = new TwoFragment(); + threeFragment = new ThreeFragment(); + fourFragment = new FourFragment(); mFragments.add(oneFragment); mFragments.add(twoFragment); mFragments.add(threeFragment); diff --git a/sample/src/main/java/com/gyf/immersionbar/activity/MainActivity.java b/sample/src/main/java/com/gyf/immersionbar/activity/MainActivity.java index 76d364e..8cd1d8d 100644 --- a/sample/src/main/java/com/gyf/immersionbar/activity/MainActivity.java +++ b/sample/src/main/java/com/gyf/immersionbar/activity/MainActivity.java @@ -44,6 +44,7 @@ protected void onCreate(Bundle savedInstanceState) { // .hideBar(BarHide.FLAG_HIDE_BAR) //隐藏状态栏或导航栏或两者,不写默认不隐藏 // .setViewSupportTransformColor(toolbar) //设置支持view变色,支持一个view,不指定颜色,默认和状态栏同色,还有两个重载方法 // .addViewSupportTransformColor(toolbar) //设置支持view变色,可以添加多个view,不指定颜色,默认和状态栏同色,还有两个重载方法 +// .titleBar(view) //解决状态栏和布局重叠问题 // .statusBarView(view) //解决状态栏和布局重叠问题 // .fitsSystemWindows(false) //解决状态栏和布局重叠问题,默认为false,当为true时一定要指定statusBarColor(),不然状态栏为透明色 // .statusBarColorTransform(R.color.orange) //状态栏变色后的颜色 @@ -57,9 +58,9 @@ protected void onCreate(Bundle savedInstanceState) { @OnClick({R.id.btn_pic_color, R.id.btn_pic, R.id.btn_color, R.id.btn_swipe_back, R.id.btn_fragment, - R.id.btn_drawer, R.id.btn_coordinator, R.id.btn_flyme, R.id.btn_over, R.id.btn_key_board, R.id.btn_status_hide, - R.id.btn_navigation_hide, R.id.btn_bar_hide, R.id.btn_bar_show, R.id.btn_full, R.id.btn_bar_font_dark, - R.id.btn_bar_font_light}) + R.id.btn_drawer, R.id.btn_coordinator, R.id.btn_action_bar, R.id.btn_flyme, R.id.btn_over, + R.id.btn_key_board, R.id.btn_white_status_bar, R.id.btn_status_hide, R.id.btn_navigation_hide, R.id.btn_bar_hide, + R.id.btn_bar_show, R.id.btn_full, R.id.btn_bar_font_dark, R.id.btn_bar_font_light}) public void onClick(View view) { switch (view.getId()) { case R.id.btn_pic_color: @@ -86,12 +87,18 @@ public void onClick(View view) { case R.id.btn_coordinator: startActivity(new Intent(this, CoordinatorActivity.class)); break; + case R.id.btn_action_bar: + startActivity(new Intent(this, ActionBarActivity.class)); + break; case R.id.btn_over: startActivity(new Intent(this, OverActivity.class)); break; case R.id.btn_key_board: startActivity(new Intent(this, KeyBoardActivity.class)); break; + case R.id.btn_white_status_bar: + startActivity(new Intent(this, WhiteStatusBarActivity.class)); + break; case R.id.btn_status_hide: ImmersionBar.with(this).hideBar(BarHide.FLAG_HIDE_STATUS_BAR).init(); break; diff --git a/sample/src/main/java/com/gyf/immersionbar/activity/Over5Activity.java b/sample/src/main/java/com/gyf/immersionbar/activity/Over5Activity.java new file mode 100644 index 0000000..5c9deec --- /dev/null +++ b/sample/src/main/java/com/gyf/immersionbar/activity/Over5Activity.java @@ -0,0 +1,41 @@ +package com.gyf.immersionbar.activity; + +import android.os.Bundle; +import android.support.annotation.Nullable; +import android.support.v7.widget.Toolbar; +import android.view.View; +import android.view.ViewGroup; +import android.widget.TextView; + +import com.gyf.barlibrary.ImmersionBar; +import com.gyf.immersionbar.R; + +import butterknife.BindView; +import butterknife.ButterKnife; + +/** + * Created by geyifeng on 2017/5/8. + */ + +public class Over5Activity extends BaseActivity { + + @BindView(R.id.text) + TextView textView; + @BindView(R.id.toolbar) + Toolbar toolbar; + + @Override + protected void onCreate(@Nullable Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_over5); + ButterKnife.bind(this); + ImmersionBar.with(this) + .titleBar(toolbar) + .navigationBarColor(R.color.colorPrimary) + .init(); + textView.setText("不需要在xml文件增加view给状态栏预留空间,重点是这个方法titleBar(toolbar),实现原理:" + + "根据状态栏高度动态设置标题栏(demo是ToolBar,也可以是其他的)高度," + + "设置标题栏距离顶部padding值的为状态栏的高度,记住xml标题栏的高度不能指定为wrap_content," + + "不然绘制的高度只有状态栏的高度"); + } +} diff --git a/sample/src/main/java/com/gyf/immersionbar/activity/OverActivity.java b/sample/src/main/java/com/gyf/immersionbar/activity/OverActivity.java index f69ffad..4c53993 100644 --- a/sample/src/main/java/com/gyf/immersionbar/activity/OverActivity.java +++ b/sample/src/main/java/com/gyf/immersionbar/activity/OverActivity.java @@ -31,24 +31,27 @@ protected void onCreate(@Nullable Bundle savedInstanceState) { String text = "请看这个页面的Toolbar和状态栏重叠啦,怎么解决呢?" + "不急不急,先说说沉浸式的原理吧!" + "原理:其实沉浸式就是把整个布局拉伸到全屏显示,这样自然而然就会使得布局的最顶端和状态栏重合了," + - "好吧,以下给出四种解决方案,大家根据项目需求自己看着使用哦。"; + "好吧,以下给出五种解决方案,大家根据项目需求自己看着使用哦,不可结合使用。"; textView.setText(text); } - @OnClick({R.id.btn_one, R.id.btn_two, R.id.btn_three, R.id.btn_four}) + @OnClick({R.id.btn_one, R.id.btn_two, R.id.btn_three, R.id.btn_four, R.id.btn_five}) public void onClick(View view) { switch (view.getId()) { case R.id.btn_one: - startActivity(new Intent(this,Over1Activity.class)); + startActivity(new Intent(this, Over1Activity.class)); break; case R.id.btn_two: - startActivity(new Intent(this,Over2Activity.class)); + startActivity(new Intent(this, Over2Activity.class)); break; case R.id.btn_three: - startActivity(new Intent(this,Over3Activity.class)); + startActivity(new Intent(this, Over3Activity.class)); break; case R.id.btn_four: - startActivity(new Intent(this,Over4Activity.class)); + startActivity(new Intent(this, Over4Activity.class)); + break; + case R.id.btn_five: + startActivity(new Intent(this, Over5Activity.class)); break; } } diff --git a/sample/src/main/java/com/gyf/immersionbar/activity/PicActivity.java b/sample/src/main/java/com/gyf/immersionbar/activity/PicActivity.java index 521d734..bdf2726 100644 --- a/sample/src/main/java/com/gyf/immersionbar/activity/PicActivity.java +++ b/sample/src/main/java/com/gyf/immersionbar/activity/PicActivity.java @@ -2,6 +2,7 @@ import android.os.Bundle; import android.support.annotation.Nullable; +import android.support.v7.widget.Toolbar; import android.widget.SeekBar; import android.widget.TextView; @@ -20,23 +21,27 @@ public class PicActivity extends SwipeBackActivity { TextView textView; @BindView(R.id.seek_bar) SeekBar seekBar; + @BindView(R.id.toolbar) + Toolbar toolbar; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_pic); ButterKnife.bind(this); - ImmersionBar.with(this).transparentBar().init(); - + ImmersionBar.with(this) + .titleBar(toolbar) + .transparentBar() + .init(); seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { @Override public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { float alpha = (float) progress / 100; textView.setText("透明度:" + alpha + "f"); ImmersionBar.with(PicActivity.this) - .barAlpha(alpha) - .statusBarColorTransform(R.color.colorPrimary) + .addViewSupportTransformColor(toolbar,R.color.colorPrimary) .navigationBarColorTransform(R.color.orange) + .barAlpha(alpha) .init(); } diff --git a/sample/src/main/java/com/gyf/immersionbar/activity/PicAndColorActivity.java b/sample/src/main/java/com/gyf/immersionbar/activity/PicAndColorActivity.java index 21d32dc..e5851b8 100644 --- a/sample/src/main/java/com/gyf/immersionbar/activity/PicAndColorActivity.java +++ b/sample/src/main/java/com/gyf/immersionbar/activity/PicAndColorActivity.java @@ -28,12 +28,13 @@ public class PicAndColorActivity extends BaseActivity implements SeekBar.OnSeekB protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_pic_color); + ButterKnife.bind(this); ImmersionBar.with(this) .statusBarView(R.id.top_view) .navigationBarColor(R.color.colorPrimary) .fullScreen(true) + .addTag("PicAndColor") //给上面参数打标记,以后可以通过标记恢复 .init(); - ButterKnife.bind(this); seekBar.setOnSeekBarChangeListener(this); } @@ -50,10 +51,7 @@ public void onClick(View v) { Toast.makeText(this, "当前设备没有导航栏", Toast.LENGTH_SHORT).show(); break; case R.id.btn_color: - ImmersionBar.with(this) - .transparentStatusBar() - .navigationBarColor(R.color.colorPrimary) - .init(); + ImmersionBar.with(this).getTag("PicAndColor").init(); //根据tag标记来恢复 break; } } @@ -62,7 +60,7 @@ public void onClick(View v) { public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { float alpha = (float) progress / 100; ImmersionBar.with(this) - .barColorTransform(R.color.orange) + .statusBarColorTransform(R.color.orange) .navigationBarColorTransform(R.color.tans) .addViewSupportTransformColor(toolbar) .barAlpha(alpha) diff --git a/sample/src/main/java/com/gyf/immersionbar/activity/WhiteStatusBarActivity.java b/sample/src/main/java/com/gyf/immersionbar/activity/WhiteStatusBarActivity.java new file mode 100644 index 0000000..33111a4 --- /dev/null +++ b/sample/src/main/java/com/gyf/immersionbar/activity/WhiteStatusBarActivity.java @@ -0,0 +1,49 @@ +package com.gyf.immersionbar.activity; + +import android.os.Bundle; +import android.support.annotation.Nullable; +import android.view.View; +import android.widget.Button; +import android.widget.TextView; + +import com.gyf.barlibrary.ImmersionBar; +import com.gyf.immersionbar.R; + +import butterknife.BindView; +import butterknife.ButterKnife; + +/** + * Created by geyifeng on 2017/6/2. + */ + +public class WhiteStatusBarActivity extends BaseActivity { + + @BindView(R.id.btn) + Button btn; + @BindView(R.id.text) + TextView text; + @BindView(R.id.top_view) + View view; + + @Override + protected void onCreate(@Nullable Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_white_status_bar); + ButterKnife.bind(this); + ImmersionBar.with(this) + .statusBarView(view) + .init(); + btn.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + ImmersionBar.with(WhiteStatusBarActivity.this) + .statusBarDarkFont(true, 0.2f) + .init(); + text.setText("A:重点在于statusBarDarkFont(true,0.2f)这个方法," + + "原理:如果当前设备支持状态栏字体变色,会设置状态栏字体为黑色," + + "如果当前设备不支持状态栏字体变色,会使当前状态栏加上透明度,否则不执行透明度。"); + btn.setVisibility(View.GONE); + } + }); + } +} diff --git a/sample/src/main/java/com/gyf/immersionbar/adapter/OneAdapter.java b/sample/src/main/java/com/gyf/immersionbar/adapter/OneAdapter.java new file mode 100644 index 0000000..4849e77 --- /dev/null +++ b/sample/src/main/java/com/gyf/immersionbar/adapter/OneAdapter.java @@ -0,0 +1,20 @@ +package com.gyf.immersionbar.adapter; + +import com.chad.library.adapter.base.BaseQuickAdapter; +import com.chad.library.adapter.base.BaseViewHolder; +import com.gyf.immersionbar.R; + +/** + * Created by geyifeng on 2017/6/3. + */ + +public class OneAdapter extends BaseQuickAdapter { + public OneAdapter() { + super(R.layout.item_one); + } + + @Override + protected void convert(BaseViewHolder helper, String item) { + helper.setText(R.id.text, item); + } +} diff --git a/sample/src/main/java/com/gyf/immersionbar/fragment/BaseFragment.java b/sample/src/main/java/com/gyf/immersionbar/fragment/BaseFragment.java new file mode 100644 index 0000000..40adc2d --- /dev/null +++ b/sample/src/main/java/com/gyf/immersionbar/fragment/BaseFragment.java @@ -0,0 +1,154 @@ +package com.gyf.immersionbar.fragment; + +import android.content.Context; +import android.os.Bundle; +import android.support.annotation.IdRes; +import android.support.annotation.Nullable; +import android.support.v4.app.Fragment; +import android.support.v4.app.FragmentActivity; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.AdapterView; + +import com.gyf.barlibrary.ImmersionBar; +import com.gyf.barlibrary.ImmersionFragment; + +import butterknife.ButterKnife; + +/** + * Fragment的基类 + * Created by geyifeng on 2017/4/7. + */ +public abstract class BaseFragment extends ImmersionFragment { + /** + * The M activity. + */ + protected FragmentActivity mActivity; + + /** + * 是否对用户可见 + */ + protected boolean mIsVisible; + /** + * 是否加载完成 + * 当执行完oncreatview,View的初始化方法后方法后即为true + */ + protected boolean mIsPrepare; + + /** + * The M root view. + */ + protected View mRootView; + + @Override + public void onAttach(Context context) { + super.onAttach(context); + mActivity = getActivity(); + } + + @Override + public void setUserVisibleHint(boolean isVisibleToUser) { + super.setUserVisibleHint(isVisibleToUser); + + if (getUserVisibleHint()) { + mIsVisible = true; + onVisible(); + } else { + mIsVisible = false; + onInvisible(); + } + } + + @Override + protected void immersionInit() { + ImmersionBar.with(this).init(); + } + + @Nullable + @Override + public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { + mRootView = inflater.inflate(setLayoutId(), container, false); + ButterKnife.bind(this, mRootView); + initView(); + if (isLazyLoad()) { + mIsPrepare = true; + onLazyLoad(); + } else { + initData(); + } + setListener(); + return mRootView; + } + + + /** + * 是否懒加载 + * + * @return the boolean + */ + protected boolean isLazyLoad() { + return true; + } + + /** + * 用户可见时执行的操作 + */ + protected void onVisible() { + onLazyLoad(); + } + + private void onLazyLoad() { + + if (mIsVisible & mIsPrepare) { + mIsPrepare = false; + initData(); + } + + } + + /** + * 找到activity的控件 + * + * @param the type parameter + * @param id the id + * @return the t + */ + @SuppressWarnings("unchecked") + protected T findActivityViewById(@IdRes int id) { + + return (T) mActivity.findViewById(id); + } + + /** + * Sets layout id. + * + * @return the layout id + */ + protected abstract int setLayoutId(); + + protected void initView() { + } + + /** + * 初始化数据 + */ + protected abstract void initData(); + + /** + * 设置监听 + */ + protected void setListener() { + } + + /** + * 用户不可见执行 + */ + protected void onInvisible() { + } + + @Override + public void onDestroyView() { + super.onDestroyView(); + } +} diff --git a/sample/src/main/java/com/gyf/immersionbar/fragment/FourFragment.java b/sample/src/main/java/com/gyf/immersionbar/fragment/FourFragment.java index a579c8c..b737cf4 100644 --- a/sample/src/main/java/com/gyf/immersionbar/fragment/FourFragment.java +++ b/sample/src/main/java/com/gyf/immersionbar/fragment/FourFragment.java @@ -2,6 +2,7 @@ import android.os.Bundle; import android.support.annotation.Nullable; +import android.support.v7.widget.Toolbar; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; @@ -10,21 +11,31 @@ import com.gyf.barlibrary.ImmersionFragment; import com.gyf.immersionbar.R; +import butterknife.BindView; + /** * Created by geyifeng on 2017/5/12. */ -public class FourFragment extends ImmersionFragment { +public class FourFragment extends BaseFragment { + + @BindView(R.id.toolbar) + Toolbar toolbar; + + @Override + protected int setLayoutId() { + return R.layout.fragment_four; + } - @Nullable @Override - public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { - return inflater.inflate(R.layout.fragment_four, container, false); + protected void initData() { + } @Override protected void immersionInit() { - ImmersionBar.with(getActivity()) + ImmersionBar.with(this) + .titleBar(toolbar) .statusBarDarkFont(true) .navigationBarColor(R.color.btn1) .init(); diff --git a/sample/src/main/java/com/gyf/immersionbar/fragment/OneFragment.java b/sample/src/main/java/com/gyf/immersionbar/fragment/OneFragment.java index 3c061b9..2166353 100644 --- a/sample/src/main/java/com/gyf/immersionbar/fragment/OneFragment.java +++ b/sample/src/main/java/com/gyf/immersionbar/fragment/OneFragment.java @@ -1,32 +1,183 @@ package com.gyf.immersionbar.fragment; -import android.os.Bundle; -import android.support.annotation.Nullable; +import android.os.Handler; +import android.support.v7.widget.LinearLayoutManager; +import android.support.v7.widget.RecyclerView; +import android.support.v7.widget.Toolbar; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; +import com.chad.library.adapter.base.BaseQuickAdapter; import com.gyf.barlibrary.ImmersionBar; -import com.gyf.barlibrary.ImmersionFragment; import com.gyf.immersionbar.R; +import com.gyf.immersionbar.adapter.OneAdapter; +import com.gyf.immersionbar.utils.GlideImageLoader; +import com.lcodecore.tkrefreshlayout.RefreshListenerAdapter; +import com.lcodecore.tkrefreshlayout.TwinklingRefreshLayout; +import com.youth.banner.Banner; + +import java.util.ArrayList; +import java.util.List; + +import butterknife.BindView; /** * Created by geyifeng on 2017/5/12. */ -public class OneFragment extends ImmersionFragment { +public class OneFragment extends BaseFragment { + + @BindView(R.id.toolbar) + Toolbar mToolbar; + @BindView(R.id.rv) + RecyclerView mRv; + @BindView(R.id.refreshLayout) + TwinklingRefreshLayout refreshLayout; + private OneAdapter mOneAdapter; + private View headView; + private List mItemList; + private List mImages; + private int bannerHeight = 300; + private Banner banner; + + @Override + protected int setLayoutId() { + return R.layout.fragment_one; + } - @Nullable @Override - public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { - return inflater.inflate(R.layout.fragment_one, container, false); + protected void initView() { + refreshLayout.setEnableLoadmore(false); + final LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity(), + LinearLayoutManager.VERTICAL, false); + mRv.setLayoutManager(linearLayoutManager); + mOneAdapter = new OneAdapter(); + mOneAdapter.openLoadAnimation(BaseQuickAdapter.SCALEIN); + mRv.setAdapter(mOneAdapter); + } + + @Override + protected void initData() { + mItemList = new ArrayList<>(); + for (int i = 1; i <= 20; i++) { + mItemList.add("item" + i); + } + mImages = new ArrayList<>(); + mImages.add("http://desk.zol.com.cn/showpic/1024x768_63850_14.html"); + mImages.add("http://desk.zol.com.cn/showpic/1024x768_63850_14.html"); + mImages.add("http://desk.zol.com.cn/showpic/1024x768_63850_14.html"); + mImages.add("http://desk.zol.com.cn/showpic/1024x768_63850_14.html"); + mOneAdapter.setNewData(mItemList); + addHeaderView(); + } + + private void addHeaderView() { + headView = LayoutInflater.from(mActivity).inflate(R.layout.item_banner, (ViewGroup) mRv.getParent(), false); + banner = (Banner) headView.findViewById(R.id.banner); + banner.setImages(mImages) + .setImageLoader(new GlideImageLoader()) + .setDelayTime(5000) + .start(); + mOneAdapter.addHeaderView(headView); + mOneAdapter.setPreLoadNumber(1); + } + + @Override + protected void setListener() { + ViewGroup.LayoutParams bannerParams = banner.getLayoutParams(); + ViewGroup.LayoutParams titleBarParams = mToolbar.getLayoutParams(); + bannerHeight = bannerParams.height - titleBarParams.height - ImmersionBar.getStatusBarHeight(getActivity()); + mRv.addOnScrollListener(new RecyclerView.OnScrollListener() { + private int totalDy = 0; + + @Override + public void onScrolled(RecyclerView recyclerView, int dx, int dy) { + super.onScrolled(recyclerView, dx, dy); + totalDy += dy; + ImmersionBar immersionBar = ImmersionBar.with(OneFragment.this) + .addViewSupportTransformColor(mToolbar, R.color.colorPrimary); + if (totalDy <= bannerHeight) { + float alpha = (float) totalDy / bannerHeight; + immersionBar.statusBarAlpha(alpha) + .init(); + } else { + immersionBar.statusBarAlpha(1.0f) + .init(); + } + } + }); + mOneAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() { + @Override + public void onLoadMoreRequested() { + new Handler().postDelayed(new Runnable() { + @Override + public void run() { + mOneAdapter.addData(addData()); + if (mItemList.size() == 100) { + mOneAdapter.loadMoreEnd(); + } else + mOneAdapter.loadMoreComplete(); + } + }, 2000); + } + }, mRv); + refreshLayout.setOnRefreshListener(new RefreshListenerAdapter() { + @Override + public void onRefresh(final TwinklingRefreshLayout refreshLayout) { + new Handler().postDelayed(new Runnable() { + @Override + public void run() { + mItemList.clear(); + mItemList.addAll(newData()); + mOneAdapter.setNewData(mItemList); + refreshLayout.finishRefreshing(); + mToolbar.setVisibility(View.VISIBLE); + ImmersionBar.with(OneFragment.this).statusBarDarkFont(false).init(); + } + }, 2000); + } + + @Override + public void onPullingDown(TwinklingRefreshLayout refreshLayout, float fraction) { + mToolbar.setVisibility(View.GONE); + ImmersionBar.with(OneFragment.this).statusBarDarkFont(true).init(); + } + + @Override + public void onPullDownReleasing(TwinklingRefreshLayout refreshLayout, float fraction) { + if (Math.abs(fraction - 1.0f) > 0) { + mToolbar.setVisibility(View.VISIBLE); + ImmersionBar.with(OneFragment.this).statusBarDarkFont(false).init(); + } else { + mToolbar.setVisibility(View.GONE); + ImmersionBar.with(OneFragment.this).statusBarDarkFont(true).init(); + } + } + }); + } + + private List addData() { + List data = new ArrayList<>(); + for (int i = mItemList.size() + 1; i <= mItemList.size() + 20; i++) { + data.add("item" + i); + } + return data; + } + + private List newData() { + List data = new ArrayList<>(); + for (int i = 1; i <= 20; i++) { + data.add("item" + i); + } + return data; } @Override protected void immersionInit() { - ImmersionBar.with(getActivity()) - .statusBarDarkFont(false) - .navigationBarColor(R.color.btn4) + ImmersionBar.with(this) + .titleBar(mToolbar) + .navigationBarColor(R.color.colorPrimary) .init(); } } diff --git a/sample/src/main/java/com/gyf/immersionbar/fragment/ThreeFragment.java b/sample/src/main/java/com/gyf/immersionbar/fragment/ThreeFragment.java index 8f51312..b823e1b 100644 --- a/sample/src/main/java/com/gyf/immersionbar/fragment/ThreeFragment.java +++ b/sample/src/main/java/com/gyf/immersionbar/fragment/ThreeFragment.java @@ -1,31 +1,34 @@ package com.gyf.immersionbar.fragment; -import android.os.Bundle; -import android.support.annotation.Nullable; -import android.view.LayoutInflater; -import android.view.View; -import android.view.ViewGroup; +import android.support.v7.widget.Toolbar; import com.gyf.barlibrary.ImmersionBar; -import com.gyf.barlibrary.ImmersionFragment; import com.gyf.immersionbar.R; +import butterknife.BindView; + /** * Created by geyifeng on 2017/5/12. */ -public class ThreeFragment extends ImmersionFragment { +public class ThreeFragment extends BaseFragment { - @Nullable + @BindView(R.id.toolbar) + Toolbar toolbar; @Override - public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { - return inflater.inflate(R.layout.fragment_three, container, false); + protected int setLayoutId() { + return R.layout.fragment_three; + } + + @Override + protected void initData() { + } @Override protected void immersionInit() { - ImmersionBar.with(getActivity()) - .statusBarDarkFont(false) + ImmersionBar.with(this) + .titleBar(toolbar) .navigationBarColor(R.color.btn13) .init(); } diff --git a/sample/src/main/java/com/gyf/immersionbar/fragment/TwoFragment.java b/sample/src/main/java/com/gyf/immersionbar/fragment/TwoFragment.java index daf8c48..6ff04a2 100644 --- a/sample/src/main/java/com/gyf/immersionbar/fragment/TwoFragment.java +++ b/sample/src/main/java/com/gyf/immersionbar/fragment/TwoFragment.java @@ -1,31 +1,36 @@ package com.gyf.immersionbar.fragment; -import android.os.Bundle; -import android.support.annotation.Nullable; -import android.view.LayoutInflater; -import android.view.View; -import android.view.ViewGroup; +import android.support.v7.widget.Toolbar; import com.gyf.barlibrary.ImmersionBar; -import com.gyf.barlibrary.ImmersionFragment; import com.gyf.immersionbar.R; +import butterknife.BindView; + /** * Created by geyifeng on 2017/5/12. */ -public class TwoFragment extends ImmersionFragment { +public class TwoFragment extends BaseFragment { + + @BindView(R.id.toolbar) + Toolbar toolbar; + + @Override + protected int setLayoutId() { + return R.layout.fragment_two; + } - @Nullable @Override - public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { - return inflater.inflate(R.layout.fragment_two, container, false); + protected void initData() { + } @Override protected void immersionInit() { - ImmersionBar.with(getActivity()) - .statusBarDarkFont(true) + ImmersionBar.with(this) + .titleBar(toolbar, true) + .statusBarDarkFont(true, 0.2f) .navigationBarColor(R.color.btn3) .init(); } diff --git a/sample/src/main/java/com/gyf/immersionbar/utils/GlideImageLoader.java b/sample/src/main/java/com/gyf/immersionbar/utils/GlideImageLoader.java new file mode 100644 index 0000000..8ebb7c0 --- /dev/null +++ b/sample/src/main/java/com/gyf/immersionbar/utils/GlideImageLoader.java @@ -0,0 +1,25 @@ +package com.gyf.immersionbar.utils; + +import android.content.Context; +import android.widget.ImageView; + +import com.bumptech.glide.Glide; +import com.bumptech.glide.load.engine.DiskCacheStrategy; +import com.gyf.immersionbar.R; +import com.youth.banner.loader.ImageLoader; + +/** + * Created by geyifeng on 2017/6/4. + */ + +public class GlideImageLoader extends ImageLoader { + @Override + public void displayImage(Context context, Object path, ImageView imageView) { + Glide.with(context.getApplicationContext()) + .load(path) + .diskCacheStrategy(DiskCacheStrategy.SOURCE) + .placeholder(R.mipmap.test) + .into(imageView); + } + +} diff --git a/sample/src/main/java/com/gyf/immersionbar/view/CustomViewPager.java b/sample/src/main/java/com/gyf/immersionbar/view/CustomViewPager.java new file mode 100644 index 0000000..ad6eaf6 --- /dev/null +++ b/sample/src/main/java/com/gyf/immersionbar/view/CustomViewPager.java @@ -0,0 +1,48 @@ +package com.gyf.immersionbar.view; + +import android.content.Context; +import android.support.v4.view.ViewPager; +import android.util.AttributeSet; +import android.view.MotionEvent; + +/** + * 设置viewpager不可滑动 默认可以滑动 + * Created by geyifeng on 2017/1/14. + */ + +public class CustomViewPager extends ViewPager { + + private boolean isScroll = true; + + public CustomViewPager(Context context) { + super(context); + } + + public CustomViewPager(Context context, AttributeSet attrs) { + super(context, attrs); + } + + /** + * Sets scroll. true 可以滑动 false 不可以滑动 + * + * @param isScroll the is scroll + */ + public void setScroll(boolean isScroll) { + this.isScroll = isScroll; + } + + @Override + public boolean onTouchEvent(MotionEvent ev) { + return isScroll && super.onTouchEvent(ev); + } + + @Override + public boolean onInterceptTouchEvent(MotionEvent ev) { + return isScroll && super.onInterceptTouchEvent(ev); + } + + @Override + public void setCurrentItem(int item) { + setCurrentItem(item,false); + } +} diff --git a/sample/src/main/res/drawable/shape_seek.xml b/sample/src/main/res/drawable/shape_seek.xml new file mode 100644 index 0000000..10ada52 --- /dev/null +++ b/sample/src/main/res/drawable/shape_seek.xml @@ -0,0 +1,8 @@ + + + + + + \ No newline at end of file diff --git a/sample/src/main/res/layout/activity_action_bar.xml b/sample/src/main/res/layout/activity_action_bar.xml new file mode 100644 index 0000000..c8d1ba0 --- /dev/null +++ b/sample/src/main/res/layout/activity_action_bar.xml @@ -0,0 +1,25 @@ + + + + + + + +