Android-----------RecyclerView下拉加载,上拉加载更多

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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值