新聞中心

        EEPW首頁 > 嵌入式系統(tǒng) > 設(shè)計(jì)應(yīng)用 > Android開發(fā)之SwipeRefreshLayout實(shí)現(xiàn)下拉刷新

        Android開發(fā)之SwipeRefreshLayout實(shí)現(xiàn)下拉刷新

        作者: 時(shí)間:2016-09-12 來源:網(wǎng)絡(luò) 收藏

        簡(jiǎn)介

        本文引用地址:http://www.104case.com/article/201609/303423.htm

        SwipeRefreshLayout是Google官方推出的一款下拉刷新組件,位于v4兼容包下,android.support.v4.widget.SwipeRefreshLayout,Support Library 必須19.1以上。使用起來很簡(jiǎn)單,只要在需要刷新的控件最外層加上SwipeRefreshLayout,其child必須是可滾動(dòng)的view,如ScrollView、GridView或者ListView,這里就測(cè)試最常用的ListView。

        布局

        xmlns:tools=http://schemas.android.com/tools

        android:layout_width=match_parent

        android:layout_height=match_parent

        android:paddingBottom=@dimen/activity_vertical_margin

        android:paddingLeft=@dimen/activity_horizontal_margin

        android:paddingRight=@dimen/activity_horizontal_margin

        android:paddingTop=@dimen/activity_vertical_margin

        tools:context=.MainActivity>

        android:id=@+id/swipeLayout

        android:layout_width=match_parent

        android:layout_height=match_parent>

        android:id=@+id/listview

        android:layout_width=match_parent

        android:layout_height=match_parent>

        Activity

        public class MainActivity extends AppCompatActivity implements SwipeRefreshLayout.OnRefreshListener {

        private SwipeRefreshLayout mSwipeLayout;

        private ListView mListView;

        private ArrayAdapter mAdapter;

        private ArrayList data;

        private boolean isRefresh = false;//是否刷新中

        @Override

        protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        init();

        //設(shè)置SwipeRefreshLayout

        mSwipeLayout = (SwipeRefreshLayout) findViewById(R.id.swipeLayout);

        //設(shè)置進(jìn)度條的顏色主題,最多能設(shè)置四種 加載顏色是循環(huán)播放的,只要沒有完成刷新就會(huì)一直循環(huán),holo_blue_bright>holo_green_light>holo_orange_light>holo_red_light

        // mSwipeLayout.setColorScheme(android.R.color.holo_blue_bright,

        // android.R.color.holo_green_light,

        // android.R.color.holo_orange_light,

        // android.R.color.holo_red_light);

        //上面的方法已經(jīng)廢棄

        mSwipeLayout.setColorSchemeColors(Color.BLUE,

        Color.GREEN,

        Color.YELLOW,

        Color.RED);

        // 設(shè)置手指在屏幕下拉多少距離會(huì)觸發(fā)下拉刷新

        mSwipeLayout.setDistanceToTriggerSync(300);

        // 設(shè)定下拉圓圈的背景

        mSwipeLayout.setProgressBackgroundColorSchemeColor(Color.WHITE);

        // 設(shè)置圓圈的大小

        mSwipeLayout.setSize(SwipeRefreshLayout.LARGE);

        //設(shè)置下拉刷新的監(jiān)聽

        mSwipeLayout.setOnRefreshListener(this);

        }

        /*

        * 初始化 設(shè)置ListView

        */

        private void init() {

        mListView = (ListView) findViewById(R.id.listview);

        data = new ArrayList();

        for (int i = 1; i 10; i++) {

        data.add(這是第 + i + 個(gè)數(shù)據(jù));

        }

        //初始化adapter

        mAdapter = new ArrayAdapter(MainActivity.this, android.R.layout.simple_list_item_1, data);

        mListView.setAdapter(mAdapter);

        }

        /*

        * 監(jiān)聽器SwipeRefreshLayout.OnRefreshListener中的方法,當(dāng)下拉刷新后觸發(fā)

        */

        public void onRefresh() {

        //檢查是否處于刷新狀態(tài)

        if (!isRefresh) {

        isRefresh = true;

        //模擬加載網(wǎng)絡(luò)數(shù)據(jù),這里設(shè)置4秒,正好能看到4色進(jìn)度條

        new Handler().postDelayed(new Runnable() {

        public void run() {

        //顯示或隱藏刷新進(jìn)度條

        mSwipeLayout.setRefreshing(false);

        //修改adapter的數(shù)據(jù)

        data.add(這是新添加的數(shù)據(jù));

        mAdapter.notifyDataSetChanged();

        isRefresh = false;

        }

        }, 4000);

        }

        }

        }

        問題

        細(xì)心的讀者肯定發(fā)現(xiàn),代碼中 setColorSchemeColors 設(shè)置顏色時(shí)候并未按如下設(shè)置

        mSwipeLayout.setColorSchemeColors(android.R.color.holo_blue_bright,

        android.R.color.holo_green_light,

        android.R.color.holo_orange_light,

        android.R.color.holo_red_light);

        那是因?yàn)椋?jīng)過測(cè)試,如果按照如上設(shè)置顏色,進(jìn)度條不會(huì)有顏色循環(huán)的效果,不知道為何?難道是bug嗎?



        關(guān)鍵詞: Android

        評(píng)論


        相關(guān)推薦

        技術(shù)專區(qū)

        關(guān)閉
        主站蜘蛛池模板: 长乐市| 合肥市| 得荣县| 永州市| 仪征市| 新干县| 庆城县| 长海县| 建湖县| 邛崃市| 都安| 金昌市| 桓台县| 乐安县| 清远市| 尉氏县| 临漳县| 吴忠市| 云浮市| 上林县| 讷河市| 黔南| 会同县| 土默特左旗| 和林格尔县| 右玉县| 五指山市| 德江县| 西畴县| 额尔古纳市| 岳普湖县| 金溪县| 高台县| 大连市| 驻马店市| 临泉县| 师宗县| 莆田市| 嘉兴市| 安远县| 云霄县|