Android-----------RecyclerView下拉加载,上拉加载更多
来源:互联网 发布:在线算法的特点 编辑:程序博客网 时间:2024/06/05 17:22
OkHttp网络请求依赖:
compile 'com.squareup.okhttp3:okhttp:3.8.1'
RecyclerView依赖:
compile 'com.android.support:recyclerview-v7:26.0.0-alpha1'
实现此功能用的是一个XRecyclerView的一个封装库
详细见链接:https://github.com/jianghejie/XRecyclerView
1.导依赖
compile 'com.jcodecraeer:xrecyclerview:1.3.2'
2.在布局中把RecyclerView换成XRecyclerView布局
<com.jcodecraeer.xrecyclerview.XRecyclerView android:id="@+id/recyclerview" android:layout_width="match_parent" android:layout_height="wrap_content"></com.jcodecraeer.xrecyclerview.XRecyclerView>
3.在MainActivity中初始化该布局
mrv=(XRecyclerView)findViewById(R.id.recyclerview);loaddata();mrv.setLayoutManager(new LinearLayoutManager(this));md=new Myadapter(this,mlist);mrv.setAdapter(md);
4.通过设置RecyclerView监听事件获得定义好的刷新和加载更多的方法
mRecyclerView.setLoadingListener(new XRecyclerView.LoadingListener() { @Override public void onRefresh() { //refresh data here } @Override public void onLoadMore() { // load more data here }});
5.刷新完或者加载完记得关闭,否则一致刷新或者加载
mRecyclerView.loadMoreComplete();
mRecyclerView.refreshComplete();
6.也可以自定义刷新和加载更多样式如:
mRecyclerView.setRefreshProgressStyle(ProgressStyle.BallSpinFadeLoader);mRecyclerView.setLaodingMoreProgressStyle(ProgressStyle.SquareSpin);
7.上MainActivity代码
public class MainActivity extends AppCompatActivity implements NetDataCallback<News>{ private String url="http://app.u17.com/v3/appV3_3/android/phone/list/commonComicList?argValue=23&argName=sort&argCon=0&android_id=4058040115108878&v=3330110&model=GT-P5210&come_from=Tg002&page="; private XRecyclerView mrv; private int page=1; private OkHttpUtils http; private Myadapter md; private ArrayList<News.DataBean.ReturnDataBean.ComicsBean> mlist=new ArrayList<>(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mrv=(XRecyclerView)findViewById(R.id.recyclerview); loaddata(); mrv.setLayoutManager(new LinearLayoutManager(this)); md=new Myadapter(this,mlist); mrv.setAdapter(md); mrv.setLoadingListener(new XRecyclerView.LoadingListener() { @Override public void onRefresh() { page=1; mlist.clear(); loaddata(); mrv.refreshComplete(); } @Override public void onLoadMore() { page++; loaddata(); mrv.loadMoreComplete(); } }); } private void loaddata() { http=new OkHttpUtils(); http.getdata(url+page,this,News.class); } @Override public void success(News news) { mlist= (ArrayList<News.DataBean.ReturnDataBean.ComicsBean>) news.getData().getReturnData().getComics(); md.setdata(mlist); md.notifyDataSetChanged(); } @Override public void fail(int code, String str) { }}
------------------------------------------------------------------------------------------RecyclerView简单的设置间隔:1)定义一个设置间隔的类public class SpacesItemDecoration extends XRecyclerView.ItemDecoration{ private int space; public SpacesItemDecoration(int space) { this.space=space; } @Override public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) { outRect.left=space; outRect.right=space; outRect.bottom=space; if(parent.getChildAdapterPosition(view)==0){ outRect.top=space; } }}2)在主界面中设置间隔://设置间隔SpacesItemDecoration decoration=new SpacesItemDecoration(16);mrv.addItemDecoration(decoration);-----------------------------------------------------------------------------------------另一种RecyclerView上拉刷新下拉加载更多的方法见链接:https://github.com/WuXiaolong/PullLoadMoreRecyclerView-------------------------------------------------------------------------------------- mouth题布局(实名认证)<RelativeLayout android:layout_width="match_parent" android:layout_height="400dp" > <ImageView android:id="@+id/userImg" android:layout_width="match_parent" android:layout_height="match_parent" android:scaleType="fitXY" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_alignParentBottom="true"> <TextView android:id="@+id/userName" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="秦心" android:textColor="#ffe100" android:layout_alignParentBottom="true" android:layout_marginLeft="20dp" android:textSize="20sp" android:layout_marginTop="40dp"/> <TextView android:id="@+id/textc" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="| 已实名认证" android:textColor="#f6fafa" android:layout_alignParentBottom="true" android:layout_marginLeft="20dp" android:textSize="20sp" android:layout_marginTop="40dp" android:layout_alignRight="@id/userName"/> </LinearLayout></RelativeLayout>
阅读全文
0 0
- Android-----------RecyclerView下拉加载,上拉加载更多
- RecyclerView 下拉刷新上拉加载更多
- RecyclerView 下拉刷新上拉加载更多
- RecyclerView 下拉刷新、上拉加载更多
- RecyclerView 下拉刷新上拉加载更多
- RecyclerView上拉刷新,下拉加载更多
- RecyclerView 下拉刷新上拉加载更多
- RecyclerView上拉加载更多,下拉刷新
- Android中RecyclerView实现下拉刷新上拉加载更多
- android中RecyclerView下拉刷新上拉加载更多
- Android RecyclerView下拉刷新和上拉加载更多
- Android RecyclerView下拉刷新 & 上拉加载更多
- Android RecyclerView下拉刷新和上拉加载更多
- Android RecyclerView下拉刷新、上拉加载更多、到底自动加载更多
- android Recyclerview上拉加载更多
- android 上拉刷新,下拉加载更多
- RecyclerView 上拉加载更多
- RecyclerView实现下拉刷新上拉加载更多
- 对RecyclerView的封装,使用简单,功能丰富
- Java中Unsafe类详解
- vue.js 练手--简单ToDoList
- ubuntu 14.04zabbix的安装
- cnn分类
- Android-----------RecyclerView下拉加载,上拉加载更多
- java导出excel,txt
- this
- Vuforia云识别
- 键盘监听事件
- Java输入输出(IO)和流的基本概念
- 英美重视IoT安全,漏洞披露和安全人才培养
- Java线程间通信问题分析(生产者消费者模型)
- solr-6.6与tomcat-8.5安装