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>




原创粉丝点击