BaseFragment
/**
* Created by HASEE.
*/
public abstract class BaseFragment extends Fragment{
private LoadingPage loadingPage;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
//加载失败view 加载为空view 加载中view 加载成功view
loadingPage = new LoadingPage(UIUitls.getContext()) {
@Override
public ResultState onLoad() {
//请求网络方法,而且请求结果必须是ResultState这个枚举中的3个对象之一
//url
//请求方式
//请求参数
//请求结果
return onSubLoad();
}
@Override
public View onCreateSuccessedView() {
//将请求成功后(首页,应用,游戏)布局文件转换成view对象方法
return onSubCreateSuccessedView();
}
};
return loadingPage;
}
public void getData(){
if (loadingPage!=null){
loadingPage.show();
}
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
getData();
super.onActivityCreated(savedInstanceState);
}
//(首页,应用,游戏) 请求过程有差异,所以此处无法统一处理,交由子类实现
public abstract LoadingPage.ResultState onSubLoad();
//(首页,应用,游戏)布局文件都有差异,所以在此处无法统一处理,交由子类实现
public abstract View onSubCreateSuccessedView() ;
}
LoadingPage
public abstract class LoadingPage extends FrameLayout {
//后续会添加在LoadingPage中的view对象
private View loadingView;
private View errorView;
private View emptyView;
private View successedView;
//请求网络过程中后请求完成后会出现的状态
public static final int STATE_NONE = 0;//初始状态
public static final int STATE_LOADING = 1;//请求过程中状态
public static final int STATE_ERROR = 2;//请求失败状态
public static final int STATE_EMPTY = 3;//请求为空状态
public static final int STATE_SUCCESSED = 4;//请求成功状态
//目前所处什么状态,用一个变量记录
private int current_state = STATE_NONE;
public LoadingPage(Context context) {
this(context,null);
}
public LoadingPage(Context context, AttributeSet attrs) {
this(context, attrs,-1);
}
public LoadingPage(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initView();
}
private void initView() {
//1.3种类型界面的添加过程
if (loadingView == null){
loadingView = UIUitls.inflate(R.layout.layout_loading);
addView(loadingView);
}
if (errorView == null){
errorView = UIUitls.inflate(R.layout.layout_error);
addView(errorView);
}
if (emptyView == null){
emptyView = UIUitls.inflate(R.layout.layout_empty);
addView(emptyView);
}
showPage();
}
//根据目前current_state所处的状态,决定展示界面内容
private void showPage() {
//对进度条的显示和隐藏通过状态码进行判断
if (loadingView!=null){
if (current_state == STATE_NONE || current_state == STATE_LOADING){
loadingView.setVisibility(View.VISIBLE);
}else{
loadingView.setVisibility(View.GONE);
}
}
//对加载失败的view对象显示隐藏通过状态码进行判断
if (errorView!=null){
errorView.setVisibility((current_state == STATE_ERROR)?View.VISIBLE:View.GONE);
}
//对加载为空的view对象显示隐藏通过状态码进行判断
if (emptyView!=null){
emptyView.setVisibility((current_state == STATE_EMPTY)?View.VISIBLE:View.GONE);
}
//请求成功
if (current_state == STATE_SUCCESSED){
//返回的就是加载数据成功后,布局文件转换成的view对象
successedView = onCreateSuccessedView();
if (successedView!=null){
addView(successedView);
successedView.setVisibility((current_state == STATE_SUCCESSED)?View.VISIBLE:View.GONE);
}
}
}
//网络请求触发方法,此方法用于获取请求的状态,此状态必须记录在current_state中,用于决定4个view中每一个view的显示隐藏
public void show(){
new Thread(){
@Override
public void run() {
//发送网络请求?
//首页 http://www.google.market/home.jsp?home=value
//应用 http://www.google.market/app.jsp?app=value
//游戏 http://www.google.market/game.jsp?game=value
final ResultState resultState = onLoad();
// resultState.state// 只能是2,3,4中的某一个状态码
//获取状态码最终的目的是,根据请求结果决定页面的显示view是那个
UIUitls.runOnMainThread(new Runnable() {
@Override
public void run() {
if (resultState!=null){
current_state = resultState.state;
showPage();
}
}
});
}
}.start();
}
//因为首页,应用,游戏.....所有的页面模块,在网络请求过程中,链接地址不同,参数不同,请求方式不同,解析json也不同
public abstract ResultState onLoad();//成功 失败 成功但是没有数据(为空) 枚举
//因为首页,应用,游戏在请求网络成功后,需要展示的页面效果完全不一样,所以不能在父类中统一写死布局文件,于是抽象
public abstract View onCreateSuccessedView() ;
//只能有 RESULT_STATE_ERROR RESULT_STATE_EMPTY RESULT_STATE_SUCCESSED三个对象,不能有其他情况
public enum ResultState{
//定义3个对象
RESULT_STATE_ERROR(2),
RESULT_STATE_EMPTY(3),
RESULT_STATE_SUCCESSED(4);
private int state;
private ResultState(int state){
this.state = state;
}
}
}
AppFragment
/**
* Created by HASEE.
*/
public class AppFragment extends BaseFragment {
@Override
public LoadingPage.ResultState onSubLoad() {
return LoadingPage.ResultState.RESULT_STATE_EMPTY;
}
@Override
public View onSubCreateSuccessedView() {
return null;
}
}
MainActivity
public class MainActivity extends AppCompatActivity {
@BindView(R.id.tabLayout)
TabLayout tabLayout;
@BindView(R.id.viewpager)
ViewPager viewpager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
//1.给viewPager设置数据适配器
MyFragmentPagerAdapter myFragmentPagerAdapter
= new MyFragmentPagerAdapter(getSupportFragmentManager());
viewpager.setAdapter(myFragmentPagerAdapter);
//2.让tabLayout和viewPager绑定
tabLayout.setupWithViewPager(viewpager);
}
class MyFragmentPagerAdapter extends FragmentPagerAdapter {
private String[] stringArray;
public MyFragmentPagerAdapter(FragmentManager fm) {
super(fm);
stringArray = UIUitls.getResource().getStringArray(R.array.tab_names);
}
@Override
public Fragment getItem(int position) {
//每一个索引位置的页面效果,Fragment产出工厂类
return FragmentFactory.createFragment(position);
}
@Override
public int getCount() {
//页面数量
return stringArray.length;
}
@Override
public CharSequence getPageTitle(int position) {
return stringArray[position];
}
}
}