標籤:
做過開發的對於下滑重新整理與上滑載入都一定不陌生,因為我們在很多時候都會使用到,那對對於這個效果如何?呢?相信難道過很多小夥伴,今天我就帶領大家一道通過第三方組件快速完成上述效果的實現,保准每位小夥伴都能快速上手。首先為大家分享一下:
下滑重新整理:
上滑載入:
第三方資源:https://github.com/Maxwin-z/XListView-Android,下載好資源後,我們開始進行設計實現:
我們把下載好的包解壓好,然後把包裡面的一下六個檔案拷貝到工程中六個檔案分別為:
我們的Activity:
public class MainActivity extends Activity implements IXListViewListener{ private XListView mListView; private ArrayAdapter<String> mAdapter; private ArrayList<String> items = new ArrayList<String>(); private Handler mHandler; private int start = 0;//資料起始位置 private static int refreshCnt = 0;//第幾次重新整理 private Date date = new Date(); private SimpleDateFormat format = new SimpleDateFormat("yyyy年MM月dd日HH:mm:ss");// 設定更新時間的格式 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); geneItems(); mListView = (XListView) findViewById(R.id.xListView); mListView.setPullLoadEnable(true);//設定下拉重新整理// mListView.setPullLoadEnable(false);//設定不能進行載入更多操作// mListView.setPullRefreshEnable(false);//設定不能進行重新整理操作 mAdapter = new ArrayAdapter<String>(this, R.layout.xlist_item, items); mListView.setAdapter(mAdapter); mListView.setRefreshTime(format.format(date));// 設定更新時間 mListView.setXListViewListener(this); mHandler = new Handler(); } private void geneItems() { for (int i = 0; i != 20; ++i) { items.add("refresh cnt " + (++start)); } } private void onLoad() { mListView.stopRefresh(); mListView.stopLoadMore(); date = new Date(); mListView.setRefreshTime(format.format(date));// 設定更新時間 } //下滑重新整理 @Override public void onRefresh() { mHandler.postDelayed(new Runnable() { @Override public void run() { start = ++refreshCnt; items.clear(); MainActivity.this.geneItems(); mAdapter.notifyDataSetChanged(); MainActivity.this.onLoad(); } }, 2000); } //上滑載入 @Override public void onLoadMore() { mHandler.postDelayed(new Runnable() { @Override public void run() { MainActivity.this.geneItems(); mAdapter.notifyDataSetChanged(); MainActivity.this.onLoad(); } }, 2000); } }
主布局檔案action_main.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" android:background="#f0f0f0"> <me.maxwin.view.XListView android:id="@+id/xListView" android:layout_width="fill_parent" android:layout_height="fill_parent" android:cacheColorHint="#00000000"> </me.maxwin.view.XListView></LinearLayout>
好了我們的工程已經完成,大家可以運行操作一下。
Android實現下滑和上滑事件