【Android】折叠式布局+RecyclerView上拉加载和下拉刷新

先上图片看一下效果

     【Android】折叠式布局+RecyclerView上拉加载和下拉刷新_第1张图片                        【Android】折叠式布局+RecyclerView上拉加载和下拉刷新_第2张图片 

 

界面代码main_activity.xml的代码如下:




    

        
        
            
            

                

                    

                        

                        

                            
                                
                                

                            
                            

                            
                                

                                
                            

                        

                        

                            
                                
                                
                            
                        


                    


                    

                    

                        

                            

                            


                        

                        

                        

                        

                    

                    

                
            
            
            

                
                    

                    

                

                
                    

                    

                

                
                    

                    

                

            

        
        
            
            
        

    


其实界面代码网上都有很多,接下来看一下activity代码:

package com.huoniao.cq.mymode.activity;

import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;

import com.huoniao.cq.mymode.R;
import com.huoniao.cq.mymode.adapter.DetailsRecyclerViewAdapter;
import com.huoniao.cq.mymode.bean.StationDetailsBean;
import com.huoniao.cq.mymode.widget.SwipeRecyclerView;

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

import butterknife.ButterKnife;
import butterknife.InjectView;

public class MainActivity extends AppCompatActivity {

    @InjectView(R.id.swipeRecyclerView)
    SwipeRecyclerView recyclerView;

    private List dateList = new ArrayList<>();
    private List dateList1 = new ArrayList<>();
    private DetailsRecyclerViewAdapter adapter;

    private int pagerSize = 10;
    int totlePage = 5;
    int page = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.inject(this);
        getTrainPaymentSumDetailadapter(initData() , page);
    }

    private void getTrainPaymentSumDetailadapter(List dataBeanList , int pageNumber){

        if (pageNumber == 1) {
            settimeAdapter(dataBeanList);
        }else {
            //上拉加载刷新adapter,重新赋值,用set方法去刷新adapter,不然每次上拉加载都会回到第一条数据,设置recyclerView.setFocusable(false);没作用
            dateList.addAll(dataBeanList);
            adapter.notifyDataSetChanged();
        }
    }
    //第一次加载数据走这边
    private void settimeAdapter(List dataBeanList){
        recyclerView.complete();
        recyclerView.onNoMore(null);
        recyclerView.onLoadingMore();
        recyclerView.getSwipeRefreshLayout().setColorSchemeColors(getResources().getColor(R.color.colorPrimary));
        recyclerView.getRecyclerView().setLayoutManager(new LinearLayoutManager(this));
        dateList.addAll(dataBeanList);
        adapter = new DetailsRecyclerViewAdapter(this , dateList);
        recyclerView.setAdapter(adapter);
        recyclerView.setOnLoadListener(new SwipeRecyclerView.OnLoadListener() {
            @Override
            public void onRefresh() {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        dateList.clear();
                        dateList1.clear();
                        page = 1;
                        getTrainPaymentSumDetailadapter(initData() , page);
                        recyclerView.complete();
                        adapter.notifyDataSetChanged();
                    }
                }, 1000);
            }
            @Override
            public void onLoadMore() {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        if (page == totlePage){
                            recyclerView.onNoMore("已经全部加载完毕");
                        }else {
                            dateList1.clear();
                            getTrainPaymentSumDetailadapter(initData() , page);
                            recyclerView.stopLoadingMore();
                            adapter.notifyDataSetChanged();
                            page++;
                        }
                    }
                }, 1000);
            }
        });
        //设置自动下拉刷新,切记要在recyclerView.setOnLoadListener()之后调用
        //因为在没有设置监听接口的情况下,setRefreshing(true),调用不到OnLoadListener
        recyclerView.setRefreshing(false);

    }

    private List initData(){
        for (int i = 0; i < pagerSize; i++) {
            StationDetailsBean dateBean = new StationDetailsBean();
            dateBean.setId((i+1)+"");
            dateBean.setName("这是第" + (i+1) +"条数据");
            dateList1.add(dateBean);
        }
        return dateList1;
    }

}

整个实现的代码大家可以看这里

你可能感兴趣的:(Android)