1:下移隐藏,上移出现
//android.support.v4.widget.NestedScrollView nsvScroller;
//FrameLayout llDetailBottom; nsvScroller.setOnScrollChangeListener(new NestedScrollView.OnScrollChangeListener() {
@Override
public void onScrollChange(NestedScrollView v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
if(scrollY - oldScrollY > 0 && isBottomShow) { //下移隐藏
isBottomShow = false;
llDetailBottom.animate().translationY(llDetailBottom.getHeight());
} else if(scrollY - oldScrollY < 0 && !isBottomShow){ //上移出现
isBottomShow = true;
llDetailBottom.animate().translationY(0);
}
}
});
2:Rxjava 定时轮询,替代handler
//5s的延迟,5s的循环时间
Subscription subscription = Observable.interval(5, 5, TimeUnit.SECONDS).subscribeOn(AndroidSchedulers.mainThread())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Action1<Long>() {
@Override
public void call(Long aLong) {
// 进行轮播操作
Log.e("--------->", "xxxx" + (i++));
}
}); //结束定时
subscription.unsubscribe();
3:使用okhttp3对网络返回内容做缓存,还有日志、超时重连、头部消息的配置
private static void initOkHttp() {
OkHttpClient.Builder builder = new OkHttpClient.Builder();
// http://www.jianshu.com/p/93153b34310e
File cacheFile = new File(SyncStateContract.Constants.CONTENT_DIRECTORY);
Cache cache = new Cache(cacheFile, 1024 * 1024 * 50);
Interceptor cacheInterceptor = new Interceptor() {
@Override
public Response intercept(Chain chain) throws IOException {
Request request = chain.request();
if (!SystemUtil.isNetworkConnected()) {
request = request.newBuilder()
.cacheControl(CacheControl.FORCE_CACHE)
.build();
}
Response response = chain.proceed(request);
if (SystemUtil.isNetworkConnected()) {
int maxAge = 0;
// 有网络时, 不缓存, 最大保存时长为0
response.newBuilder()
.header("Cache-Control", "public, max-age=" + maxAge)
.removeHeader("Pragma")
.build();
} else {
// 无网络时,设置超时为4周
int maxStale = 60 * 60 * 24 * 28;
response.newBuilder()
.header("Cache-Control", "public, only-if-cached, max-stale=" + maxStale)
.removeHeader("Pragma")
.build();
}
return response;
}
};
//设置缓存
builder.addNetworkInterceptor(cacheInterceptor);
builder.addInterceptor(cacheInterceptor);
builder.cache(cache);
//设置超时
builder.connectTimeout(10, TimeUnit.SECONDS);
builder.readTimeout(20, TimeUnit.SECONDS);
builder.writeTimeout(20, TimeUnit.SECONDS);
//错误重连
builder.retryOnConnectionFailure(true);
OkHttpClient okHttpClient = builder.build();
}
4:腾讯X5SDK
com.tencent.smtt.sdk.WebView
5:退出程序
public void exitApp() {
AppManager.getAppManager().finishAllActivity();
android.os.Process.killProcess(android.os.Process.myPid());
System.exit(0);
}
6:夜间模式切换
//currentActivity extends AppCompatActivity
if (isNight) {
AppCompatDelegate.setDefaultNightMode(
AppCompatDelegate.MODE_NIGHT_YES);
} else {
AppCompatDelegate.setDefaultNightMode(
AppCompatDelegate.MODE_NIGHT_NO);
}
//当前页面的重新绘制
recreate();
资源文件改变:
drawable-xhdpi
drawable-night-xhdpi
colors:(color的name相同)
values
values-night
现在的四种模式:
MODE_NIGHT_NO 日间模式
MODE_NIGHT_YES 夜间模式
MODE_NIGHT_AUTO 根据时间自动切换日夜间模式
MODE_NIGHT_FOLLOW_SYSTEM 默认模式,就是跟随系统的设置,据说有可能以后会在android系统设置中添加日夜间模式的设置,此时如果你的app是默认模式,会根据系统设置变化日夜间模式