Android Activity基类封装

编写一个Activity基类,对Activity进行封装,方便使用,参考知乎上一个帖子,可以点击这里查看

1、新建抽象类ParentActivity继承AppCompatActivity,并实现OnClickListener接口
public abstract class ParentActivity extends AppCompatActivity implements View.OnClickListener {
   @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO
    }
    
	@Override
    public void onClick(View v) {
        // TODO
    }
}
2、编写几个子类必须实现的抽象方法
/**
 * 设置ContentView
 * @return R.layout.xxx
 */
protected abstract int getLayoutId();

 /**
  * 初始化View
  */
protected abstract void initView();

/**
 * add Listener
 */
protected abstract void setListener();

/**
 * 初始化数据
 */
protected abstract void initData();

 /**
  * view点击
  * @param v
  */
public abstract void widgetClick(View v);
3、调用,顺序基本是设置布局,加载布局,事件监听,初始化数据,点击时加入了防止快速点击
public abstract class ParentActivity extends AppCompatActivity implements View.OnClickListener {
   @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(getLayoutId());
        initView();
        setListener();
        initData();
    }
    
	@Override
    public void onClick(View v) {
        if (fastClick())
            widgetClick(v);
    }
    
	private long lastClick = 0;
    private boolean fastClick() {
        if (System.currentTimeMillis() - lastClick <= 1000) {
            return false;
        }
        lastClick = System.currentTimeMillis();
        return true;
    }
}
4、添加title,这里没在基类写死,子类可以直接使用这个,也可以重写父类方法,这里使用了Toolbar,可以自己定义
  // 通用title
    protected void $setToolBar() {
        Toolbar toolbar = $findViewById(R.id.toolbar);
        toolbar.setTitle("");
        setSupportActionBar(toolbar);
        toolbar.setNavigationIcon(R.drawable.back);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }
5、加入一些使用多,通用功能
// findViewById
public  T $findViewById(int resId) {
     return (T) findViewById(resId);
}

// Toast
protected void $toast(CharSequence msg) {
     Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
}

// Toast
protected void $toast(int resId) {
     Toast.makeText(this, resId, Toast.LENGTH_SHORT).show();
}

protected boolean isDebug = true;
// Log
protected void $Log(String msg) {
     if (isDebug) {
         Log.d(this.getClass().getName(), msg);
     }
 }
6、startActivity通用样式编写
// startActivity
protected void $startActivity(Class cls, Bundle bundle) {
     Intent intent = new Intent(this, cls);
     if (bundle != null) {
         intent.putExtras(bundle);
      }
     startActivity(intent);
}

// startActivity
protected void $startActivity(Class cls) {
     $startActivity(cls, null);
}

// startActivityForResult
protected void $startActivityForResult(Class cls, Bundle bundle, int requestCode) {
     Intent intent = new Intent(this, cls);
     if (bundle != null) {
         intent.putExtras(bundle);
     }
     startActivityForResult(intent, requestCode);
 }
 
 // startActivityForResult
 protected void $startActivityForResult(Class cls, int requestCode) {
      $startActivityForResult(cls, null, requestCode);
 }
 
// getIntent
protected Bundle $getIntentExtra() {
      Intent intent = getIntent();
      Bundle bundle = null;
      if (null != intent)
         bundle = intent.getExtras();
      return bundle;
 }

上面简单的进行了封装,有许多不足之处,参考一下,自己根据实际情况再封装一下。

源码以及Demo地址:https://github.com/YBill/EncapsulateActivity

你可能感兴趣的:(android开发)