Android笔记
——各类控件的使用(控件)-滑动刷新布局
【若对该知识点有更多想了解的,欢迎私信博主~~】
滑动刷新布局:android.support.v4.widget.SwipeRefreshLayout
说明:抽屉布局可以实现侧滑菜单效果
属性:
XML属性 | 说明 |
---|---|
layout_width | 布局宽度 |
layout_height | 布局高度 |
方法:
方法 | 说明 |
---|---|
setSize() | 设置刷新圆圈大小(SwipeRefreshLayout.LARGE大;SwipeRefreshLayout.DEFAULT默认) |
setRefreshing() | 设置是否默认启动刷新(true 打开–无法下拉,flase 关闭) |
setColorSchemeResources() | 设置刷新圆圈颜色切换 |
setProgressBackgroundColorSchemeResource() | 设置刷新圆圈背景色 |
用法:
-
布局(SwipeRefreshLayout中必须有且仅有一个可滑动的控件,否则会报错)
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity"> <android.support.v4.widget.SwipeRefreshLayout android:id="@+id/srl" android:layout_width="match_parent" android:layout_height="match_parent"> <ListView android:layout_width="match_parent" android:layout_height="match_parent"> </ListView> </android.support.v4.widget.SwipeRefreshLayout> </LinearLayout>
-
在主代码中使用
SwipeRefreshLayout swipeRefreshLayout; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //绑定控件 swipeRefreshLayout=findViewById(R.id.srl); //下拉刷新 swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() { @Override public void onRefresh() { new Handler().postDelayed(new Runnable() { @Override public void run() { //此处为下拉刷新处理内容 Toast.makeText(MainActivity.this, "刷新", Toast.LENGTH_SHORT).show(); //停止下拉刷新 swipeRefreshLayout.setRefreshing(false); } },1000); } }); }