小知识点
有些简单的东西
1、gson字符串转对象数组
//把JSON格式的字符串轉成对象数组
ArrayList<Model> list = new Gson().fromJson((String) msg.obj, new TypeToken<ArrayList<Model>>() {
}.getType());
2、RecycleView或listview中操作adapter绑定的list集合的删除时,顺序会乱(删除混乱)
需要通过list.remove(adapter.position);来删除然后通过adapter.notifyDataSetChanged(adapter.position);来通知界面更新。
3、对包含有输入框的监听的RecycleView或listview的输入值重复或混乱问题
在getView()方法中
//判断是否有TextWatcher监听事件,有的话先移除
if (baseViewHolder.getView(R.id.et_words).getTag(R.id.tv_id) instanceof TextWatcher){
((EditText) baseViewHolder.getView(R.id.et_words)).removeTextChangedListener(((TextWatcher) baseViewHolder.getView(R.id.et_words).getTag(R.id.tv_id)));
}
//用一个空的textview存储我们列表中的唯一id,位置无论在哪,反正不显示
((TextView) baseViewHolder.getView(R.id.tv_id)).setVisibility(View.GONE);
//监听
TextWatcher textWatcher = new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
// todo 获取到edit改变前的数字
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void afterTextChanged(Editable editable) {
// todo 获取到edit改后的数字
model.word = editText.getText().toString();
}
};
//设置tag为TextWatcher
((EditText) baseViewHolder.getView(R.id.et_words)).addTextChangedListener(textWatcher);
//绑定到空的textview
baseViewHolder.getView(R.id.et_words).setTag(R.id.tv_id,textWatcher);
4、状态栏颜色和字体
import android.annotation.TargetApi;
import android.app.Activity;
import android.graphics.Color;
import android.os.Build;
import android.support.annotation.ColorInt;
import android.support.v4.graphics.ColorUtils;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import com.readystatesoftware.systembartint.SystemBarTintManager;
public class StatusBarUtil {
/**
* 修改状态栏为全透明
*
* @param activity
*/
@TargetApi(19)
public static void transparencyBar(Activity activity) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
Window window = activity.getWindow();
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
| View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.setStatusBarColor(Color.TRANSPARENT);
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
Window window = activity.getWindow();
window.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS,
WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
}
}
/**
* 修改状态栏颜色,支持4.4以上版本
*
* @param activity
* @param colorId
*/
public static void setStatusBarColor(Activity activity, @ColorInt int colorId) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
Window window = activity.getWindow();
// window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.setStatusBarColor(colorId);
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
//使用SystemBarTint库使4.4版本状态栏变色,需要先将状态栏设置为透明
transparencyBar(activity);
SystemBarTintManager tintManager = new SystemBarTintManager(activity);
tintManager.setStatusBarTintEnabled(true);
tintManager.setStatusBarTintResource(colorId);
}
// 如果亮色,设置状态栏文字为黑色
if (isLightColor(colorId)) {
changeStatusBarTextColor(activity, true);
} else {
changeStatusBarTextColor(activity, false);
}
}
/**
* 判断颜色是不是亮色
*
* @param color
* @return
* @from https://stackoverflow.com/questions/24260853/check-if-color-is-dark-or-light-in-android
*/
private static boolean isLightColor(@ColorInt int color) {
return ColorUtils.calculateLuminance(color) >= 0.5;
}
/**
* 6.0+设置状态栏字体
*
* @param activity
* @param isBlack
*/
public static void changeStatusBarTextColor(Activity activity, boolean isBlack) {
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.M) {
if (isBlack) {
activity.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);//设置状态栏黑色字体
} else {
activity.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE);//恢复状态栏白色字体
}
}
}
}
使用:浅色的状态栏字体会显示成白色,深色的状态栏字体为白色
优化到4.4系统模拟器可能出现的崩溃
package com.at.ecology.utils;
import android.annotation.TargetApi;
import android.app.Activity;
import android.graphics.Color;
import android.os.Build;
import android.support.annotation.ColorInt;
import android.support.v4.graphics.ColorUtils;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import com.at.ecology.R;
import com.readystatesoftware.systembartint.SystemBarTintManager;
public class StatusBarUtil {
/**
* 修改状态栏为全透明
*
* @param activity
*/
@TargetApi(19)
public static void transparencyBar(Activity activity) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
Window window = activity.getWindow();
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
| View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.setStatusBarColor(Color.TRANSPARENT);
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
Window window = activity.getWindow();
window.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS,
WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
}
}
/**
* 修改状态栏颜色,支持4.4以上版本
*
* @param activity
* @param colorId
*/
public static void setStatusBarColor(Activity activity, @ColorInt int colorId) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
Window window = activity.getWindow();
// window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.setStatusBarColor(colorId);
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
//使用SystemBarTint库使4.4版本状态栏变色,需要先将状态栏设置为透明
transparencyBar(activity);
SystemBarTintManager tintManager = new SystemBarTintManager(activity);
tintManager.setStatusBarTintEnabled(true);
if (colorId == -1){
tintManager.setStatusBarTintResource(R.color.color_FFFFFF);
}else {
tintManager.setStatusBarTintResource(colorId);
}
}
// 如果亮色,设置状态栏文字为黑色
if (isLightColor(colorId)) {
changeStatusBarTextColor(activity, true);
} else {
changeStatusBarTextColor(activity, false);
}
}
/**
* 判断颜色是不是亮色
*
* @param color
* @return
* @from https://stackoverflow.com/questions/24260853/check-if-color-is-dark-or-light-in-android
*/
private static boolean isLightColor(@ColorInt int color) {
return ColorUtils.calculateLuminance(color) >= 0.5;
}
/**
* 6.0+设置状态栏字体
*
* @param activity
* @param isBlack
*/
public static void changeStatusBarTextColor(Activity activity, boolean isBlack) {
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.M) {
if (isBlack) {
activity.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);//设置状态栏黑色字体
} else {
activity.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE);//恢复状态栏白色字体
}
}
}
}
//设置状态栏颜色
StatusBarUtil.setStatusBarColor(this, ContextCompat.getColor(this,R.color.StatusBgColor));