1.單例模式
https://www.cnblogs.com/Ycheng/p/7197751.html
作用:保證在Java應用程式中,一個類class只有一個例項存在;而且可以節省記憶體,因為它限制了例項的個數,有利於垃圾回收
1.餓漢模式
一開始的時候就進行了例項化,無論你到底用到與否,在類載入的時候就立即建立物件

【注意:】與 “懶載入”進行類比
private static TaskGoApplication instance;
複製程式碼
2.懶漢模式
等到需要使用的時候才進行建立
3.弊端--程式同步Bug-synchronized同步鎖
一個單例模式建立的物件是可以同時被多個執行緒處理的,如果一個物件被多個執行緒同時處理的話,就有可能出現執行緒同步問題。
解決方法:synchronized 同步鎖
http://blog.51cto.com/lavasoft/99155
2.“確認”Button 介面中靠右且有一定距離
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="11dp"
複製程式碼
3.文字覆蓋於按鈕之下

在位置佈局上進行設定

4.讓一段文字豎直方向上居中,使用gravity 而不是用layout_gravity
android:gravity="center_vertical"
複製程式碼
5.一個有生命的列表
以Information通知介面為例
1.InformationFragmrnt
public class InformFragment
extends BasePresenterFragment
implements InformationItemListener, SwipeRefreshLayout.OnRefreshListener
複製程式碼
自動重寫下面這些方法
/**
* SwipeRefreshLayout介面中的方法
*/
@Override
public void onRefresh() {
}
/**
* InforItemListener介面中的方法
*點選使用者頭像顯示使用者詳細資訊,點選確認按鈕觸發其他事件
* @param position
*/
@Override
public void onClickPicture(int position) {
}
@Override
public void onClickSure(int position) {
}
@Override
protected BaseContract.Presenter initPresenter() {
return null;
}
/**
* BasePresenterFragment中的方法
*
* @return
*/
@Override
protected int getLayoutId() {
return R.layout.fragment_information;
}
@Override
protected void initVariable() {
}
@Override
protected void initView() {
}
@Override
protected void loadData() {
}
/**
* generate-Bind自動建立的方法
* @param inflater
* @param container
* @param savedInstanceState
* @return
*/
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// TODO: inflate a fragment view
View rootView = super.onCreateView(inflater, container, savedInstanceState);
unbinder = ButterKnife.bind(this, rootView);
return rootView;
}
@Override
public void onDestroyView() {
super.onDestroyView();
unbinder.unbind();
}
複製程式碼
2.InformationItemListener
public interface InformationItemListener {
/**
* 點選頭像事件
* @param position
*/
void onClickPicture(int position);
/**
* 點選確認按鈕事件
* @param position
*/
void onClickSure(int position);
}
複製程式碼
3.R.layout.fragment_information

