4.2.5 Android 下拉刷新的几个方法:SwipeRefreshLayout,android-Ultra-Pull-To-Refresh(ptr),PullToRefreshListView

1: SwipeRefreshLayout

2: android-Ultra-Pull-To-Refresh(ptr),

3: PullToRefreshListView


SwipeRefreshLayout swipeRefreshLayout =(SwipeRefreshLayout)findViewById(R.id.main_swipeRefreshLayout);
// swipeRefreshLayout.setRefreshing(true);  swipeRefreshLayout.setColorSchemeColors(Color.RED,Color.BLUE,Color.YELLOW);
        swipeRefreshLayout.setSize(SwipeRefreshLayout.LARGE);
        swipeRefreshLayout.setOnRefreshListener(this);
 
 

下拉刷新的大小,只有两种模式,一个是小的,一个是大的。

swipe.setSize(SwipeRefresh.LARGE);
还可以设置一组颜色
swipe.setColorSchemeColors(Color.RED,Color.YELLOW,Color.BLUE);
swipe.setOnRefreshListener();

public ovid onRefresh(){
HttpUtils.getService().getArticle.....
就是刷新第一页。
}

/**  * A simple {@link Fragment} subclass.  */ public class LeiBieFragment extends Fragment implements Callback<LeiBieEntity>, SwipeRefreshLayout.OnRefreshListener {
    private RecyclerView recyclerView;
    private LeiBieAdapter adapter;
    private SwipeRefreshLayout mSwipeRefreshLayout;
    public LeiBieFragment() {
        // Required empty public constructor  }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment  View ret = inflater.from(getContext()).inflate(R.layout.fragment_lei_bie, container, false);
// return inflater.inflate(R.layout.fragment_lei_bie, container, false);  mSwipeRefreshLayout =(SwipeRefreshLayout)ret.findViewById(R.id.leibie_swiperefreshlayout);
        recyclerView = (RecyclerView)ret.findViewById(R.id.leibie_recyclerview);
        adapter = new LeiBieAdapter(ret.getContext(),new ArrayList<LeiBieEntity.DataEntity.ReturnDataEntity.RankinglistEntity>());
        recyclerView.setAdapter(adapter);
//SwipeRefreshLayout-----------
        mSwipeRefreshLayout.setOnRefreshListener(this);
        mSwipeRefreshLayout.setColorSchemeColors(Color.RED, Color.BLUE, Color.CYAN);
        mSwipeRefreshLayout.setSize(SwipeRefreshLayout.LARGE);
//SwipeRefreshLayout-----------
HttpUtils.getLeiBieService().getLeiBie(1446).enqueue(this); return ret; } //DataEntity.ReturnDataEntity.RankinglistEntity @Override public void onResponse(Call<LeiBieEntity> call, Response<LeiBieEntity> response) { adapter.addAll(response.body().getData().getReturnData().getRankinglist()); mSwipeRefreshLayout.setRefreshing(false); } @Override public void onFailure(Call<LeiBieEntity> call, Throwable t) { mSwipeRefreshLayout.setRefreshing(false); } @Override public void onRefresh() { HttpUtils.getLeiBieService().getLeiBie(1446).enqueue(this); }}



无论成功还是失败,都要把求收回来。
swipe.setRefreshing(false);

刷新之前一定要清空数据,并加载第一页
添加一个变量,page。
page=1;
然后

在adapter 里面添加listClean

public void clean(){
list.clean();
notify
}

在onResponse里面
page 还可以用来判断是下拉刷新和上拉加载。
if(page==1){
adapter.clear();
}




你可能感兴趣的:(4.2.5 Android 下拉刷新的几个方法:SwipeRefreshLayout,android-Ultra-Pull-To-Refresh(ptr),PullToRefreshListView)