編輯:關於Android編程
原理就是通過對FooterView的控制實現上劃加載的效果
public class LoadMoreListView extends ListView { private View mLoadMoreFooter; private OnLoadMoreListener mLoadMoreListener; private boolean mAddedFooter; private boolean mLoadMore; public interface OnLoadMoreListener extends OnScrollListener { // 加載更多數據 void onLoadMoreData(); } public LoadMoreListView(Context context) { super(context); init(); } public LoadMoreListView(Context context, AttributeSet attrs) { super(context, attrs); init(); } public LoadMoreListView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); init(); } private void init() { mLoadMoreFooter = LayoutInflater.from(getContext()).inflate(R.layout.load_more_view, null); this.setOnScrollListener(new OnScrollListener() { @Override public void onScrollStateChanged(AbsListView view, int scrollState) { if (mLoadMoreListener != null) { mLoadMoreListener.onScrollStateChanged(view, scrollState); } } @Override public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) { if (mLoadMoreListener != null) { mLoadMoreListener.onScroll(view, firstVisibleItem, visibleItemCount, totalItemCount); // 滑動到底部顯示LoadingMore if (firstVisibleItem + visibleItemCount == totalItemCount && mLoadMore) { showLoadMoreView(); // 加載耗時操作 post(new Runnable() { @Override public void run() { mLoadMoreListener.onLoadMoreData(); mLoadMore = false; } }); } } } }); } // 加載數據完成 public void loadMoreComplete() { deferNotifyDataSetChanged(); setLoadMore(false); } public void setLoadMore(boolean loadMore) { mLoadMore = loadMore; if (!loadMore) { removeFooterView(mLoadMoreFooter); } } private void showLoadMoreView() { if (!mAddedFooter) { addFooterView(mLoadMoreFooter); mAddedFooter = true; } } public void setLoadMoreListener(OnLoadMoreListener loadMoreListener) { this.mLoadMoreListener = loadMoreListener; } }
public class MainActivity extends AppCompatActivity { private static final String TAG = "MyListView"; private LoadMoreListView mLvList; private ListmData = new ArrayList<>(); private Handler mHandler = new Handler(); { for (int i = 0; i < 50; i++) { mData.add("num " + i); } } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mLvList = (LoadMoreListView) findViewById(R.id.id_lv_list); final ArrayAdapter adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, mData); mLvList.setAdapter(adapter); mLvList.setLoadMore(true); mLvList.setLoadMoreListener(new LoadMoreListView.OnLoadMoreListener() { @Override public void onLoadMoreData() { // 模擬一個異步操作,阻塞3秒 new Thread() { @Override public void run() { try { Thread.sleep(3000); } catch (InterruptedException e) { e.printStackTrace(); } mData.add("LoadMoreOne"); mData.add("LoadMoreTwo"); mHandler.post(new Runnable() { @Override public void run() { // adapter.notifyDataSetChanged(); mLvList.loadMoreComplete(); } }); } }.start(); } @Override public void onScrollStateChanged(AbsListView view, int scrollState) { } @Override public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) { } }); } }
本文來自http://blog.csdn.net/yihongyuelan 轉載請務必注明出處 本文代碼以MTK平台Android 4.4為分析對象,與Google原
初看這個博文名,我都蒙蔽了,Activity的啟動模式居然能扯到內存問題,還有內存洩漏問題,WTF!!!不要方,小司機我帶你理解和稍微深入的探討一下Activity的四種
內容概要示例演示和基本介紹啟用Action Bar在Action Bar上添加按鈕自定義Action Bar樣式自動隱藏Action BarAction Provider
有段時間沒寫博客了,可能寫博客的時間都用去把妹了吧。說到把妹,偶爾也會在無意間了解一些把妹的技巧,比如雲擒故縱啊什麼的。可能這些技巧前期會有一定的作用,可能會讓妹子對你產