01、设置状态栏的高度
布局
<View
android:id="@+id/status_line"
android:layout_width="match_parent"
android:layout_height="0dp" />
代码设置
int statusBarHeight = mContext.getResources().getDimensionPixelSize(mContext.getResources().getIdentifier("status_bar_height", "dimen", "android"));
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, statusBarHeight);
mTitleTop.setLayoutParams(params);
02、RecyclerView 屏蔽滑动 -- 解决ScrollView嵌套RecyclerView获得焦点问题
LinearLayoutManager mLayoutManager = new LinearLayoutManager(mContext) {
@Override
public boolean canScrollVertically() {
return false;
}
};
03、解决ScrollView嵌套ListView,进页面时,滑动到底部屏蔽listview的焦点
listview.setFocusable(false);
04、获得自定义View宽高
/**
* 获取到布局文件的高度和宽度
*
* @param child
*/
private void measureView(View child) {
ViewGroup.LayoutParams lp = child.getLayoutParams();
if (lp == null) {
lp = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
}
int childMeasureWidth = ViewGroup.getChildMeasureSpec(0, 0, lp.width);
int childMeasureHeight;
if (lp.height > 0) {
childMeasureHeight = View.MeasureSpec.makeMeasureSpec(lp.height, View.MeasureSpec.EXACTLY);
} else {
childMeasureHeight = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
}
child.measure(childMeasureWidth, childMeasureHeight);
}
使用
设置view measureView(自定义view);
高 childView.getMeasuredHeight()
宽 childView.getMeasuredWidth()
05、Google 推荐使用SparseArray,ArrayMap代替HashMap。
可以提高一倍的性能。
06、反射得到资源
/**
* 反射得到资源id
*
* @param context
* @param key
* 传入参数,如"R.drawable.ic_launcher", "R.id.textview"
* @return
*/
public static int getResourceId(Context context, String key) {
String[] keys = key.split("\\.");
Resources res = context.getResources();
return res.getIdentifier(keys[2], keys[1], context.getPackageName());
}
07、判断多次点击
private static long lastClickTime = 0;
private static final long DIFF = 800;
/**
* 判断两次点击的间隔,如果小于diff,则认为是多次无效点击
*
* @return
*/
public boolean isFastDoubleClick() {
long time = System.currentTimeMillis();
long timeD = time - lastClickTime;
lastClickTime = time;
if (timeD < DIFF) {
return true;
}
return false;
}