溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Blade如何在Android中使用

發(fā)布時間:2021-03-29 16:55:14 來源:億速云 閱讀:161 作者:Leah 欄目:移動開發(fā)

Blade如何在Android中使用?相信很多沒有經(jīng)驗的人對此束手無策,為此本文總結(jié)了問題出現(xiàn)的原因和解決方法,通過這篇文章希望你能解決這個問題。

啟動Activity并傳遞參數(shù)

Extra

正常情況下啟動Activity并且傳遞參數(shù)的代碼:

Intent intent = new Intent(context,LoginActivity.class);
intent.putExtra("phone","123456);
intent.putExtra("pwd","123456);
startActivity(intent);

使用Blade啟動Activity的方式

public class LoginActivity extends AppcompatActivity{
 @Extra
 String mText;
 @Extra
 MyData mData;
}

通過上面的代碼就會自動生成一個如下兩個方法

Intent forX(Context c, T1 extra1[, T2 extra2, ...])
void startX(Context c, T1 extra1[, T2 extra2, ...])

然后我們就可以直接通過 I.startLoginActivity 來啟動Activity。

創(chuàng)建Fragment實例

@Arg

用來為Fragment生成newInstance方法

通常我們創(chuàng)建Fragment對象都是些如下的樣板代碼

public class MyFragment extends Fragment{
 public MyFragment newInstance(String data){
 MyFragment fragment = new MyFragment();
 Bundle bundle = new Bundle();
 bundle.putExtra("data",data);
 fragment.setArguments(bundle);
 return fragment;
 }
 ...
}

使用Blade的方式

public class MyFragment extends Fragment {
 @Arg
 String mText;
 @Arg
 MyData mData;
}

自定義序列化

public class MyFragment extends Fragment {
 @Arg(MyTypeBundler.class)
 MyType mMyType;
}
public class MyTypeBundler implements Bundler<MyType> {
 void save(@Nullable final MyType value, @NonNull final Bundle state) {
 // save given value to the state
 }
 @Nullable
 MyType restore(@NonNull final Bundle state) {
 // restore and return value from state
 }
}

@Parcel

當我們創(chuàng)建一個實體類需要實現(xiàn)Parcelable的時候,可以按如下的方法寫

@blade.Parcel
public class MyClass implements Parcelable {
 String text;
 int number;
 boolean flag;
 double[] doubleArray;
 protected MyClass(Parcel in) {
 }
 @Override
 public int describeContents() {
  return 0;
 }
 @Override
 public void writeToParcel(Parcel dest, int flags) {
 }
}

如果某個字段想忽略,不需要被序列化的話,使用 @blade.ParcelIgnore

Mvp

Mvp是和Dager配合使用的。

第一步:在你的build.gradle中添加dager依賴

compile 'com.google.dagger:dagger:2.x'
apt 'com.google.dagger:dagger-compiler:2.x'

第二步:創(chuàng)建一個繼承自IView的接口

public interface IMyView extends blade.mvp.IView {
 void show(String something);
}

第三步:創(chuàng)建Prensenter和View接口相互影響

public class MyPresenter extends blade.mvp.BasePresenter<IMyView> {
 public void onUserDidSomething() {
 String s = // do something ...
 if (getView() != null) {
  getView().show(s);
 }
 }
 //...
}

第四步:創(chuàng)建View的實現(xiàn),使用@Inject注入Presenter,現(xiàn)在支持Fragmnt,Activit,View

public class MyView extends android.support.v4.app.Fragment implements IMyView {
 @Inject
 MyPresenter mPresenter;
 @Override
 public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
 super.onViewCreated(view, savedInstanceState);
 yourDaggerComponent.inject(this);
 }
 @Override
 void show(String something) { /* ... */ }
 // ...
}

第五步:Activity中包含存在Presenter的Fragment,View,那么該Activity需要使用@Blade注解一遍讓Blade知道。

State

簡化狀態(tài)管理, @State 注解會生成一個幫助類,里面包含兩個靜態(tài)的方法:

public class StateArgFragment extends Fragment {
 @Arg
 @State
 int num;
}
@Weave(
 into = "0_onSaveInstanceState",
 args = {"android.os.Bundle"},
 statement = "eu.f3rog.blade.sample.state.StateArgFragment_Helper.saveState(this, $1);"
 )
 public static void saveState(StateArgFragment target, Bundle state) {
 if (state == null) {
 throw new IllegalArgumentException("State cannot be null!");
 }
 BundleWrapper bundleWrapper = BundleWrapper.from(state);
 bundleWrapper.put("<Stateful-num>", target.num);
 }
 @Weave(
 into = "1^onCreate",
 args = {"android.os.Bundle"},
 statement = "eu.f3rog.blade.sample.state.StateArgFragment_Helper.restoreState(this, $1);"
 )
 public static void restoreState(StateArgFragment target, Bundle state) {
 if (state == null) {
 return;
 }
 BundleWrapper bundleWrapper = BundleWrapper.from(state);
 target.num = bundleWrapper.get("<Stateful-num>", target.num);
 }

看完上述內(nèi)容,你們掌握Blade如何在Android中使用的方法了嗎?如果還想學到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注億速云行業(yè)資訊頻道,感謝各位的閱讀!

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI