1.一般的封装。这种封装每次切换都会重新加载数据。有两种实现方式:
a.让子类传递一个View(一般是一个加载的布局)
public abstract class BaseFragment extends Fragment {
protected Activity mActivity;
@Override
public void onAttach(Context context) {
super.onAttach(context);
mActivity = (AppCompatActivity) context;
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return setLayoutView(inflater,container,savedInstanceState);
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
findViewById(view);
setViewData(view);
setClickEvent(view);
}
/**
* 设置布局
*
* @return
* @param inflater
* @param container
*/
public abstract View setLayoutView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState);
/**
* findViewById
*/
public void findViewById(View view){}
/**
* setViewData
*/
public void setViewData(View view){}
/**
* setClickEvent
*/
public void setClickEvent(View view){}
}
b.让子类传递一个id(一般是一个布局的id)
public abstract class BaseFragment1 extends Fragment {
protected Activity mActivity;
@Override
public void onAttach(Context context) {
super.onAttach(context);
mActivity = (AppCompatActivity) context;
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(setLayoutId(), container, false);
// return setLayoutView(inflater,container,savedInstanceState);
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
findViewById(view);
setViewData(view);
setClickEvent(view);
}
/**
* 设置布局
*
* @return
*/
public abstract int setLayoutId();
/**
* findViewById
*/
public void findViewById(View view){}
/**
* setViewData
*/
public void setViewData(View view){}
/**
* setClickEvent
*/
public void setClickEvent(View view){}
}
2.加强版封装。在实际项目中,我们不需要每次切换都加载数据,这时候就需要用到赖加载。这里采用继承basefragment的方式实现,因此,也有两种实现方式
a.继承BaseFragment
/**
* 创建日期:2018/11/28 on 11:02
* 描述: 赖加载
*/
public abstract class BaseLazyFragment1 extends BaseFragment {
View view = null;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
if (null != view) {
ViewGroup parent = (ViewGroup) view.getParent();
if (null != parent) {
parent.removeView(view);
}
} else {
view=setLayoutView(inflater,container,savedInstanceState);
initView(view);
}
return view;
}
protected abstract void initView(View view);
}
2.继承BaseFragment1
/**
* 创建日期:2018/11/28 on 11:02
* 描述: 赖加载:子类传递id
*/
public abstract class BaseLazy1Fragment1 extends BaseFragment1 {
View view=null;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
if (null != view) {
ViewGroup parent = (ViewGroup) view.getParent();
if (null != parent) {
parent.removeView(view);
}
} else {
view=inflater.inflate(getLayoutViewId(), container, false);
initView(view);
}
return view;
}
//初始化控件
protected abstract void initView(View view);
}
3.懒加载并不能满足我们的需求。因为在使用fragment和viewpage进行导航栏功能的时候,Android会默认预加载当前页面的前后各一页的数据,有时候我们不需要这种预加载,我们希望能够滑到那一页才进行网络请求加载数据,这样使用我们的APP时间间隔很少的情况下异步请求多次。
同样有两种实现方式,但是不同于第2中懒加载方式,这里可以随意继承上面两种懒加载。
a.继承BaseLazyFragment1 或者BaseLazy1Fragment1。
/**
* 创建日期:2018/11/28 on 11:02
* 描述: 赖加载+取消预加载
*/
public abstract class BaseLazyFragment2 extends BaseLazyFragment1 {
/**
* 是否初始化过布局
*/
protected boolean isViewInitiated;
/**
* 当前界面是否可见
*/
protected boolean isVisibleToUser;
/**
* 是否加载过数据
*/
protected boolean isDataInitiated;
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
isViewInitiated=true;
prepareFetchData();
}
@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);
this.isVisibleToUser = isVisibleToUser;
if (isVisibleToUser) {
prepareFetchData();
}
}
/**
* 懒加载
*/
// public abstract void fetchData();
protected abstract void loadData();
public void prepareFetchData() {
prepareFetchData(false);
}
/**
* 判断懒加载条件
*
* @param forceUpdate 强制更新,好像没什么用?
*/
public void prepareFetchData(boolean forceUpdate) {
if (isVisibleToUser && isViewInitiated && (!isDataInitiated || forceUpdate)) {
loadData();
isDataInitiated = true;
}
}
}
b.继承BaseLazyFragment1 或者BaseLazy1Fragment1。
/**
* 创建日期:2018/11/28 on 11:02
* 描述: 赖加载+取消预加载
*/
public abstract class BaseLazy2Fragment2 extends BaseLazyFragment1 {
private boolean isPrepared;
private boolean isFirstVisible = true;
private boolean isFirstInvisible = true;
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
initPrepare();
}
private synchronized void initPrepare() {
if (isPrepared) {
onFirstUserVisible();
} else {
isPrepared = true;
}
}
@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);
if (isVisibleToUser) {
if (isFirstVisible) {
isFirstVisible = false;
initPrepare();
} else {
onUserVisible();
}
} else {
if (isFirstInvisible) {
isFirstInvisible = false;
onFirstUserInvisible();
} else {
onUserInvisible();
}
}
}
protected abstract void onFirstUserVisible();//加载数据,开启动画/广播..
protected void onUserVisible(){}///开启动画/广播..
private void onFirstUserInvisible() {
}
protected void onUserInvisible(){}//暂停动画,暂停广播
}
注意:fragment中的onViewCreated每次切换都会调用,在懒加载的时候如果在该方法中初始化控件,初始化数据,网络请求等操作就起不到懒加载的效果
总结:fragment的封装基本可以按照上面的方式使用,当然那是最基本的,需要根据自己的需要增加。如果有什么不当之处欢迎指出。
网友评论