在网上找了很久,都没有找到自己想要的结果,所以自己就写了一个,并把它放到了github上,喜欢的朋友可以去下载源码看下。先上代码
public abstract class LoadPager extends FrameLayout { public static final int STATE_UNKOWN = 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_SUCCESS = 4; public int state = STATE_UNKOWN; private View loadingView;// 加载中的界面 private View errorView;// 错误界面 private View emptyView;// 空界面 private View successView;// 加载成功的界面 private Context mContext; public LoadPager(Context context) { super(context); mContext = context; init(); } public LoadPager(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); mContext = context; init(); } public LoadPager(Context context, AttributeSet attrs) { super(context, attrs); mContext = context; init(); } private void init() { loadingView = createLoadingView(); // 创建了加载中的界面 if (loadingView != null) { this.addView(loadingView, new FrameLayout.LayoutParams( LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)); } errorView = createErrorView(); // 加载错误界面 if (errorView != null) { this.addView(errorView, new FrameLayout.LayoutParams( LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)); } emptyView = createEmptyView(); // 加载空的界面 if (emptyView != null) { this.addView(emptyView, new FrameLayout.LayoutParams( LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)); } showPage(STATE_LOADING);// 根据不同的状态显示不同的界面 } // 根据不同的状态显示不同的界面 public void showPage(int result) { state = result; if (loadingView != null) { loadingView.setVisibility(state == STATE_UNKOWN || state == STATE_LOADING ? View.VISIBLE : View.INVISIBLE); } if (errorView != null) { errorView.setVisibility(state == STATE_ERROR ? View.VISIBLE : View.INVISIBLE); } if (emptyView != null) { emptyView.setVisibility(state == STATE_EMPTY ? View.VISIBLE : View.INVISIBLE); } if (state == STATE_SUCCESS) { if (successView == null) { successView = createSuccessView(); this.addView(successView, new FrameLayout.LayoutParams( LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)); } successView.setVisibility(View.VISIBLE); } else { if (successView != null) { successView.setVisibility(View.INVISIBLE); } } } /* 创建了空的界面 */ private View createEmptyView() { return View.inflate(mContext, R.layout.loadpage_empty, null); } /* 创建了错误界面 */ private View createErrorView() { View view = View.inflate(mContext, R.layout.loadpage_error, null); Button page_bt = (Button) view.findViewById(R.id.page_bt); page_bt.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { show(); } }); return view; } /* 创建加载中的界面 */ private View createLoadingView() { return View.inflate(mContext, R.layout.loadpage_loading, null); } // 根据服务器的数据 切换状态 public void show() { if (state == STATE_ERROR || state == STATE_EMPTY) { state = STATE_LOADING; } load(); showPage(state); } /*** * 创建成功的界面 */ public abstract View createSuccessView(); /** * 请求服务器 * 必须在请求完成后调用showPage方法 */ protected abstract void load(); }
上面的注释都写的很清楚啦,如果对此类还不了解的朋友请在下面留言,看到后我将会回复你的。
下面显示如何在基类中使用LoadPager,使得以后我们少写很多的代码。
<pre name="code" class="java">public abstract class BaseFragment extends Fragment{ protected LoadPager mLoadPager = null; protected Context mContext = getActivity() == null ? APP.getInstance() : getActivity(); protected ViewGroup mContainer; protected LayoutInflater mInflater; @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { mContainer = container; mInflater = inflater; if (mLoadPager == null){ mLoadPager = new LoadPager(mContext) { @Override public View createSuccessView() { return BaseFragment.this.createSuccessView(); } @Override protected void load() { BaseFragment.this.loadData(); } }; } return mLoadPager; } @Override public void onActivityCreated(@Nullable Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); show(); } public void show(){ if(mLoadPager!=null){ mLoadPager.show(); } } /** * 必须在获取到数据后调用checkData方法 * */ protected abstract void loadData(); protected abstract View createSuccessView(); /**校验数据 */ protected void checkData(Object datas) { if(datas==null){ mLoadPager.showPage(LoadPager.STATE_ERROR);// 请求服务器失败 }else{ try { @SuppressWarnings("unchecked") List<Object> ds = (List<Object>) datas; if(ds.size()==0){ mLoadPager.showPage(LoadPager.STATE_EMPTY); }else{ mLoadPager.showPage(LoadPager.STATE_SUCCESS); } } catch (Exception e) { e.printStackTrace(); if("".equals(datas)){ mLoadPager.showPage(LoadPager.STATE_EMPTY); }else{ mLoadPager.showPage(LoadPager.STATE_SUCCESS); } } } } }
下面来看看我们在子类中如何使用
</pre><pre code_snippet_id="1677905" snippet_file_name="blog_20160510_8_2136022" name="code" class="java"><pre name="code" class="java">public class ErrorFragment extends BaseFragment { private String str = "加载成功啦"; @Override protected void loadData() { new Thread(new Runnable() { @Override public void run() { try { Thread.sleep(3000); APP.getMainThreadHandler().post(new Runnable() { @Override public void run() { checkData(null); } }); } catch (InterruptedException e) { e.printStackTrace(); } } }).start(); } @Override protected View createSuccessView() { TextView tv = new TextView(mContext); tv.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT)); tv.setText(str); return tv; } }
这里使用线程是为了模拟在网络中请求数据的耗时操作。
好了,基本就是这样了,如果您代码不够好,有什么好的想法可以给我留言或者加我QQ347402035,请填写理由,否则可能会添加失败。
时间: 2024-10-02 21:38:40