wordpress多用戶后臺(tái)windows10優(yōu)化大師
1.ViewModel
解決問題:
- 瞬態(tài)數(shù)據(jù)丟失
- 異步調(diào)用內(nèi)存泄漏
- 類膨脹提高維護(hù)難度和測(cè)試難度
作用:
- 介于View視圖和Model數(shù)據(jù)模型之間橋梁
- 使視圖和數(shù)據(jù)能夠分離,也能保持通信
public class MainActivity extends AppCompatActivity {private TextView textView;private MyViewModel viewModel;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);textView = findViewById(R.id.textView);viewModel = new ViewModelProvider(this, new ViewModelProvider.AndroidViewModelFactory()).get(MyViewModel.class);textView.setText(String.valueOf(viewModel.number));}public void plusNumber(View view) {textView.setText(String.valueOf(++viewModel.number));}
}
public class MyViewModel extends ViewModel {public int number;}
2.LiveData
LiveData是Android架構(gòu)組件中的一部分,用于在數(shù)據(jù)源和UI之間建立連接,實(shí)現(xiàn)數(shù)據(jù)的實(shí)時(shí)更新和同步。它允許觀察者(例如Activity或Fragment)訂閱數(shù)據(jù)變化,并在數(shù)據(jù)發(fā)生變化時(shí)自動(dòng)更新UI。
- 基本概念:LiveData是一個(gè)可觀察的數(shù)據(jù)持有者類,它遵循觀察者模式。這意味著它可以持有數(shù)據(jù),并且當(dāng)數(shù)據(jù)發(fā)生變化時(shí),它會(huì)通知所有已訂閱的觀察者。這在處理生命周期相關(guān)的數(shù)據(jù)時(shí)非常有用,因?yàn)樗梢源_保只有當(dāng)活動(dòng)或片段處于激活狀態(tài)時(shí),才會(huì)接收到更新。
- 工作原理:LiveData的核心是LifecycleOwner,通常是Activity或Fragment。當(dāng)LiveData對(duì)象有新數(shù)據(jù)時(shí),它會(huì)檢查是否有活躍的觀察者,并通知它們。如果觀察者已經(jīng)銷毀,如Activity已經(jīng)停止或Fragment已經(jīng)分離,那么LiveData就不會(huì)再發(fā)送通知,從而避免了內(nèi)存泄漏。
- 使用步驟:要使用LiveData,首先需要在項(xiàng)目的build.gradle文件中添加依賴。然后,可以在ViewModel中創(chuàng)建LiveData對(duì)象,并在UI層(如Activity或Fragment)中觀察它。當(dāng)LiveData對(duì)象的數(shù)據(jù)改變時(shí),UI會(huì)自動(dòng)更新。
- 應(yīng)用場(chǎng)景:LiveData適用于多種場(chǎng)景,包括但不限于配置更改、網(wǎng)絡(luò)狀態(tài)變化、數(shù)據(jù)庫(kù)操作等。它特別適用于需要響應(yīng)配置更改(如屏幕旋轉(zhuǎn))的場(chǎng)景,因?yàn)樵谶@些情況下,觀察者可能會(huì)被銷毀并重新創(chuàng)建,而LiveData能夠確保在這些過程中不會(huì)發(fā)生內(nèi)存泄漏。
- 雙向綁定:雖然LiveData本身不直接支持雙向綁定,但可以與DataBinding庫(kù)結(jié)合使用,通過將LiveData對(duì)象與UI元素綁定來實(shí)現(xiàn)雙向綁定的效果。
- 數(shù)據(jù)更新:當(dāng)LiveData中的數(shù)據(jù)發(fā)生變化時(shí),所有訂閱了該LiveData的觀察者都會(huì)收到通知,從而實(shí)現(xiàn)UI的自動(dòng)更新。
- 事件處理:LiveData還提供了Transformations API,允許開發(fā)者對(duì)LiveData對(duì)象進(jìn)行轉(zhuǎn)換和組合,以實(shí)現(xiàn)更復(fù)雜的數(shù)據(jù)處理邏輯。
- 高級(jí)功能:除了基本的數(shù)據(jù)持有和通知功能外,LiveData還提供了一些高級(jí)功能,如切換地圖類型、設(shè)置篩選條件等,這些功能使得LiveData更加靈活和強(qiáng)大。
public class MyViewModel extends ViewModel {private MutableLiveData<Integer> currentSecond;public MutableLiveData<Integer> getCurrentSecond(){if (currentSecond == null){currentSecond = new MutableLiveData<>();currentSecond.setValue(0);}return currentSecond;}
}
public class MainActivity extends AppCompatActivity {private TextView textView;private MyViewModel viewModel;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);textView = findViewById(R.id.textView);viewModel = new ViewModelProvider(this, new ViewModelProvider.AndroidViewModelFactory()).get(MyViewModel.class);textView.setText(String.valueOf(viewModel.getCurrentSecond()));viewModel.getCurrentSecond().observe(this, new Observer<Integer>() {@Overridepublic void onChanged(Integer integer) {textView.setText(String.valueOf(integer));}});startTimer();}private void startTimer(){new Timer().schedule(new TimerTask() {@Overridepublic void run() {// 非ui線程 postValue// ui線程 setValueviewModel.getCurrentSecond().postValue(viewModel.getCurrentSecond().getValue()+1);}},1000,1000);}
}
3.DataBinding
Android DataBinding是Google在Jetpack中推出的一款數(shù)據(jù)綁定的支持庫(kù),它通過將數(shù)據(jù)源直接與UI元素進(jìn)行綁定來實(shí)現(xiàn)數(shù)據(jù)與視圖之間的自動(dòng)更新。
對(duì)DataBinding的詳細(xì)介紹:
- 基本概念:DataBinding是一種強(qiáng)大的數(shù)據(jù)綁定技術(shù),它能夠?qū)崿F(xiàn)視圖和數(shù)據(jù)之間的自動(dòng)更新。開發(fā)者可以將數(shù)據(jù)直接綁定到視圖上,從而簡(jiǎn)化了視圖和數(shù)據(jù)之間的操作,提高了代碼的可讀性和可維護(hù)性。
- 工作原理:DataBinding的工作原理主要依賴于數(shù)據(jù)綁定引擎和數(shù)據(jù)對(duì)象。數(shù)據(jù)綁定引擎負(fù)責(zé)管理數(shù)據(jù)和視圖之間的關(guān)系,當(dāng)數(shù)據(jù)發(fā)生變化時(shí),引擎會(huì)自動(dòng)更新視圖。而數(shù)據(jù)對(duì)象則是數(shù)據(jù)的載體,它可以是任何對(duì)象,只要實(shí)現(xiàn)了相應(yīng)的數(shù)據(jù)接口。
- 使用步驟:啟用DataBinding需要在app module的build.gradle中添加相應(yīng)代碼。布局文件需要更改為layout節(jié)點(diǎn)并引入data節(jié)點(diǎn),同時(shí)創(chuàng)建好需要用到的Model。在Activity或Fragment中,用DataBindingUtil.setContentView(Activity activity, int layoutId)代替setContentView(int layoutId),同時(shí)初始化數(shù)據(jù),并進(jìn)行綁定。
- 應(yīng)用場(chǎng)景:列表展示、條件渲染、數(shù)據(jù)綁定布局、MVVM架構(gòu)等都是DataBinding的應(yīng)用場(chǎng)景。DataBinding與MVVM架構(gòu)相結(jié)合,可以實(shí)現(xiàn)更好的代碼分離和組件化開發(fā)。
- 雙向綁定:雙向綁定的效果是數(shù)據(jù)影響界面,界面變化也要使得數(shù)據(jù)發(fā)生變化。例如EditText輸入內(nèi)容時(shí),綁定的數(shù)據(jù)bean要跟著變化。
- 數(shù)據(jù)更新:Databinding通過使用實(shí)現(xiàn)Observable的數(shù)據(jù),當(dāng)數(shù)據(jù)更新的時(shí)候,自動(dòng)更新UI。監(jiān)聽對(duì)象變化更新。
- 事件處理:事件處理包括方法引用和監(jiān)聽綁定兩種方式。
- 高級(jí)功能:DataBinding支持在普通方法上添加@注解來添加自定義控件屬性。這提供了更大的靈活性,允許開發(fā)者根據(jù)需要定制控件的行為
public class MainActivity extends AppCompatActivity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);ActivityMainBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_main);Idol idol = new Idol("蔡徐坤", "五星");binding.setIdol(idol);}
}
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"><data><variablename="idol"type="com.zzzjian.databinding.Idol" /></data><androidx.constraintlayout.widget.ConstraintLayoutandroid:id="@+id/main"android:layout_width="match_parent"android:layout_height="match_parent"tools:context=".MainActivity"><androidx.constraintlayout.widget.Guidelineandroid:id="@+id/guideline"android:layout_width="wrap_content"android:layout_height="wrap_content"android:orientation="horizontal"app:layout_constraintGuide_begin="365dp" /><ImageViewandroid:id="@+id/imageView2"android:layout_width="300dip"android:layout_height="300dip"android:contentDescription="TODO"app:layout_constraintBottom_toTopOf="@+id/guideline"app:layout_constraintEnd_toEndOf="parent"app:layout_constraintStart_toStartOf="parent"android:src="@drawable/ikun"app:layout_constraintTop_toTopOf="parent"tools:src="@tools:sample/avatars" /><TextViewandroid:id="@+id/textView"android:layout_width="wrap_content"android:layout_height="wrap_content"tools:text="姓名"android:text="@{idol.name}"android:textSize="24sp"app:layout_constraintBottom_toBottomOf="parent"app:layout_constraintEnd_toEndOf="parent"app:layout_constraintStart_toStartOf="parent"app:layout_constraintTop_toTopOf="@+id/guideline"app:layout_constraintVertical_bias="0.245" /><TextViewandroid:id="@+id/textView2"android:layout_width="wrap_content"android:layout_height="wrap_content"tools:text="五星"android:text="@{idol.star}"android:textSize="24sp"app:layout_constraintBottom_toBottomOf="parent"app:layout_constraintEnd_toEndOf="parent"app:layout_constraintStart_toStartOf="parent"app:layout_constraintTop_toTopOf="@+id/guideline" /></androidx.constraintlayout.widget.ConstraintLayout>
</layout>