SmartRefreshLayout 刷新

依赖

//上啦刷新
compile 'com.scwang.smartrefresh:SmartRefreshLayout:1.0.5.1'

paragraph_layout.xml

<?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">

    <com.scwang.smartrefresh.layout.SmartRefreshLayout
        android:id="@+id/para_srl"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <android.support.v7.widget.RecyclerView
            android:id="@+id/para_rcy"
            android:layout_width="match_parent"
            android:layout_height="match_parent"></android.support.v7.widget.RecyclerView>
    </com.scwang.smartrefresh.layout.SmartRefreshLayout>
</LinearLayout>
ParagraphFragment
package com.example.quarterhour.view.fragment;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.quarterhour.R;
import com.example.quarterhour.model.beans.ParagraBeans;
import com.example.quarterhour.presenter.ParagraPresenter;
import com.example.quarterhour.view.adapter.ParagraRcyAdapter;
import com.example.quarterhour.view.interfaces.IParagraView;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by nyj on 2018/6/5.
 */

public class ParagraphFragment extends Fragment implements IParagraView {

    private RecyclerView para_rcy;
    private SmartRefreshLayout para_srl;


    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View inflate = inflater.inflate(R.layout.paragraph_layout, container, false);
        para_rcy = inflate.findViewById(R.id.para_rcy);
        para_srl = inflate.findViewById(R.id.para_srl);
        return inflate;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        //设置布局管理器
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false);
        para_rcy.setLayoutManager(linearLayoutManager);
        //获取数据
        initData();
        //======刷新
        para_srl.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(RefreshLayout refreshLayout) {
                refreshLayout.finishLoadMore(2000);
            }

            @Override
            public void onRefresh(RefreshLayout refreshLayout) {
                refreshLayout.finishRefresh(2000);
                initData();
            }
        });
    }

    private void initData() {
        ParagraPresenter paragraPresenter = new ParagraPresenter();
        paragraPresenter.attachView(this);
        SharedPreferences login = getActivity().getSharedPreferences("login", Context.MODE_PRIVATE);
        String token = login.getString("token", "000");
        paragraPresenter.loadDataParagra("android", token, "101", "1");

    }

    @Override
    public void onSuccess(ParagraBeans paragraBeans) {
        List<ParagraBeans.DataBean> list = new ArrayList<>();
        List<ParagraBeans.DataBean> data = paragraBeans.getData();
        list.addAll(data);
        //设置适配器
        ParagraRcyAdapter paragraRcyAdapter = new ParagraRcyAdapter(getActivity(), list);
        para_rcy.setAdapter(paragraRcyAdapter);
    }
}

猜你喜欢

转载自blog.csdn.net/niu_yue_jiao/article/details/80627411
今日推荐