亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Android中Blade的使用方法

 更新時間:2018年02月01日 10:53:47   作者:性格分裂程序媛  
這篇文章主要介紹了Android中Blade的使用實例詳解,需要的朋友可以參考下

啟動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);
 }

繼承自Fragment、Activity或View的類都會自動管理狀態(tài)。自定義序列化的功能如上所示。

Blade可以讓我們少寫很多的樣板代碼,具體的我還沒有應用到項目中,之后會在項目中進行使用,用來使項目看起來更加清晰。

Blade地址: https://github.com/FrantisekGazo/Blade

總結

以上所述是小編給大家介紹的Android中Blade的使用方法,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網站的支持!

相關文章

最新評論