谷歌自己的下拉刷新组件解析
扫描二维码
随时随地手机看文章
SwipeRefreshLayout
public class SwipeRefreshLayout
extends ViewGroup implements NestedScrollingParent, NestedScrollingChild
java.lang.Object
↳ android.view.View
↳ android.view.ViewGroup
↳ android.support.v4.widget.SwipeRefreshLayout
API doc:http://developer.android.com/reference/android/support/v4/widget/SwipeRefreshLayout.html
主要方法
setOnRefreshListener(OnRefreshListener): 为布局添加一个Listener
setRefreshing(boolean): 显示或隐藏刷新进度条
isRefreshing(): 检查是否处于刷新状态
setColorScheme(): 设置进度条的颜色主题,最多能设置四种
布局:在最外层加上SwipeRefreshLayout,他的child是可滚动的view即可,如ScrollView或者ListView
在代码中使用:
public class MainActivity extends ActionBarActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_main); final TextView mTextView = (TextView)findViewById(R.id.refresh); final SwipeRefreshLayout mSwipeRefreshLayout = (SwipeRefreshLayout)findViewById(R.id.swipe_refresh_layout); mSwipeRefreshLayout.setColorSchemeResources(android.R.color.holo_blue_bright, android.R.color.holo_blue_light, android.R.color.holo_green_light, android.R.color.holo_orange_light); mSwipeRefreshLayout.setOnRefreshListener(new OnRefreshListener(){ @Override public void onRefresh() { mTextView.setText("正在刷新"); // TODO Auto-generated method stub //boolean android.os.Handler.postDelayed(Runnable r, long delayMillis) new Handler().postDelayed(new Runnable() { @Override public void run() { // TODO Auto-generated method stub mTextView.setText("刷新完成"); mSwipeRefreshLayout.setRefreshing(false); } }, 5000); } }); } }
Tanks to:stormzhang