SwipeRefreshLayout 实现下拉刷新

来源:互联网 发布:skype linux 编辑:程序博客网 时间:2024/05/16 11:54



Google发布的V4包中有一个SwipeRefreshLayout控件,可以实现下拉刷新数据的效果。该类可以作为一个布局文件的根布局,作为一个容器类,至少需要一个子视图,结合ListView使用;


使用这个类的时候我碰到如下的问题,和一些体会,希望对你有所帮助:

1、单把SwipeRefreshLayout作为跟视图的时候,如果子视图为TextView就无法显示下拉效果。

2、当子视图多余一个的时候,也不会报错,但如果第一个子视图为ListView就会报错。

3、swipeRefreshLayout.setColorSchemeResources(),这个方法里面的颜色如果用Color类作为参数就会报错,如果用android.R.color.XXX无法正常显示颜色,解决办法是在values文件夹下新建一个colors.xml文件,里面定义颜色,然后参数传R.color.XXX这样就可以正常显示了。



布局文件activity_main.xml:

<android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:id="@+id/refresh"    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:divider="#f00"        android:dividerHeight="1dp" /></android.support.v4.widget.SwipeRefreshLayout>


核心代码:

public class MainActivity extends Activity {private SwipeRefreshLayout swipeRefreshLayout;private String url = "http://japi.juhe.cn/joke/content/text.from?key=998baa9d677a41ecefffd1607d2bee4c&page=1&pagesize=10";private ArrayList<String> dataList;private ArrayAdapter<String> adapter;private Handler handler = new Handler() {public void handleMessage(android.os.Message msg) {if (msg.arg1 == 1) {// 一定要用这个方法通知数据改变runOnUiThread(new Runnable() {@Overridepublic void run() {adapter.notifyDataSetChanged();}});}};};@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);final ListView listView = (ListView) findViewById(R.id.listView);dataList = new ArrayList<String>();adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, dataList);listView.setAdapter(adapter);swipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.refresh);// 该方法参数为可变参数,至少放入一种颜色,刷新的时候会依次显示各种颜色swipeRefreshLayout.setColorSchemeResources(R.color.red, R.color.green, R.color.blue);// 刷新监听swipeRefreshLayout.setOnRefreshListener(new OnRefreshListener() {@Overridepublic void onRefresh() {//下载数据loadData();swipeRefreshLayout.setRefreshing(false);}});}// 下载数据private void loadData() {// 开一个线程下载数据new Thread(new Runnable() {@Overridepublic void run() {try {// 将百度首页显示在TextView上面HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection();// 各种设置conn.setReadTimeout(5 * 1000);conn.setConnectTimeout(5 * 1000);conn.setRequestMethod("GET");// 获取返回的数据InputStream is = conn.getInputStream();// 将流转化为字符串显示在屏幕上final ByteArrayOutputStream bos = new ByteArrayOutputStream();byte[] buf = new byte[1024];while (is.read(buf) != -1) {bos.write(buf);bos.flush();bos.close();}// 解析数据org.json.JSONObject obj;try {obj = new JSONObject(bos.toString());// 使用Gson去解析json数据JSONArray arr = obj.getJSONObject("result").getJSONArray("data");for (int i = 0; i < arr.length(); i++) {JSONObject obj1 = arr.getJSONObject(i);DataBeam data = new Gson().fromJson(obj1.toString(), DataBeam.class);dataList.add(data.getContent());}// 发消息通知数据下载完成Message msg = Message.obtain();msg.arg1 = 1;handler.handleMessage(msg);} catch (JSONException e) {e.printStackTrace();}} catch (MalformedURLException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}}}).start();}}

扫描关注我的微信公众号:


Demo下载:下载


1 0