android 应用架构随笔三(ListView)
import java.util.ArrayList; import java.util.List; import com.heima.googleplay.holder.BaseHolder; import com.heima.googleplay.holder.MoreHolder; import com.heima.googleplay.manager.ThreadManager; import com.heima.googleplay.utils.UIUtils; import android.view.View; import android.view.ViewGroup; import android.widget.AbsListView.RecyclerListener; import android.widget.BaseAdapter; import android.widget.ListView; public abstract class MyBaseAdapter<T> extends BaseAdapter implements RecyclerListener { public ListView mListView; public MyBaseAdapter(ListView mListView, List<T> mDatas) { this.mListView = mListView; // 初始化一个界面可见的holder mDisplayHolderLists = new ArrayList<BaseHolder>(); if (null != mListView) { // 设置listview的回收监听 mListView.setRecyclerListener(this); } setData(mDatas); } public List<T> mDatas; private BaseHolder holder; private List<BaseHolder> mDisplayHolderLists; public void setData(List<T> mDatas) { this.mDatas = mDatas; } public List<T> getData() { return mDatas; } @Override public int getCount() { // +1 表示添加一个特殊的条目 return mDatas.size() + 1; } @Override public Object getItem(int position) { // TODO Auto-generated method stub return mDatas.get(position); } @Override public long getItemId(int position) { // TODO Auto-generated method stub return position; } // 表示加载更多 private final int MORE_ITEM_TYPE = 0; // 表示普通的数据类型 private final int ITEM_VIEW_TYPE = 1; private MoreHolder moreHolder; /** * 获取到item的类型 * * @param position * @return */ @Override public int getItemViewType(int position) { // 判断当前是否是最后一个条目 if (position == getCount() - 1) { return MORE_ITEM_TYPE; } else { return getInnerItemViewType(position); } } public int getInnerItemViewType(int position) { // TODO Auto-generated method stub return ITEM_VIEW_TYPE; } /** * 获取到一共有多少中数据类型 * * @return */ @Override public int getViewTypeCount() { return super.getViewTypeCount() + 1; } @Override public View getView(int position, View convertView, ViewGroup parent) { if (convertView != null) { holder = (BaseHolder) convertView.getTag(); } else { if (getItemViewType(position) == MORE_ITEM_TYPE) { holder = getMoreHolder(); } else { holder = getHolder(); } } if (getItemViewType(position) == ITEM_VIEW_TYPE) { holder.setData(mDatas.get(position)); } // 把所有的holder全部丢入mDisplayHolderLists mDisplayHolderLists.add(holder); return holder.getRootView(); } /** * 获取更多的holder * @return */ private BaseHolder getMoreHolder() { if(moreHolder == null){ moreHolder = new MoreHolder(hasMore(),this); } return moreHolder; } /** * 表示有更多的数据 * @return */ public boolean hasMore() { // TODO Auto-generated method stub return true; } public abstract BaseHolder getHolder(); /** * 当listview回收的时候调用当前的方法 */ @Override public void onMovedToScrapHeap(View view) { System.out.println("我被回收了....."); if (null != view) { BaseHolder holder = (BaseHolder) view.getTag(); if (null != holder) { synchronized (mDisplayHolderLists) { mDisplayHolderLists.remove(holder); } } } } private boolean isLoading = false; /** * 加载更多 */ public void loadMore() { if(!isLoading){ isLoading = true; ThreadManager.getLongPool().execute(new Runnable() { @Override public void run() { final List list = onLoadMore(); UIUtils.runInMainThread(new Runnable() { @Override public void run() { if(null == list){ //如果服务器返回的数据等于null。那么设置一个错误状态 getMoreHolder().setData(MoreHolder.ERROR); }else if(list.size() < 20){ //如果服务器返回的数据小于20条。那么设置没有更多数据的状态 getMoreHolder().setData(MoreHolder.NO_MORE); }else{ getMoreHolder().setData(MoreHolder.HAS_MORE); } if(null != list){ if(null !=mDatas){ mDatas.addAll(list); }else{ setData(list); } } //刷新界面 notifyDataSetChanged(); isLoading = false; } }); } }); } } protected abstract List onLoadMore(); }
package com.heima.googleplay.widget; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import com.heima.googleplay.R; import com.heima.googleplay.manager.ThreadManager; import com.heima.googleplay.utils.UIUtils; import android.content.Context; import android.util.AttributeSet; import android.view.View; import android.widget.FrameLayout; /** * * * ============================================================ **/ public abstract class LoadingPage extends FrameLayout { //默认状态 private final int UN_LOADING = 1; //加载状态 private final int LOADING = 2; //加载失败状态 private final int ERROR = 3; //加载成功。然后服务器没有返回数据 private final int EMPTY = 4; //加载成功的状态 private final int SUCCESS = 5; //用来记录某种状态 private int mState; private View mLoadingView; private View mErrorView; private View mEmptyView; private View mSuccessView; public LoadingPage(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); init(); } private void init() { //首先赋值 mState = UN_LOADING; mLoadingView = createLoadingView(); if(null != mLoadingView){ addView(mLoadingView, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)); } mErrorView = createErrorView(); if(null != mErrorView){ addView(mErrorView,new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)); } mEmptyView = createEmptyView(); if(null != mEmptyView ){ addView(mEmptyView, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)); } showSafePage(); } private void showSafePage() { UIUtils.runInMainThread(new Runnable() { @Override public void run() { showPage(); } }); } protected void showPage() { if(null != mLoadingView){ mLoadingView.setVisibility(mState == UN_LOADING || mState == LOADING ? View.VISIBLE : View.INVISIBLE); } if(null != mErrorView){ mErrorView.setVisibility(mState == ERROR ? View.VISIBLE : View.INVISIBLE); } if(null != mEmptyView){ mEmptyView.setVisibility(mState == EMPTY ? View.VISIBLE : View.INVISIBLE); } if(null == mSuccessView && mState == SUCCESS ){ mSuccessView = createSuccessView(); addView(mSuccessView, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)); } if(null != mSuccessView){ mSuccessView.setVisibility(mState == SUCCESS ? View.VISIBLE : View.INVISIBLE); } } public abstract View createSuccessView(); private View createEmptyView() { // TODO Auto-generated method stub return UIUtils.inflate(R.layout.loading_page_empty); } private View createErrorView() { // TODO Auto-generated method stub return UIUtils.inflate(R.layout.loading_page_error); } private View createLoadingView() { // TODO Auto-generated method stub return UIUtils.inflate(R.layout.loading_page_loading); } public LoadingPage(Context context, AttributeSet attrs) { super(context, attrs); init(); } public LoadingPage(Context context) { super(context); init(); } // //加载失败状态 // private final int ERROR = 3; // //加载成功。然后服务器没有返回数据 // private final int EMPTY = 4; // //加载成功的状态 // private final int SUCCESS = 5; public enum LoadResult{ ERROR(3),EMPTY(4),SUCCESS(5); int value ; LoadResult(int value){ this.value = value; } public int getValue() { return value; } } private class LoadTask implements Runnable{ @Override public void run() { final LoadResult result = Load(); UIUtils.runInMainThread(new Runnable() { @Override public void run() { mState = result.getValue(); showPage(); } }); } } public void show() { if(mState == ERROR || mState == EMPTY){ mState = UN_LOADING; } if(mState == UN_LOADING){ mState = LOADING; LoadTask task = new LoadTask(); ThreadManager.getLongPool().execute(task); // ExecutorService service = Executors.newFixedThreadPool(3); // LoadTask task = new LoadTask(); // service.execute(task); } showSafePage(); } public abstract LoadResult Load() ; }