新聞中心

        EEPW首頁 > 嵌入式系統 > 設計應用 > Android開發之SwipeRefreshLayout實現下拉刷新

        Android開發之SwipeRefreshLayout實現下拉刷新

        作者: 時間:2016-09-12 來源:網絡 收藏

        簡介

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

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

        //設置SwipeRefreshLayout

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

        //設置進度條的顏色主題,最多能設置四種 加載顏色是循環播放的,只要沒有完成刷新就會一直循環,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);

        //上面的方法已經廢棄

        mSwipeLayout.setColorSchemeColors(Color.BLUE,

        Color.GREEN,

        Color.YELLOW,

        Color.RED);

        // 設置手指在屏幕下拉多少距離會觸發下拉刷新

        mSwipeLayout.setDistanceToTriggerSync(300);

        // 設定下拉圓圈的背景

        mSwipeLayout.setProgressBackgroundColorSchemeColor(Color.WHITE);

        // 設置圓圈的大小

        mSwipeLayout.setSize(SwipeRefreshLayout.LARGE);

        //設置下拉刷新的監聽

        mSwipeLayout.setOnRefreshListener(this);

        }

        /*

        * 初始化 設置ListView

        */

        private void init() {

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

        data = new ArrayList();

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

        data.add(這是第 + i + 個數據);

        }

        //初始化adapter

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

        mListView.setAdapter(mAdapter);

        }

        /*

        * 監聽器SwipeRefreshLayout.OnRefreshListener中的方法,當下拉刷新后觸發

        */

        public void onRefresh() {

        //檢查是否處于刷新狀態

        if (!isRefresh) {

        isRefresh = true;

        //模擬加載網絡數據,這里設置4秒,正好能看到4色進度條

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

        public void run() {

        //顯示或隱藏刷新進度條

        mSwipeLayout.setRefreshing(false);

        //修改adapter的數據

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

        mAdapter.notifyDataSetChanged();

        isRefresh = false;

        }

        }, 4000);

        }

        }

        }

        問題

        細心的讀者肯定發現,代碼中 setColorSchemeColors 設置顏色時候并未按如下設置

        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);

        那是因為,經過測試,如果按照如上設置顏色,進度條不會有顏色循環的效果,不知道為何?難道是bug嗎?



        關鍵詞: Android

        評論


        相關推薦

        技術專區

        關閉
        主站蜘蛛池模板: 玉林市| 桃园县| 枞阳县| 勐海县| 佛山市| 建始县| 肥东县| 乌鲁木齐县| 佛教| 永康市| 水城县| 吴忠市| 澎湖县| 桐梓县| 兴安县| 屏东县| 晋宁县| 禄丰县| 县级市| 交城县| 台东县| 邻水| 盈江县| 荆门市| 阳泉市| 平谷区| 鹤山市| 丹阳市| 车致| 枣庄市| 镇宁| 平湖市| 丰都县| 扎赉特旗| 深水埗区| 竹溪县| 屏山县| 巴楚县| 西乌珠穆沁旗| 乌鲁木齐市| 同江市|