转载:http://www.cnblogs.com/wubingshenyin/p/4413672.html(原文连接)
前段时间看见ActionBar-PullToRefreshLibs用来刷新很好看,配合4.4以上支持的沉浸式效果更佳,于是便想配合沉浸式+ActionBar-PullToRefreshLibs做出一个效果,在自己的大神F2手机上看见没有问题,但是在三星note3上却出现了下拉actionbar刷新和actionbar错位的情况,非常郁闷。通过调试ActionBar-PullToRefreshLibs发现状态栏在设置沉浸式的时候在三星机器上的高度被抹了(rect.top=0),于是手动修改了ActionBar-PullToRefreshLibs的内部方法(PullToRefreshAttacher类中),实现了部分机器上的沉浸式+ActionBar-PullToRefreshLibs效果。修改原理是判断rect.top是否为0,如果为0就手动获取状态栏的高度给rect设置top,修改代码如下:
protected void addHeaderViewToActivity(View headerView) {
// Get the Display Rect of the Decor View
mActivity.getWindow().getDecorView()
.getWindowVisibleDisplayFrame(mRect);
if (mRect.top == 0)
mRect.top = getStatusBarHeight(mActivity);
// Honour the requested layout params
int width = WindowManager.LayoutParams.MATCH_PARENT;
int height = WindowManager.LayoutParams.WRAP_CONTENT;
ViewGroup.LayoutParams requestedLp = headerView.getLayoutParams();
if (requestedLp != null) {
width = requestedLp.width;
height = requestedLp.height;
} // Create LayoutParams for adding the View as a panel
WindowManager.LayoutParams wlp = new WindowManager.LayoutParams(width,
height, WindowManager.LayoutParams.TYPE_APPLICATION_PANEL,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
| WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE
| WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN,
PixelFormat.TRANSLUCENT);
wlp.x = 0;
wlp.y = mRect.top;
wlp.gravity = Gravity.TOP; // Workaround for Issue #182
headerView.setTag(wlp);
mActivity.getWindowManager().addView(headerView, wlp);
} protected void updateHeaderViewPosition(View headerView) {
// Refresh the Display Rect of the Decor View
mActivity.getWindow().getDecorView()
.getWindowVisibleDisplayFrame(mRect);
if (mRect.top == 0)
mRect.top = getStatusBarHeight(mActivity);
WindowManager.LayoutParams wlp = null;
if (headerView.getLayoutParams() instanceof WindowManager.LayoutParams) {
wlp = (WindowManager.LayoutParams) headerView.getLayoutParams();
} else if (headerView.getTag() instanceof WindowManager.LayoutParams) {
wlp = (WindowManager.LayoutParams) headerView.getTag();
} if (wlp != null && wlp.y != mRect.top) {
wlp.y = mRect.top;
mActivity.getWindowManager().updateViewLayout(headerView, wlp);
}
}
添加获取状态栏高度的方法
/**
* 获取状态栏高度
*
* @return
*/
protected int getStatusBarHeight(Context context) {
int result = 0;
int resourceId = context.getResources().getIdentifier(
"status_bar_height", "dimen", "android");
if (resourceId > 0) {
result = context.getResources().getDimensionPixelSize(resourceId);
}
return result;
}
运行效果图如下:
需要注意的地方:actionbar我没有用开源的第三方组件,也没有用自带的actionbar,而是把隐藏了title在封装的基类中自定义布局的actionbar。