Then yesterday's topic, yesterday talked about how to achieve "reading" and "welfare" two modules of the general realization of ideas. Today we will do some basic work.
First of all, I must have created the package. Below is my project package structure.
Then, let's start with BaseActivity and BaseFragment.
The code is as follows:
public abstract class BaseActivity extends AppCompatActivity {
protected abstract
@LayoutRes
int getLayoutId();
protected Toolbar toolbar;
protected abstract void initViews(Bundle savedInstanceState);
protected abstract void loadData();
/**
* Initialize ToorBar
*/
private void initToolBar() {
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
initTheme();
setContentView(getLayoutId());
initToolBar();
initViews(savedInstanceState);
loadData();
}
// Add a return button to the left of the top left Icon
protected void setDisplayHomeAsUpEnabled(boolean enable) {
if (getSupportActionBar() != null)
getSupportActionBar().setDisplayHomeAsUpEnabled(enable);
}
/**
* Menu Creation
* @param item Menu Item
* @return
*/
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == android.R.id.home) {
onBackPressed();
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* set up themes
*/
private void initTheme() {
SharedPreferences pf = getSharedPreferences(AppGlobal.FILE_NAME, Context.MODE_PRIVATE);
int themeIndex = pf.getInt("theme",0);
switch (themeIndex) {
case 0:
setTheme(R.style.LapisBlueTheme);
break;
case 1:
setTheme(R.style.PaleDogwoodTheme);
break;
case 2:
setTheme(R.style.GreeneryTheme);
break;
case 3:
setTheme(R.style.PrimroseYellowTheme);
break;
case 4:
setTheme(R.style.FlameTheme);
break;
case 5:
setTheme(R.style.IslandParadiseTheme);
break;
case 6:
setTheme(R.style.KaleTheme);
break;
case 7:
setTheme(R.style.PinkYarrowTheme);
break;
case 8:
setTheme(R.style.NiagaraTheme);
break;
}
}
}
Mainly some basic View initialization and some common operations, as well as theme settings, in order to facilitate the later change of theme. In the future, our Activity just needs to inherit this BaseActivity. The initViews (Bundle saved Instance State) method is used to initialize the View, while the loadData() method is used to load the data. We just need to implement these two methods, instead of calling the onCreat() method every time.
Similarly, here's the code for BaseFragment
public abstract class BaseFragment extends Fragment {
private boolean isViewPrepared; // Identification fragment view has been initialized
private boolean hasFetchData; // Identification has triggered lazy loading of data
protected ViewDataBinding binding;
protected abstract
@LayoutRes
int getLayoutId();
public ViewDataBinding getBinding(){
return binding;
}
//Initialize View
protected abstract void initViews();
//Lazy load
protected abstract void lazyFetchData();
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
binding = DataBindingUtil.inflate(inflater, getLayoutId(), container, false);
initViews();
return binding.getRoot();
}
/**
* Determine whether Fragment lazy loading is ready to complete
*/
private void lazyFetchDataIfPrepared() {
if (getUserVisibleHint() && !hasFetchData && isViewPrepared) {
hasFetchData = true;
lazyFetchData();
}
}
/**
* User Visibility
*
* @param isVisibleToUser
*/
@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);
if (isVisibleToUser) {
lazyFetchDataIfPrepared();
}
}
/**
* View Create Completion
*
* @param view
* @param savedInstanceState
*/
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
isViewPrepared = true;
lazyFetchDataIfPrepared();
}
@Override
public void onDestroyView() {
super.onDestroyView();
hasFetchData = false;
isViewPrepared = false;
}
}
Like the BaseActivity above, some common operations are encapsulated. It's worth mentioning that I used Data Binding and lazy loading techniques in the above code. If you haven't used it, you can enter the following portal.
DataBindIng portal
Lazy Loading Portal
Or, change the above code to not use DataBinding.
The code is as follows
public abstract class BaseFragment extends Fragment {
private boolean isViewPrepared; // Identification fragment view has been initialized
private boolean hasFetchData; // Identification has triggered lazy loading of data
protected View mRootView;
protected abstract
@LayoutRes
int getLayoutId();
protected abstract void initViews();
protected abstract void lazyFetchData();
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
mRootView = inflater.inflate(getLayoutId(), container, false);
initViews();
return mRootView;
}
private void lazyFetchDataIfPrepared() {
if (getUserVisibleHint() && !hasFetchData && isViewPrepared) {
hasFetchData = true;
lazyFetchData();
}
}
@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);
if (isVisibleToUser) {
lazyFetchDataIfPrepared();
}
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
isViewPrepared = true;
lazyFetchDataIfPrepared();
}
@Override
public void onDestroyView() {
super.onDestroyView();
hasFetchData = false;
isViewPrepared = false;
}
protected <T extends View> T findView(@IdRes int id) {
return (T) mRootView.findViewById(id);
}
}
Use the Base class above to let your Activity and Fragment classes say goodbye to onCreate() and onCreateView(), which are essential tools for knocking code at home.
Welcome to the following. Well, there's no further information.