4.InformationModel
public class InformationModel {
public InformationModel(String pictureUrl,String receiveName,String releaseName){
this.pictureUrl=pictureUrl;
this.receiveName=receiveName;
this.releaseName=releaseName;
}
/**
* 頭像相關
*/
private String pictureUrl;
public String getPictureUrl() {
return pictureUrl;
}
public void setPictureUrl(String pictureUrl) {
this.pictureUrl = pictureUrl;
}
/**
* 送貨人相關
* @return
*/
public String getReceiveName() {
return receiveName;
}
public void setReceiveName(String receiveName) {
this.receiveName = receiveName;
}
private String receiveName;
/**
* 釋出者相關
* @return
*/
public String getReleaseName() {
return releaseName;
}
public void setReleaseName(String releaseName) {
this.releaseName = releaseName;
}
private String releaseName;
}
複製程式碼
5.InformationListRecAdapter
自動響應:
public class InformationListRecAdapter extends BaseRecyclerViewAdapter<InformationModel> {
public InformationListRecAdapter(Context context, List<InformationModel> informationModels) {
super(context, informationModels);
}
@Override
public BaseViewHolder<InformationModel> onCreateViewHolder(ViewGroup parent, int viewType) {
return null;
}
}
複製程式碼
補充:內部類ItemHolder
class ItemHolder extends BaseViewHolder<InformationModel> implements View.OnClickListener
複製程式碼
6.多執行緒
Java中實現多執行緒有兩種途徑:繼承Thread類活著實現Runnable介面
1.Thread
使用Thread的時候只需要new一個實力出來,呼叫start()方法即可以啟動一個執行緒
Thread Test = new Thread();
Test.start();
複製程式碼
2.Runnable
使用Runnable的時候需要先new一個繼承Runnable的例項,之後用子類Thread呼叫
Test impelements Runnable
Test t = new Test();
Thread test = new Thread(t);
複製程式碼
使用Runnable定義的子類中沒有start()方法,只有Thread類中才有
3. 例題
需要分別列印出a與b各10次,並且每列印一次a睡1秒,列印一次b睡2秒。 可以在run方法外面定義String word與int time. 之後用
Thread t1 = new Thread();
Thread t2 = new Thread();
t1.word = "a"
t1.time = 1000
t2.Word = "b"
t2.time = 2000
t1.start();
t2.start();
此處需要注意⚠️新建一個project用於測試Runnable不是一個簡單的Java編譯器,你需要讓這個inflater能夠找到啟動哪一個具體的Activity,而不是自己zz的取名一個“MainActivity”的類......呵呵呵-----所以需要 把之前想要實現的main裡面的方法,轉換為一個OnCreat()裡面的一個 initXXX


7.實現點選確認按鈕病吧這條item刪除,然後獲取相對應的id
還是以通知介面為例子
1.InformFragment.java實現InformationItemListener.java自動@Override裡的方法
public class InformFragment implements InformationItemListener{....}
複製程式碼

2.InformationItemListener.java-監聽器,監聽事件的發生

3.InformationListRecAdapter-介面卡,ItemHolder在此定義-在此一定將每一條item的position進行設定,對,源頭就在這裡,然後重返Fragment中的點選函式裡面,將 會發生的事情寫進去
8.對輸入的密碼進行隱藏設定
android:inputType="textPassword"
複製程式碼


9.設定按鈕的可點選事件
mbtnLogin.setClickable(true);
複製程式碼
10.onLoginFailed(...)與onWriteFailed(...)
登入介面關於View的介面回撥方法需注意:“輸入密碼錯誤“與”輸入格式不正確“is different
- 輸入的格式不正確:是指在Presenter驗證資料是否正確的isDataTrue()裡關於輸入的賬號為空、正規表示式要起u、密碼長度限制:過長或者過短 的判斷
- 輸入密碼錯誤:是指 經過網路請求過後 賬號密碼核對後有差異
因為我們想要區分這兩種狀態,是因為如果輸入的賬號正確,但是密碼錯誤,當我們再次輸入正確密碼的時候,“登入”按鈕變成了不可點選的狀態,即使密碼正確仍舊進入不了主介面
(因為isDatatrue判斷的是沒問題的,滿足了賬號密碼的位數,也滿足了正規表示式子,button的點選設定回不去true,所以我們還需要onLoginFailed(...)方法重新將mbtnLogin.setClickable(true);)

11.解決同時同步多個執行緒
針對重複多次快速點登入按鈕出現重複出現進入MainActivity介面的問題學習
大體上的思路?:

非同步執行緒和UI執行緒
凡是在這裡面寫的都是同步非同步執行緒?:

能看到的都是UI執行緒,比如點選一個 button實現介面Activity之間的跳轉......




12.在Presenter資料交換的過程中除了success方法還需要的fail方法在哪?
巨集宇封裝的底層中是?這樣:

接下來我們在介面中這樣使用
但注意⚠️,邏輯方面的考慮決定了我們把方法放在底層的Override中實現

13.Call與Observable
註釋部分是用RxJava,未註釋部分是用原始的網路請求


【小注意⚠️】

