本文主要是介绍调用OKhttp封装类请求网络数据适配到Recyclerview上拉下拉刷新,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
加入依赖
compile 'com.liaoinstan.springview:library:1.3.0'
compile 'com.android.support:recyclerview-v7:25.3.1'
recyclerview布局的代码
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:orientation="vertical"android:layout_height="match_parent"><com.liaoinstan.springview.widget.SpringViewandroid:layout_width="match_parent"android:id="@+id/springview"android:layout_height="match_parent"><android.support.v7.widget.RecyclerViewandroid:layout_width="match_parent"android:layout_height="match_parent"android:id="@+id/recyclerview" ></android.support.v7.widget.RecyclerView></com.liaoinstan.springview.widget.SpringView>
</LinearLayout>
代码里面的
public class LieBiaoFragment extends Fragment{private SpringView springView;private RecyclerView recyclerView;private RecyAdapter recyAdapter;@Nullable@Overridepublic View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {View view = inflater.inflate(R.layout.fragment_liebiao,container,false);springView = (SpringView) view.findViewById(R.id.springview);recyclerView = (RecyclerView) view.findViewById(R.id.recyclerview);return view;}int page = 0;@Overridepublic void onActivityCreated(@Nullable Bundle savedInstanceState) {super.onActivityCreated(savedInstanceState);recyAdapter = new RecyAdapter(getActivity());LinearLayoutManager manager = new LinearLayoutManager(getActivity(),LinearLayoutManager.VERTICAL,false);recyclerView.setLayoutManager(manager);//进入页面访问数据getData();springView.setHeader(new DefaultHeader(getActivity()));springView.setFooter(new DefaultFooter(getActivity()));springView.setListener(new SpringView.OnFreshListener() {@Overridepublic void onRefresh() {page ++;getData();springView.onFinishFreshAndLoad();}@Overridepublic void onLoadmore() {page = 0;getData();springView.onFinishFreshAndLoad();}});}//public void getData(){final String path = "http://tingapi.ting.baidu.com/v1/restserver/ting?method=baidu.ting.billboard.billList&type=1&size=10&offset="+page;OkhttpUtils.getInstance().asy(null, path, new AbstractUiCallBack<RecyBean>() {@Override//abstractUiCallBack的接口回调public void success(RecyBean bean) {//获取数据 .调用适配器中的添加数据的方法,,刷新添加到前面recyAdapter.addData(bean.getSong_list());recyclerView.setAdapter(recyAdapter);}@Overridepublic void failure(Exception e) {Toast.makeText(getActivity(),"e:"+e,Toast.LENGTH_SHORT).show();}});}}
这篇关于调用OKhttp封装类请求网络数据适配到Recyclerview上拉下拉刷新的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!