1616
1717package com .dylanc .loadinghelper .sample .base ;
1818
19- import android .view .ViewGroup ;
20-
2119import androidx .annotation .IdRes ;
2220import androidx .appcompat .app .AppCompatActivity ;
2321
2422import com .dylanc .loadinghelper .LoadingHelper ;
25- import com .dylanc .loadinghelper .ViewType ;
2623import com .dylanc .loadinghelper .sample .adapter .NavIconType ;
2724import com .dylanc .loadinghelper .sample .adapter .ToolbarAdapter ;
2825
2926/**
3027 * 这是耦合度较低的封装方式,没有任何抽象方法,可以很方便地将基类里的代码拷贝到其它项目的基类里使用。
3128 *
3229 * 使用该基类时注意以下事项:
33- * 显示对应视图之前需要注册适配器,可以设置全局适配器,某个页面想修改样式时再注册个新的适配器。
34- * 设置标题栏的方法应该根据项目需要进行编写,下面提供了参考示例。
30+ * 1. 显示对应视图之前需要注册适配器,可以设置全局适配器,某个页面想修改样式时再注册个新的适配器。
31+ * 2. 设置标题栏的方法应该根据项目需要进行编写,下面提供了参考示例。
3532 *
3633 * @author Dylan Cai
3734 */
@@ -42,29 +39,32 @@ public class BaseActivity extends AppCompatActivity {
4239
4340 @ Override
4441 public void setContentView (int layoutResID ) {
45- this .setContentView (layoutResID , android . R . id . content );
42+ this .setContentView (layoutResID , 0 );
4643 }
4744
4845 public void setContentView (int layoutResID , @ IdRes int contentViewId ) {
49- this .setContentView (layoutResID , contentViewId , null );
50- }
51-
52- public void setContentView (int layoutResID , @ IdRes int contentViewId , LoadingHelper .ContentAdapter <?> contentAdapter ) {
5346 super .setContentView (layoutResID );
54- loadingHelper = new LoadingHelper (((ViewGroup ) findViewById (contentViewId )).getChildAt (0 ), contentAdapter );
47+ if (contentViewId == 0 ) {
48+ loadingHelper = new LoadingHelper (this );
49+ } else {
50+ loadingHelper = new LoadingHelper (findViewById (contentViewId ));
51+ }
5552 loadingHelper .setOnReloadListener (this ::onReload );
5653 }
5754
55+ /**
56+ * 添加标题栏的示例方法,请根据自己的需求进行修改
57+ */
58+ public void setToolbar (String title ) {
59+ setToolbar (title , NavIconType .BACK , 0 );
60+ }
61+
5862 public void setToolbar (String title , NavIconType type ) {
5963 setToolbar (title , type , 0 );
6064 }
6165
62- /**
63- * 添加标题栏的示例方法,请根据自己的需求进行修改
64- */
6566 public void setToolbar (String title , NavIconType type , int menuId ) {
66- loadingHelper .register (ViewType .TITLE , new ToolbarAdapter (title , type , menuId , this ::onOptionsItemSelected ));
67- loadingHelper .setDecorHeader (ViewType .TITLE );
67+ loadingHelper .setDecorHeader (new ToolbarAdapter (title , type , menuId , this ::onOptionsItemSelected ));
6868 }
6969
7070 public void showLoadingView () {
0 commit comments