package com.bwei.day11_lianxi4.activity; import android.content.Context; import android.support.annotation.NonNull; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import android.view.View; import com.bwei.day11_lianxi4.R; import com.bwei.day11_lianxi4.adapter.SubAdapter; import com.bwei.day11_lianxi4.bean.UserBean; import com.google.gson.Gson; import com.scwang.smartrefresh.header.BezierCircleHeader; import com.scwang.smartrefresh.layout.SmartRefreshLayout; import com.scwang.smartrefresh.layout.api.RefreshLayout; import com.scwang.smartrefresh.layout.constant.SpinnerStyle; import com.scwang.smartrefresh.layout.footer.BallPulseFooter; import com.scwang.smartrefresh.layout.header.BezierRadarHeader; import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener; import com.scwang.smartrefresh.layout.listener.OnRefreshListener; import java.io.IOException; import java.util.ArrayList; import java.util.List; import okhttp3.Call; import okhttp3.Callback; import okhttp3.OkHttpClient; import okhttp3.Request; import okhttp3.Response; public class MainActivity extends AppCompatActivity { private RecyclerView rl_view; //1. private static final String URL_STRING = "http://www.xieast.com/api/news/news.php?page="; private Context context; private SmartRefreshLayout sml_container; private int page = 1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); //8. context = MainActivity.this; //2.数据请求 //一.上拉刷新下拉加载 sml_container.setOnRefreshListener(new OnRefreshListener() { @Override public void onRefresh(@NonNull RefreshLayout refreshLayout) { sml_container.finishRefresh(2000/*,false*/); page++; requestData(); } }); sml_container.setOnLoadMoreListener(new OnLoadMoreListener() { @Override public void onLoadMore(@NonNull RefreshLayout refreshLayout) { sml_container.finishLoadMore(2000/*,false*/); page--; } }); //二.设置上拉,下拉的样式 sml_container.setRefreshHeader(new BezierRadarHeader(this).setEnableHorizontalDrag(true)); sml_container.setRefreshFooter(new BallPulseFooter(this).setSpinnerStyle(SpinnerStyle.Scale)); } private void requestData() { //3 OkHttpClient okHttpClient = new OkHttpClient.Builder().build(); Request request = new Request.Builder().url(URL_STRING).build(); //4.开启请求 okHttpClient.newCall(request).enqueue(new Callback() { @Override public void onFailure(Call call, IOException e) { } @Override public void onResponse(Call call, Response response) throws IOException { //5. final String responseString = response.body().string(); runOnUiThread(new Runnable() { @Override public void run() { //6.使用Gson解析 Gson gson = new Gson(); UserBean userBean = gson.fromJson(responseString, UserBean.class); List<UserBean.DataBean> beanList = userBean.getData(); //8. LinearLayoutManager manager = new LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false); rl_view.setLayoutManager(manager); //7.创建适配器 SubAdapter subAdapter = new SubAdapter(context, (ArrayList<UserBean.DataBean>) beanList); rl_view.setAdapter(subAdapter); } }); } }); } private void initView() { rl_view = findViewById(R.id.rl_view); sml_container = findViewById(R.id.sml_container); } }
多条目
最新推荐文章于 2020-03-12 17:52:09 发布