swipeRefreshLayout的使用
swipeRefreshLayout是google加的一个下拉控件,放在 android-support-v4.jar包里。给listView加下拉的步骤:
首先是布局:
<android.support.v4.widget.SwipeRefreshLayout
android:id="@+id/refresh_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ListView
android:id="@+id/listview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:cacheColorHint="@android:color/transparent" >
</ListView>
</android.support.v4.widget.SwipeRefreshLayout>
然后
是activity文件:
implements SwipeRefreshLayout.OnRefreshListener
private void initView() { adapter = new RecordAdapter(list, getApplicationContext()); listView.setAdapter(adapter); swipeLayout.setColorSchemeResources(R.color.back_global_light); swipeLayout.setOnRefreshListener(this); swipeLayout.setOnLoadListener(this); }
@Override public void onRefresh() { swipeLayout.postDelayed(new Runnable() { @Override public void run() { handler.sendEmptyMessage(0); } }, 1500); } private MyHandler handler = new MyHandler(); class MyHandler extends Handler { @Override public void handleMessage(Message msg) { switch (msg.what) { case 0: if (NetWorkHelper.isConnected(getActivity())) { page = 0; list.clear(); lunBoBeanList.clear(); lunBo_Url_List.clear(); initData(); lunBoinitData(); } else { if (jsonString == null || "".equals(jsonString) || lunBojsonString == null || "".equals(lunBojsonString)) { showToast(getString(R.string.cache_no_network)); } else { getJSon(jsonString); getLunBoJSon(lunBojsonString); } } swipeLayout.setRefreshing(false); adapter.notifyDataSetChanged(); break; default: break; } }根据具体作调整,比较简单。