基類抽取BaseActivity BaseFragment 簡單框架
阿新 • • 發佈:2018-11-21
佈局
<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <Button android:id="@+id/Main_Btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello World!" /> </android.support.constraint.ConstraintLayout>
BaseActivity
import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v7.app.AppCompatActivity; import android.util.Log; import android.view.View; public abstract class BaseActivity extends AppCompatActivity implements View.OnClickListener { @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); init(); } //這個方法用來動態更改佈局資源 protected abstract int getLayoutId(); //資料 protected abstract void initData(); //初始化元件 protected abstract void findViewById(); //獲取上一個頁面傳過來的引數 protected abstract void getNextData(); //註冊監聽 protected abstract void setListener(); //頁面邏輯書寫 protected abstract void progressLogic(); void init(){ if (getLayoutId()!=0){ setContentView(getLayoutId()); initData(); findViewById(); getNextData(); setListener(); progressLogic(); } else{ Log.e("zzz","操作異常"); } } }
MainActivity繼承BaseActivity
import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v7.app.AppCompatActivity; import android.util.Log; import android.view.View; public abstract class BaseActivity extends AppCompatActivity implements View.OnClickListener { @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); init(); } //這個方法用來動態更改佈局資源 protected abstract int getLayoutId(); //資料 protected abstract void initData(); //初始化元件 protected abstract void findViewById(); //獲取上一個頁面傳過來的引數 protected abstract void getNextData(); //註冊監聽 protected abstract void setListener(); //頁面邏輯書寫 protected abstract void progressLogic(); void init(){ if (getLayoutId()!=0){ setContentView(getLayoutId()); initData(); findViewById(); getNextData(); setListener(); progressLogic(); } else{ Log.e("zzz","操作異常"); } } }
BaseFragment
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public abstract class BaseFragment extends Fragment implements View.OnClickListener {
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return initView(inflater, container);
}
protected abstract void initData();
//初始化元件
protected abstract void findViewById(View view);
//獲取上一個頁面傳過來的引數
protected abstract void getNextData();
//註冊監聽
protected abstract void setListener(
//頁面邏輯書寫
protected abstract void progressLogic();
View initView(LayoutInflater inflater, ViewGroup container){
View loading = getLoadView(inflater, container);
if (loading != null) {
findViewById(loading);
initData();
getNextData();
setListener();
progressLogic();
}
return loading;
}
protected abstract View getLoadView(LayoutInflater inflater, ViewGroup container);
}
AFragment繼承BaseFragment
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.example.day14_base.R;
import com.example.day14_base.base.BaseFragment;
public class AFragment extends BaseFragment {
@Override
protected void initData() {
}
@Override
protected void findViewById(View view) {
}
@Override
protected void getNextData() {
}
@Override
protected void setListener() {
}
@Override
protected void progressLogic() {
}
@Override
protected View getLoadView(LayoutInflater inflater, ViewGroup container) {
View inflate = inflater.inflate(R.layout.activity_main, container);
return inflate;
}
@Override
public void onClick(View v) {
}
}