RecyclerView外部嵌套SmartRefreshLayout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@color/c_F8F9FC"
xmlns:app="http://schemas.android.com/apk/res-auto">
<include layout="@layout/layout"
android:id="@+id/basetop"/>
<com.scwang.smartrefresh.layout.SmartRefreshLayout
app:layout_constraintTop_toBottomOf="@+id/basetop"
android:id="@+id/srl_fresh"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerViews"
app:layout_constraintTop_toBottomOf="@+id/ll2"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
</android.support.v7.widget.RecyclerView>
</com.scwang.smartrefresh.layout.SmartRefreshLayout>
</LinearLayout>
代码定义当前page,刷新=1,加载+1
public class TuisongActivity extends BaseActivity {
@BindView(R.id.srl_fresh)
SmartRefreshLayout srfresh;
@BindView(R.id.recyclerViews)
RecyclerView rvList;
@BindView(R.id.tv_title)
TextView tv_title;
@BindView(R.id.iv_back)
ImageView iv_back;
private int currentPage=1;
private List<TuisongBack.DataBean> mDatas=new LinkedList<>();;
private TuisongAdapter recycleAdapter;
private String TAG="TuisongActivity";
private Handler mHandler=new Handler();
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tuisong);
ButterKnife.bind(this);
tv_title.setText("信息推送");
aboutAapter();
initData();
}
@OnClick({R.id.iv_back})
public void onViewClicked(View view) {
switch (view.getId()) {
case R.id.iv_back:
finish();
break;
}
}
private void aboutAapter() {
LinearLayoutManager layoutManager = new LinearLayoutManager(TuisongActivity.this);
//设置布局管理器
rvList.setLayoutManager(layoutManager);
//设置为垂直布局,这也是默认的
layoutManager.setOrientation(OrientationHelper.VERTICAL);
rvList.setNestedScrollingEnabled(false);
rvList.setHasFixedSize(true);
rvList.setFocusable(false);
recycleAdapter = new TuisongAdapter(this , mDatas );
rvList.setAdapter(recycleAdapter);
srfresh.setRefreshHeader(new MaterialHeader(this));
srfresh.setOnRefreshListener(new OnRefreshListener() {
@Override
public void onRefresh(final RefreshLayout refreshlayout) {
//延时展示
mHandler.postDelayed(new Runnable() {
@Override
public void run() {
currentPage=1;
mDatas.clear();
initData();
refreshlayout.finishRefresh();
}
},0);
}
});
srfresh.setOnLoadMoreListener(new OnLoadMoreListener() {
@Override
public void onLoadMore(final RefreshLayout refreshlayout) {
mHandler.postDelayed(new Runnable() {
@Override
public void run() {
currentPage++;
initData();
refreshlayout.finishLoadMore();
}
},500);
}
});
// srfresh.setEnableLoadMore(true); 禁止加载更多
}
private void initData() {
Request request = new Request.Builder()
.url(NetUtils.gettuiinfoApi+"/"+currentPage)
.build();
OkHttpClient client = new OkHttpClient.Builder()
.addInterceptor(new TokenInterceptor())//添加获取token的拦截器
.build();
client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response) throws IOException {
if (response.isSuccessful()) {//回调的方法执行在子线程。
final TuisongBack rdb= new Gson().fromJson(info, TuisongBack.class);
if(1000==(rdb.getCode())){
runOnUiThread(new Runnable()
{
public void run() {
mDatas.addAll(rdb.getData());
recycleAdapter.notifyDataSetChanged();
} });}}}}); }
}