转发请注明出处:http://blog.csdn.net/qq_28055429/article/details/59539073
一,简意:是一个下拉刷新控件,它同时也能上拉加载,使用较简单,支持api 11及上
二,用法:与SwipeRefreshLayout基本一样,但比它更强大,更漂亮,也更容易使用
(SwipeRefreshLayout简单使用可参考:http://blog.csdn.net/qq_28055429/article/details/59117869)
三,引用具体步骤:
1,在Gradle中引入依赖:
compile 'com.cjj.materialrefeshlayout:library:1.3.0'
2,在布局中直接使用,里面可以放置任意列表控件
<com.cjj.MaterialRefreshLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/refresh"
app:overlay="false"
app:wave_show="false"
app:progress_colors="@array/material_colors"
app:wave_height_type="higher"
app:progress_show_circle_backgroud="false"
>
//在这里添加所需要列表控件
<...>
</...>
</com.cjj.MaterialRefreshLayout>
其中:
overlay----->设置是否侵入刷新
wave_show------》设置是否波浪形状
3,在代码中实现:
(1)为该控件绑定id
mRefreshLayout = (MaterialRefreshLayout)view.findViewById(R.id.refresh);
(2)设置支持加载更多和设置监听:
//设置支持下拉加载更多
mRefreshLayout.setLoadMore(true);
//刷新以及加载回调
mRefreshLayout.setMaterialRefreshListener(new MaterialRefreshListener() {
//下拉刷新
@Override
public void onRefresh(MaterialRefreshLayout materialRefreshLayout) {
//
}
//上拉加载更多
@Override
public void onRefreshLoadMore(MaterialRefreshLayout materialRefreshLayout) {
}
});
另外:还可以设置自动加载和刷新
materialRefreshLayout.autoRefresh();//drop-down refresh automatically
materialRefreshLayout.autoRefreshLoadMore();// pull up refresh automatically
例子:用RecyclerView+MaterialRefreshLayout实现:
主要在上一篇文章:http://blog.csdn.net/qq_28055429/article/details/59117869
的最后一个例子中做修改:改动的布局有activity_test.xml,
布局文件:activity_test.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:fresco="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<com.cjj.MaterialRefreshLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/refresh"
app:overlay="true"
app:wave_show="true"
app:wave_color="#90ffffff"
app:progress_colors="@array/material_colors"
app:wave_height_type="higher"
>
<android.support.v7.widget.RecyclerView
android:id="@+id/recycleView"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</com.cjj.MaterialRefreshLayout>
</LinearLayout>
在适配器MyAdapter中增加方法:
public List<String> getDatas(){
return mDatas;
}
public void addData(int position,List<String> datas){
if(datas !=null && datas.size()>0) {
mDatas.addAll(datas);
notifyItemRangeChanged(position, mDatas.size());
}
}
即 获取集合,和 为mDatas从位置position起开始添加datas的数据
在TestActivity中,绑定控件,设置监听,代码如下:
public class TestActivity extends AppCompatActivity {
private final static int MAX_NUM = 3; //
private static int CURRENT_NUM = 0;
private MaterialRefreshLayout mRefreshLayout; //定义SwipeRefreshLayout
private RecyclerView mRecyclerView; //定义RecyclerView
private List<String> list = new ArrayList<>(); //定义List<String>集合
private MyAdapter adapter ; //定义MyAdapter对象
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
initView(); //初始化对象
addListDatas(); //为List<String>集合初始化数据
initDatas(); //初始化数据
}
/*
初始化对象
*/
private void initView() {
//为各个控件绑定id
mRefreshLayout = (MaterialRefreshLayout)this.findViewById(R.id.refresh);
mRecyclerView = (RecyclerView)this.findViewById(R.id.recycleView);
}
/*
为List<String>集合初始化数据
*/
private void addListDatas(){
list.add("Years");
list.add("Months");
list.add("Days");
list.add("Hours");
}
/*
初始化数据
*/
private void initDatas() {
//初始化适配器
adapter = new MyAdapter(list);
//为RecyclerView绑定适配器
mRecyclerView.setAdapter(adapter);
//
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
mRecyclerView.setItemAnimator(new DefaultItemAnimator());
adapter.setOnItemClickListener(new MyAdapter.OnItemClickListener() {
@Override
public void onClick(View v, int position, String city) {
Toast.makeText(TestActivity.this, "city:" + city + ",position:" + position, Toast.LENGTH_LONG).show();
}
});
mRefreshLayout.setLoadMore(true);
mRefreshLayout.setMaterialRefreshListener(new MaterialRefreshListener() {
//下拉刷新
@Override
public void onRefresh(MaterialRefreshLayout materialRefreshLayout) {
refreshData2();
}
//上拉加载
@Override
public void onRefreshLoadMore(MaterialRefreshLayout materialRefreshLayout) {
super.onRefreshLoadMore(materialRefreshLayout);
if(CURRENT_NUM < MAX_NUM){
loadMore();
}
else {
Toast.makeText(getApplicationContext(),"没有数据了" ,Toast.LENGTH_SHORT).show();
mRefreshLayout.finishRefreshLoadMore();
}
}
});
}
private void refreshData2() {
//这里不处理
// mRecyclerView.scrollToPosition(0);
// //向上刷新完成
// mRefreshLayout.finishRefresh();
}
private void loadMore(){
CURRENT_NUM++;
List<String> datas = new ArrayList<>();
for(int i = 0; i < 6 ; i++){
list.add("NUM : " + CURRENT_NUM + " -----> " + i);
}
//得到当前页面最下面位置
int position = adapter.getDatas().size();
//从position位置开始加载数据
adapter.addData(position , list.subList(position,list.size()));
//为view设置当前滑动位置:为新加载完毕后的最下面
mRecyclerView.scrollToPosition(adapter.getDatas().size());
//向下下拉加载完毕
mRefreshLayout.finishRefreshLoadMore();
}
}
效果,,,略