Android 專案總結(1)_20180808
關於前期專案中的問題,做一下總結:後續還是用MK來寫,直接複製,文字樣式沒了。
1.List物件 排序
public interface Comparable<T> {
int compareTo(T var1);
}
讓Bean執行此介面,重寫compareTo() 來對比對應的屬性
Collections.sort(output); 最後使用集合的sort方法即可
2. 組合View
public class HeaderViewextends RelativeLayout {
public HeaderView(Context context, AttributeSet attrs) {
super(context, attrs);
initView(context);
}
public void initView(Context context) {
View view = LayoutInflater.from(context).inflate(R.layout.header, this);
}
在子View 比較多,全部在Activity 中程式碼比較雜亂時,通過抽象成View的模式
set/get 一些引數和屬性,程式碼更加清晰(配合MVP 模式)
(Contract 中定義presenter資料代理+view顯示,Activity/Fragment 中只負責主要的介面顯示)
3.獲取佈局截圖
frameLayout.setDrawingCacheEnabled(true);
Bitmap tBitmap = frameLayout.getDrawingCache();
// 拷貝圖片,否則在setDrawingCacheEnabled(false)以後該圖片會被釋放掉
tBitmap = tBitmap.createBitmap(tBitmap);
frameLayout.setDrawingCacheEnabled(false);
需要設定Activity為 android:hardwareAccelerated="false" (否則拿到的為null)
從圖片快取中獲取Bitmap
4.列舉與Int
public enum MoreBean {
MoreActivity(0),
ScannerActivity(1);
private int type;
MoreBean(int iValue) {
this.type = iValue;
}
public int type() {
return this.type;
}
}
MoreBean myType = MoreBean.values()[1]; index 轉化為列舉
int index = MoreBean.MoreActivity.type(); 列舉轉化為 int
4.基於MigrationHelper的DB保留資料升級
@Override
public void onUpgrade(Database db, int oldVersion, int newVersion) {
MigrationHelper.migrate(db, new MigrationHelper.ReCreateAllTableListener() {
@Override
public void onDropAllTables(Database db, boolean ifExists) {
DaoMaster.dropAllTables(db, ifExists);
}
}, VideosDao.class);
}
Helper直接拿來用
5.轉化File與Uri
Android N以後,不能直接從檔案得到Uri
需要設定manifest FileProvider 並配合
File file = new File(picturePath);
Uri uri = null;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
uri = FileProvider.getUriForFile(this, BuildConfig.APPLICATION_ID+".provider", file);
} else {
uri = Uri.fromFile(file);
}
6.WebView 與alert
@Override
public boolean onJsAlert(WebView view, String url, String message, JsResult result) {
Toast.makeText(getActivity(), message, Toast.LENGTH_SHORT).show();
result.confirm();//return true,表示攔截alert, 需要給js返回結果,否則js不會往下執行
return true;
}
WebView 中捕獲js的alert 彈窗,一定要有 JsResult 的確認,否則js不會往下執行
6.遮蔽返回按鍵
配合style <item name="android:windowCloseOnTouchOutside">false</item> 來使用
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(keyCode == KeyEvent.KEYCODE_BACK){
return true;
}
return super.onKeyDown(keyCode, event);
}
7.Fragment 與Adapter
Fragment中,onCreate 第一次建立時,初始化一些資料
onCreateView 在每次顯示,都會重新繪製view 對於一些非實時資料,不宜多次refresh 資料,而應該在onCreate
中進行請求,結合onCreate 中建立Adapter 後續只是View的渲染,資料不用變
Adapter介面卡作為資料而存在,可以在onCreate 中建立,並new List 資料
後續再setData notifyDataChange()
8.ViewPager 預載入
1 ViewPager setOffscreenPageLimit 3
設定預載入量3,除了顯示的Fragment ,之後的3個Fragment會被預載入
9.Debuggable 除錯開關
1 android:debuggable="true"
設定引數,在編譯release 版本時,覆蓋安裝,可以run-as 檢視資料庫
10.Webview 查詢資料
存在資料不高亮的情況,通過反射呼叫setFindIsUp 設定即可
public void toogleSetFindIsUp(WebView webView) {
try {
for (Method m : WebView.class.getDeclaredMethods()) {
if (m.getName().equals("setFindIsUp")) {
m.setAccessible(true);
m.invoke((webView), true);
break;
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
11.圖片小紅點
圖片或文字上的小紅點:自定義FrameLayout 下的ImageView 和View(紅點)
配合attr 和set方法,設定紅點顯示與隱藏
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:id="@+id/red_image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerInParent="true" />
<View
android:id="@+id/red_point"
android:layout_width="@dimen/offset_5dp"
android:layout_height="@dimen/offset_5dp"
android:visibility="gone"
android:layout_gravity="end"
android:background="@drawable/all_item_red"/>
</FrameLayout>