編輯:關於android開發
作為一個碼農,什麼都不用多講,貼代碼
build project
build module
Application
public class App extends Application { private AppComponent appComponent; @Override public void onCreate() { super.onCreate(); appComponent= DaggerAppComponent.builder() .appModule(new AppModule(this)) .build(); } public AppComponent getAppComponent() { return appComponent; } } View CodeAppComponent@Singleton @Component(modules = { AppModule.class }) public interface AppComponent { @ContextLife("Application") App getApplication(); RetrofitHelper retrofitHelper(); //提供http的幫助類 } View Code
AppModule@Module public class AppModule { private App application; public AppModule(App application){ this.application=application; } @Provides @Singleton @ContextLife("Application") public App provideApplication(){ return application; } @Provides @Singleton public RetrofitHelper provideRetrofitHelper() { return new RetrofitHelper(); } } View Code
ActivityComponent@ActivityScope @Component(dependencies = AppComponent.class, modules = ActivityModule.class)//關聯上下 public interface ActivityComponent { Activity getActivity(); void inject(LoginActivity loginActivity);//要在類中使用注入功能 void inject(MainActivity mainActivity); } View Code
ActivityModule@Module public class ActivityModule { private Activity mActivity; public ActivityModule(Activity activity) { this.mActivity = activity; } @Provides @ActivityScope public Activity provideActivity() { return mActivity; } @Provides @ActivityScope public IPresenterFactory providePresenter(RetrofitHelper retrofitHelper) { return new PresenterFactory(retrofitHelper); } } View Code
Base裡面使用注入,否則無法注入
public abstract class PresenterActivity extends BaseActivity { private final static String TAG = "PresenterActivity"; protected abstract void initInject(); @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); initInject(); } protected ActivityComponent getActivityComponent(){ return DaggerActivityComponent.builder() .appComponent(App.getInstance().getAppComponent()) .activityModule(getActivityModule()) .build(); } private ActivityModule getActivityModule() { return new ActivityModule(this); } } View Code此時前提條件都有了
點擊運行自動生成注入代碼(沒問題的前提)
使用注入:
public class MainActivity extends PresenterActivity { @Inject protected IPresenterFactory mPresenter;//沒有創建對象,inject標簽注入類型 private final int BASIC_PERMISSION_REQUEST_CODE = 100; private TextView username; @Override public void findView() { username = findView(R.id.username); username.setText(App.getInstance().getUserEntity().getName()+" hello"); } @Override public void init() { if (mPresenter != null){ Toasts.showShort("mPresenter success",context);//注入成功會打印出success } } @Override public int layoutResID() { return R.layout.activity_main; } @Override protected void initInject() { getActivityComponent().inject(this); } @Subscribe(threadMode = ThreadMode.MAIN) public void onEvent() { Toasts.showShort("刷新UI",context); } }
Android中Action Bar的使用 內容概要 示例演示和基本介紹 啟用Action Bar 在Action Bar上添加按鈕 自定義Action Bar樣式 自動
ViewPager的刷新、限制預加載、緩存所有,viewpager緩存【框架】: 公共部分:左側菜單、TitleBar、RadioGroup(3個RadioButton
面面具到!android重力傳感器,前兩篇都是向大家介紹了很有意思的兩種手勢操作,嵌入我們游戲中,不得不說讓游戲的自由度、可玩性和趣味性都增色不少!那麼今天繼續給大家介紹
Android 急速發布項目到 JitPack,androidjitpack 轉載請標明出處: http://www.cnblogs.com/zhaoyanjun/p/