刚开始学习的Listview,后面想做一个上拉刷新以及下拉加载,但是发现挺麻烦的,在github上面看见人家推荐使用RecycleView,在网上看看确实挺容易的,一般都是推荐RecycleView+google 提供的SwipeRefreshLayout,有一点不知道google怎么搞的,(SwipeRefreshLayout居然和RecyclerView冲突,连续下拉,刷新样式会一团糟,还会卡住)—这个是我看别人说的。现在很多人都知道RecycleView的作用了,所以他的东西我也不多说了,我就说下SwipeToLoadLayout,反正我在网上看这类资料都有点乱,我就自己写一篇,以后忘了再看看!
1.支持上拉下拉,头部和尾部也都是用接口实现,所以我们只需要在实现接口里面的内容就可以轻松写一个刷新效果,就像使用BaseAdapter一样,自己慢慢弄,都可以轻松实现。
2.只需要将SwipeToLoadLayout包裹住内容,支持各种View和ViewGroup(RecycleView、TextView…….++) 就可以实现啦
3.代码简单,层次分明。适合新手(反正我觉得挺好的,新手一枚,有什么错误的地方别见笑)。
文章实现SwipeToLoadLayout+RecyleView,并且自定义上拉刷新以及下拉加载,现在很多列表都需要有网络图片,我就一并写在这个了吧,实现自定义Item的点击事件,以及获取Item里的内容。
因为内容数据本地添加太麻烦,我就用了Bmob的后端云数据库来使用(太懒=-=)。
这里推荐一个图片控件Fresco ,只需要给他图片地址就可以了。
1.显示占位图直到加载完成;
2.下载图片;
3.缓存图片;
4.图片不再显示时,从内存中移除;
解决大问题,不然用异步下载还要写缓存,麻烦!
实际效果如图所示:
好啦好啦,开始吧=-=
第一步
repositories {
maven { url “https://jitpack.io” }
}
compile ‘com.android.support:recyclerview-v7:24.2.1’
compile ‘com.github.Aspsine:SwipeToLoadLayout:1.0.3’
compile ‘com.facebook.fresco:fresco:0.12.0’
这个用android studio的应该都会吧,不会我也无奈了=-=
主页面布局文件
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.aspsine.swipetoloadlayout.SwipeToLoadLayout
android:id="@+id/swipeToLoadLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.example.plix.recyleswipetoloadlayout.RefreshHeadView
android:id="@+id/swipe_refresh_header"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<android.support.v7.widget.RecyclerView
android:id="@+id/swipe_target"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical">
</android.support.v7.widget.RecyclerView>
<com.example.plix.recyleswipetoloadlayout.LoadMoreView
android:id="@+id/swipe_load_more_footer"
android:layout_width="match_parent"
android:padding="20dp"
android:gravity="center"
android:layout_height="wrap_content" />
</com.aspsine.swipetoloadlayout.SwipeToLoadLayout>
</RelativeLayout>
下面是主页面代码
public class SwipeToLayoutActivity extends Activity implements OnRefreshListener, OnLoadMoreListener {
private RecyclerView mRecycleView;
SwipeToLoadLayout swipeToLoadLayout;
private RecyleViewApadter adapter;
private int size=10;
private List<person> data;
private LinearLayoutManager layoutManager;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.recyle_main);
Fresco.initialize(this);//fresco的控件初始化
Bmob.initialize(this,"这个不能看,哈哈~-~");
//RecycleView的一些东西,不知道的去看看RecycleView
swipeToLoadLayout = ((SwipeToLoadLayout) findViewById(R.id.swipeToLoadLayout));
mRecycleView = ((RecyclerView) findViewById(R.id.swipe_target));
layoutManager = new LinearLayoutManager(this);//创建线性布局
layoutManager.setOrientation(LinearLayoutManager.VERTICAL);//垂直方向
mRecycleView.addItemDecoration(new DividerItemDecoration(getApplicationContext(), DividerItemDecoration.VERTICAL_LIST));//添加分割线
mRecycleView.setLayoutManager(layoutManager);//给RecyclerView设置布局管理器
mRecycleView.setItemAnimator(new DefaultItemAnimator());//设置Item增加、移除动画
find(size);
swipeToLoadLayout.setOnRefreshListener(this);//下拉
swipeToLoadLayout.setOnLoadMoreListener(this);//上拉
//RecycleView的点击事件
mRecycleView.addOnItemTouchListener(new RecyclerItemClickListener(this, new RecyclerItemClickListener.OnItemClickListener() {
@Override
public void onItemClick(View view, int position) {
person per= data.get(position);
String aaa= per.getName();//获得点击的item的里面的值
Toast.makeText(SwipeToLayoutActivity.this,aaa,Toast.LENGTH_SHORT).show();
}
@Override
public void onLongClick(View view, int posotion) {
}
}));
}
private void Data(List<person> data)
{
adapter = new RecyleViewApadter(this,data);//传递数据搭载适配器