您的位置:首页 > 移动开发 > Android开发

Android Material的SwipeRefreshLayout使用

2016-11-29 17:34 169 查看
写在前面

之前一直都是用开源的下拉刷新框架,最近看了material desgin中下拉刷新效果,感觉挺好的。自己就研究研究了。

在xml文件中的使用

这个控件在supportV4就提供的。使用的时候要在gradle中添加依赖。

compile 'com.android.support:support-v4:25.0.0'  //25.0.0是版本号,个人视情况而定


在xml文件中布局的配置

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<android.support.v4.widget.SwipeRefreshLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/refreshlayout_view">
<ListView
android:id="@+id/lv_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</ListView>
</android.support.v4.widget.SwipeRefreshLayout>
</RelativeLayout>


xml中资源color配置文件

<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="color_1">#8477f4</color>
<color name="color_2">#4c7a9e</color>
<color name="color_3">#2c405e</color>
<color name="color_4">#395d79</color>
<color name="swipe_background_color">#fafbfc</color>
</resources>


在activity中的使用

public class MainActivity extends AppCompatActivity {

private SwipeRefreshLayout swipeRefreshLayout;
private ListView mListView;
private List<String> mList;
private ArrayAdapter<String> adapter;
private final int SUCCESS = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

mListView = (ListView)findViewById(R.id.lv_view);
mList = new ArrayList<String>();
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mList);
mListView.setAdapter(adapter);

//找到下拉控件
swipeRefreshLayout = (SwipeRefreshLayout)findViewById(R.id.refreshlayout_view);
//设置下拉刷新过程中进度动画四种颜色变化
swipeRefreshLayout.setColorSchemeResources(R.color.color_1,
R.color.color_2,
R.color.color_3,
R.color.color_4);
//进度圈的背景颜色
swipeRefreshLayout.setProgressBackgroundColor(R.color.swipe_background_color);
//progress的位置从100位置移动到200 单位是scale
swipeRefreshLayout.setProgressViewOffset(true, 100, 200);
//设置手指在屏幕下拉多少距离会触发下拉刷新
//swipeRefreshLayout.setDistanceToTriggerSync(50);
//实现下拉滚动效果,150是下拉的位置
swipeRefreshLayout.setProgressViewEndTarget(true, 150);
//设置圆圈的大小,只有两个值:DEFAULT、LARGE
swipeRefreshLayout.setSize(SwipeRefreshLayout.LARGE);
//手势滑动监听
swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
//new一个线程实现刷新
new Thread(new Runnable() {
@Override
public void run() {
mList.clear(); //清空数据
//刷新添加的内容
for(int
4000
i=1;i<20;i++){
mList.add("刷新结果"+i);
}
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
//刷新成功
mHandler.sendEmptyMessage(SUCCESS);
}
}).start();
}
});
}
//new一个处理程序,接收传过来的数据并做处理
private Handler mHandler = new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
switch (msg.what) {
//返回成功
case 1:
swipeRefreshLayout.setRefreshing(false);//关闭刷新
//将刷新得到的数据添加到适配器里
adapter.notifyDataSetChanged();
break;
default:
break;
}
}

};
}


更多的使用方法,Google的SwipeRefreshLayout官网文档

源码在此
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息