Android開發(fā)之SwipeRefreshLayout實(shí)現(xiàn)下拉刷新
簡(jiǎn)介
本文引用地址:http://2s4d.com/article/201609/303423.htmSwipeRefreshLayout是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嗎?
評(píng)論