- 不要在RecyclerView的控件内容更新中做耗时操作,每个控件理想的就是仅仅进行set操作
- 在JavaWebsocket中添加cookie:
public class WebSocketExampleClient extends WebSocketClient {
public WebSocketExampleClient( URI serverUri, Draft draft, Map<String, String> headers, int timeout) {
super( serverUri, draft, headers, timeout );
}
@Override
public void onOpen( ServerHandshake handshakedata ) {
Log.d("websocket", "open");
}
@Override
public void onMessage( String message ) {
final String msg = message;
Log.d("websocket", msg);
//Handle this message
}
@Override
public void onClose( int code, String reason, boolean remote ) {
Log.d("websocket", "closed");
}
@Override
public void onError( Exception ex ) {
ex.printStackTrace();
}
}
public class SocketHandler {
//Server IP address
private static String localipaddress = "192.168.1.123";
private static final String localWSSIP = "wss://" + localipaddress + ":9443/socket";
private static final int TIMEOUT = 10000;
public SocketHandler(String output, MainActivity main){
SocketConnector socketConnector = new SocketConnector();
socketConnector.sendString = output;
socketConnector.main = main;
socketConnector.execute();
}
private class SocketConnector extends AsyncTask<Void, Void, Void> {
private String sendString;
private MainActivity main;
@Override
protected Void doInBackground(Void... voids) {
PersistentCookieStore cookieStore = SingletonPersistentCookieStore.getInstance(main);
final Cookie cookie = cookieStore.getCookies().get(0);
try {
Map<String, String> cmap = new HashMap<String, String>();
String cookieString = cookie.getName()+"="+cookie.getValue();
cmap.put("cookie", cookieString);
URI uri = new URI(localWSSIP);
WebSocketExampleClient webSocketExampleClient = new WebSocketExampleClient(uri, new Draft_17(), cmap, TIMEOUT);
//This part is needed in case you are going to use self-signed certificates
TrustManager[] trustAllCerts = new TrustManager[]{new X509TrustManager() {
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
return new java.security.cert.X509Certificate[]{};
}
public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {}
public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {}
}};
try {
SSLContext sc = SSLContext.getInstance("TLS");
sc.init(null, trustAllCerts, new java.security.SecureRandom());
//Otherwise the line below is all that is needed.
//sc.init(null, null, null);
webSocketExampleClient.setWebSocketFactory(new DefaultSSLWebSocketClientFactory(sc));
} catch (Exception e) {
e.printStackTrace();
}
webSocketExampleClient.connectBlocking();
webSocketExampleClient.send(sendString);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
- 实现导航栏的五种方式:
navigationBar
radioGroup
textView + linearLayout
tabLayout + viewPager
tabHost - 代码重用继承父类后,记得把子类布局加载到父类中去,否则控件不能重用
- 弹窗的实现方式:PopUpWindow和AlterDialog,两者区别: AlertDialog在位置显示上是固定的,而PopupWindow则相对比较随意,能够在主屏幕上的任意位置显示。
- activity与fragment相互发消息的几种方式:利用handle进行消息传递;利用接口调用方式进行通信;相互获取对象实例进行方法调用
- Java小数点位数保留的方法:简书-Java小数点保留
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Formatter;
public final class PrecisionTest {
private PrecisionTest() {
}
/**
* 使用BigDecimal,保留小数点后两位
*/
public static String format1(double value) {
BigDecimal bd = new BigDecimal(value);
bd = bd.setScale(2, RoundingMode.HALF_UP);
return bd.toString();
}
/**
* 使用DecimalFormat,保留小数点后两位
*/
public static String format2(double value) {
DecimalFormat df = new DecimalFormat("0.00");
df.setRoundingMode(RoundingMode.HALF_UP);
return df.format(value);
}
/**
* 使用NumberFormat,保留小数点后两位
*/
public static String format3(double value) {
NumberFormat nf = NumberFormat.getNumberInstance();
nf.setMaximumFractionDigits(2);
/*
* setMinimumFractionDigits设置成2
*
* 如果不这么做,那么当value的值是100.00的时候返回100
*
* 而不是100.00
*/
nf.setMinimumFractionDigits(2);
nf.setRoundingMode(RoundingMode.HALF_UP);
/*
* 如果想输出的格式用逗号隔开,可以设置成true
*/
nf.setGroupingUsed(false);
return nf.format(value);
}
/**
* 使用java.util.Formatter,保留小数点后两位
*/
public static String format4(double value) {
/*
* %.2f % 表示 小数点前任意位数 2 表示两位小数 格式后的结果为 f 表示浮点型
*/
return new Formatter().format("%.2f", value).toString();
}
/**
* 使用String.format来实现。
*
* 这个方法其实和format4是一样的
*/
public static String format5(double value) {
return String.format("%.2f", value).toString();
}
}
- jsonNew.getString("lat") returns the String "null" and not null. Use !jsonNew.isNull("lat") etc. for the checks.json字符串解析的时候,如果value对应为空,那解析出的值是“null”字符串,而不是null对象
- recyclerView实现点击,长按事件的方法:在adapter中回调viewholder的点击事件;重写recyclerView的OnItemTouchEvent类来利用gestureDetector判断点击事件
- 在fragment中为recyclerView添加点击事件时,如果用到SimpleOnGestureListener接口,fragment中new出这个接口时要判断是否为空,为空则new一个,不为空则不再new新的,否则点击事件回调用两次,如下所示:
public class SimpleRecyclerViewItemClickListener extends RecyclerView.SimpleOnItemTouchListener {
private OnItemClickListener mListener;
private GestureDetectorCompat mGestureDetector;
public SimpleRecyclerViewItemClickListener(OnItemClickListener listener) {
this.mListener = listener;
}
@Override
public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {
if (mGestureDetector == null) {
initGestureDetector(rv);
}
return mGestureDetector.onTouchEvent(e);// 把事件交给GestureDetector处理
}
/**
* 初始化GestureDetector
*/
private void initGestureDetector(final RecyclerView recyclerView) {
mGestureDetector = new GestureDetectorCompat(recyclerView.getContext(), new GestureDetector.SimpleOnGestureListener() { // 这里选择SimpleOnGestureListener实现类,可以根据需要选择重写的方法
/**
* 单击事件
*/
@Override
public boolean onSingleTapUp(MotionEvent e) {
View childView = recyclerView.findChildViewUnder(e.getX(), e.getY());
if (childView != null && mListener != null) {
mListener.onItemClick(childView, recyclerView.getChildLayoutPosition(childView));
}
return false;
}
/**
* 长按事件
*/
@Override
public void onLongPress(MotionEvent e) {
View childView = recyclerView.findChildViewUnder(e.getX(), e.getY());
if (childView != null && mListener != null) {
mListener.onItemLongClick(childView, recyclerView.getChildLayoutPosition(childView));
}
}
});
}
public interface OnItemClickListener {
/**
* 当ItemView的单击事件触发时调用
*/
void onItemClick(View view, int position);
/**
* 当ItemView的长按事件触发时调用
*/
void onItemLongClick(View view, int position);
}
}
在fragment中初始化点击事件:</br>
private void setOnTouchListener() {
if (mRecView == null) {
return;
}
if (mTouchListener == null) {
mTouchListener = new SimpleRecyclerViewItemClickListener(new SimpleRecyclerViewItemClickListener.OnItemClickListener() {
@Override
public void onItemClick(View view, int position) {
}
@Override
public void onItemLongClick(View view, int position) {
}
});
mRecView.addOnItemTouchListener(mTouchListener);
}
}
如果没用到Gesture类的话,则貌似没有关系:
public class RecyclerViewTouchListener implements RecyclerView.OnItemTouchListener {
private int mLastDownX,mLastDownY;
//该值记录了最小滑动距离
private int touchSlop ;
private OnItemClickListener mListener;
//是否是单击事件
private boolean isSingleTapUp = false;
//是否是长按事件
private boolean isLongPressUp = false;
private boolean isMove = false;
private long mDownTime;
//内部接口,定义点击方法以及长按方法
public interface OnItemClickListener {
void onItemClick(View view, int position);
void onItemLongClick(View view, int position);
}
public RecyclerViewTouchListener(Context context, OnItemClickListener listener){
touchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
mListener = listener;
}
@Override
public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {
int x = (int) e.getX();
int y = (int) e.getY();
switch (e.getAction()){
/**
* 如果是ACTION_DOWN事件,那么记录当前按下的位置,
* 记录当前的系统时间。
*/
case MotionEvent.ACTION_DOWN:
mLastDownX = x;
mLastDownY = y;
mDownTime = System.currentTimeMillis();
isMove = false;
break;
/**
* 如果是ACTION_MOVE事件,此时根据TouchSlop判断用户在按下的时候是否滑动了,
* 如果滑动了,那么接下来将不处理点击事件
*/
case MotionEvent.ACTION_MOVE:
if(Math.abs(x - mLastDownX)>touchSlop || Math.abs(y - mLastDownY)>touchSlop){
isMove = true;
}
break;
/**
* 如果是ACTION_UP事件,那么根据isMove标志位来判断是否需要处理点击事件;
* 根据系统时间的差值来判断是哪种事件,如果按下事件超过1s,则认为是长按事件,
* 否则是单击事件。
*/
case MotionEvent.ACTION_UP:
if(isMove){
break;
}
if(System.currentTimeMillis()-mDownTime > 1000){
isLongPressUp = true;
}else {
isSingleTapUp = true;
}
break;
}
if(isSingleTapUp ){
//根据触摸坐标来获取childView
View childView = rv.findChildViewUnder(e.getX(),e.getY());
isSingleTapUp = false;
if(childView != null){
//回调mListener#onItemClick方法
mListener.onItemClick(childView,rv.getChildLayoutPosition(childView));
return true;
}
return false;
}
if (isLongPressUp ){
View childView = rv.findChildViewUnder(e.getX(),e.getY());
isLongPressUp = false;
if(childView != null){
mListener.onItemLongClick(childView, rv.getChildLayoutPosition(childView));
return true;
}
return false;
}
return false;
}
@Override
public void onTouchEvent(RecyclerView rv, MotionEvent e) {
}
@Override
public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {
}
}
全面介绍Android上git的使用:
全面介绍Android上git的使用一</br>
全面介绍Android上git的使用二</br>fragment的复用
private String mTitle;
public static QFragment newInstance(String title){
QFragment fragment = new QFragment();
Bundle bundle = new Bundle();
bundle.putString(KEY_FRAGMENT_TITLE, title);
fragment.setArguments(bundle);
return fragment;
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle bundle = getArguments();
mTitle = bundle.getString(KEY_FRAGMENT_TITLE);
setHasOptionsMenu(true);
}
- activity之间的通信
基于消息的通信机制 Intent ---boudle ,extra
数据类型有限,比如遇到不可序列化的数据Bitmap,InputStream, 或者LinkList链表等等数据类型就不太好用。
利用static静态数据,public static成员变量;
基于外部存储的传输, File/Preference/ Sqlite ,如果要针对第三方应用需要Content Provider
基于IPC的通信机制context 与Service之间的传输,如Activity与Service之间的通信,定义AIDL接口文件。
基于Application Context - fragment之间传递数据的三种方式:demo
利用fragmentManager通过fragment_id获取目标对象实实例,进而传递数据
利用回调
利用eventBus,高效优雅 -
获取屏幕或组件的各种大小尺寸等
获取屏幕分辨率:
//方式一, 通过WindowManager获取
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
System.out.println("heigth : " + dm.heightPixels);
System.out.println("width : " + dm.widthPixels);
// 方式二,通过Resources获取
DisplayMetrics dm2 = getResources().getDisplayMetrics();
System.out.println("heigth2 : " + dm2.heightPixels);
System.out.println("width2 : " + dm2.widthPixels);
// 方式三,获取屏幕的默认分辨率
Display display = getWindowManager().getDefaultDisplay();
System.out.println("width-display :" + display.getWidth());
System.out.println("heigth-display :" + display.getHeight());
//方式四,通过getSize获取分辨率
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;
if (android.os.Build.VERSION.SDK_INT >= 13) {
display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
width = size.x;
height = size.y;
}else {
display = getWindowManager().getDefaultDisplay();
width = display.getWidth();
height = display.getHeight();
}
获取屏幕的像素密度:
//方法一
DisplayMetrics dm = new DisplayMetrics();
dm = getResources().getDisplayMetrics();
float density = dm.density; // 屏幕密度(像素比例:0.75/1.0/1.5/2.0)
int densityDPI = dm.densityDpi; // 屏幕密度(每寸像素:120/160/240/320)
//方法二
dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
density = dm.density; // 屏幕密度(像素比例:0.75/1.0/1.5/2.0)
densityDPI = dm.densityDpi; // 屏幕密度(每寸像素:120/160/240/320)
获取屏幕的尺寸:
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
double x = Math.pow(dm.widthPixels/dm.xdpi,2);
double y = Math.pow(dm.heightPixels/dm.ydpi,2);
double screenInches = Math.sqrt(x+y); //屏幕尺寸(英寸)
获取文字的大小:
TextPaint paint = textView.getPaint();
float len = paint.measureText(string);
获取状态栏的高度:
//方式一
public static int getStatusBarHeight(Context context){
Class<?> c = null;
Object obj = null;
Field field = null;
int x = 0, statusBarHeight = 0;
try
{
c = Class.forName("com.android.internal.R$dimen");
obj = c.newInstance();
field = c.getField("status_bar_height");
x = Integer.parseInt(field.get(obj).toString());
statusBarHeight = context.getResources().getDimensionPixelSize(x);
} catch (Exception e1) {
e1.printStackTrace();
}
return statusBarHeight;
}
//方式二:
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
int width = dm.widthPixels; //屏幕宽
int height = dm.heightPixels; //屏幕高
Rect frame = new Rect();
getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
int statusBarHeight = frame.top; //状态栏高
int contentTop = getWindow().findViewById(Window.ID_ANDROID_CONTENT).getTop();
int titleBarHeight = contentTop - statusBarHeight; //标题栏高
获得导航栏的高度:
public int getNavigationBarHeight(Activity activity) {
Resources resources = activity.getResources();
int resourceId = resources.getIdentifier("navigation_bar_height","dimen", "android"); //获取NavigationBar的高度
int height = resources.getDimensionPixelSize(resourceId);
return height;
}
- 当AndroidStudio出现莫名其妙的类无法找到错误时, 试着把软件关掉重启看看
- android:allowBackup="false" 备份有安全风险, 建议设置为false
- android:hardwareAccelerated="true"硬件加速开启, 提高渲染效率, 默认开启. 硬件加速的主要原理, 就是通过底层软件代码, 将CPU不擅长的图形计算转换成GPU专用指令, 由GPU完成.硬件加速原理
- Activity、Window、ViewRootImpl和View之间的关系
ContextImpl:Context实现类。
PhoneWindow:Window唯一实现类。Window是一个抽象概念,是添加到WindowManager的根容器。
ViewRootImpl:ViewRootImpl是View的根,它控制了View的测量和绘制,同时持有WindowSession通过Binder与WMS通信,同时持有IWindow作为WSM的回调接口,用于例如touch事件的回调。
WindowManagerImpl:WindowManager和ViewManager的实现类,通过WindowManagerGlobal与WMS通信。
DecorView:继承FrameLayout,是视图树的根布局。
使用AS自带的tools/layout inspector可以看出,整个DecorView包含了三部分:navigationBarBackground为导航栏,statusBarBackground为状态栏,LinearLayout为当中内容部分,展开LinearLayout.FrameLayout,可以得到action_bar_container即actionbar或toolbar和content(R.id.content)即真正setContentView的目标。