下拉刷新初体验之二 SwipeRefreshLayout

GoogleStyle谷歌风格的下拉刷新控件也是谷歌官方出品,要求你的库必须跟新到19.1以上,谷歌现在被封了,没有更新的亲蛋疼了

主要使用就是设置监听setOnRefreshListener,设置颜色条setColorScheme,然后刷新的方法里要用Handler延迟下

参考过的文章:http://www.apkbus.com/forum.php?mod=viewthread&tid=166418

主要代码:

1
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mSwipeRefreshLayout = (SwipeRefreshLayout) this.findViewById(R.id.swiperefresh);
mSwipeRefreshLayout.setOnRefreshListener(this);
mSwipeRefreshLayout.setColorScheme(android.R.color.holo_green_light, android.R.color.holo_orange_light,
android.R.color.holo_red_light, android.R.color.holo_purple);
mItemLists = new ArrayList<ItemList>();
ItemList itemsource = new ItemList();
itemsource.setName("lixinyangblog.com");
mItemLists.add(itemsource);
mListView = (ListView) this.findViewById(R.id.listview);
mAdapter = new ListViewAdapter(this, mItemLists);
mListView.setAdapter(mAdapter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public void onRefresh() {
new Handler().postDelayed(new Runnable() {
public void run() {
mSwipeRefreshLayout.setRefreshing(false);
ItemList newdata = new ItemList();
newdata.setName("new data"+new Date());
mItemLists.add(newdata);
mAdapter.notifyDataSetChanged();
}
}, 1000);
}
// TODO Auto-generated method stub
}

布局文件:

1
<android.support.v4.widget.SwipeRefreshLayout
android:id=”@+id/swiperefresh”
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” >
</ListView>
</android.support.v4.widget.SwipeRefreshLayout>

放两张效果图

image
image