android控件SwipeRefreshLayout实现下拉刷新

来源:互联网 发布:西南seo大神 编辑:程序博客网 时间:2024/05/16 15:55

新手写博文,写的不好,还请见谅。

Google为我们推出的控件可以简单的实现下拉刷新的功能,但是这里要注意的是,我们需要引入v4的jar包,并且在xml文件引用SwipeRefreshLayout。
1.xml文件引用SwipeRefreshLayout,代码如下

        <android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"        android:id="@+id/swipe_container"        android:layout_width="match_parent"        android:layout_height="match_parent" >        <ListView            android:id="@+id/listview"            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:padding="16dp" />    </android.support.v4.widget.SwipeRefreshLayout>    2.Java代码实现    package com.alpha.swiperefresh;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.widget.SwipeRefreshLayout;
import android.widget.ListView;
import android.widget.SimpleAdapter;

/**
* SwipeRefreshLayout实现下拉刷新
*
* @author Simon
*
*/
public class MainActivity extends Activity implements
SwipeRefreshLayout.OnRefreshListener {
private ListView listView;
private SimpleAdapter simpleAdapter;
private SwipeRefreshLayout swipeRefreshLayout;

@SuppressWarnings("deprecation")@Overrideprotected void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.activity_main);    swipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swiperefresh);    swipeRefreshLayout.setOnRefreshListener(this);    // 顶部刷新的样式    swipeRefreshLayout.setColorScheme(android.R.color.holo_red_light,            android.R.color.holo_green_light,            android.R.color.holo_blue_bright,            android.R.color.holo_orange_light);    listView = (ListView) findViewById(R.id.listview1);    listView.setAdapter(new SimpleAdapter(this, getData(),            R.layout.listview,            new String[] { "images", "title", "content" }, new int[] {                    R.id.imageview1, R.id.textview1, R.id.textview2 }));}/** * 用集合适配数据 *  * @return */private List<Map<String, Object>> getData() {    Map<String, Object> map = new HashMap<>();    List<Map<String, Object>> list = new ArrayList<>();    for (int i = 0; i < 20; i++) {        map.put("images", R.drawable.voice);        map.put("title", "標題");        map.put("content", "內容");        list.add(map);    }    return list;}@Overridepublic void onRefresh() {    // TODO Auto-generated method stub    new Handler().postDelayed(new Runnable() {        @Override        public void run() {            // TODO Auto-generated method stub            swipeRefreshLayout.setRefreshing(false);        }    }, 3000);}

}

0 0
原创粉丝点击