啟動(dòng)Activity并傳遞參數(shù)
Extra
正常情況下啟動(dòng)Activity并且傳遞參數(shù)的代碼:
Intent intent = new Intent(context,LoginActivity.class);intent.putExtra("phone","123456);intent.putExtra("pwd","123456);startActivity(intent);使用Blade啟動(dòng)Activity的方式
public class LoginActivity extends AppcompatActivity{ @Extra String mText; @Extra MyData mData;}通過(guò)上面的代碼就會(huì)自動(dòng)生成一個(gè)如下兩個(gè)方法
Intent forX(Context c, T1 extra1[, T2 extra2, ...])void startX(Context c, T1 extra1[, T2 extra2, ...])
然后我們就可以直接通過(guò) I.startLoginActivity 來(lái)啟動(dòng)Activity。
創(chuàng)建Fragment實(shí)例
@Arg
用來(lái)為Fragment生成newInstance方法
通常我們創(chuàng)建Fragment對(duì)象都是些如下的樣板代碼
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
當(dāng)我們創(chuàng)建一個(gè)實(shí)體類需要實(shí)現(xiàn)Parcelable的時(shí)候,可以按如下的方法寫
@blade.Parcelpublic 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) { }}如果某個(gè)字段想忽略,不需要被序列化的話,使用 @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)建一個(gè)繼承自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的實(shí)現(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
簡(jiǎn)化狀態(tài)管理, @State 注解會(huì)生成一個(gè)幫助類,里面包含兩個(gè)靜態(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); }繼承自Fragment、Activity或View的類都會(huì)自動(dòng)管理狀態(tài)。自定義序列化的功能如上所示。
Blade可以讓我們少寫很多的樣板代碼,具體的我還沒(méi)有應(yīng)用到項(xiàng)目中,之后會(huì)在項(xiàng)目中進(jìn)行使用,用來(lái)使項(xiàng)目看起來(lái)更加清晰。
Blade地址: https://github.com/FrantisekGazo/Blade
總結(jié)
以上所述是小編給大家介紹的Android中Blade的使用方法,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)武林網(wǎng)網(wǎng)站的支持!
新聞熱點(diǎn)
疑難解答
圖片精選