SmartRefreshLayout集成笔记,实现下拉刷新上拉加载更多。

SmartRefreshLayout是一个功能超级全面的下拉刷新,上拉加载更多框架。

1.地址:https://github.com/scwang90/SmartRefreshLayout

2.在gradle添加依赖:

compile 'com.android.support:appcompat-v7:25.3.1'//版本随意
compile 'com.scwang.smartrefresh:SmartRefreshLayout:1.0.3'
compile 'com.scwang.smartrefresh:SmartRefreshHeader:1.0.3'//没有使用特殊Header,可以不加这行
3.在布局中添加SmartRefreshLayout


    
    
        
    

4.SmartRefreshLayout的头布局和尾布局有三种方法可以定义,这里只实现一种。
在自定义的app全局定义
public class App extends Application {
    //static 代码段可以防止内存泄露
    static {
        //设置全局的Header构建器
        SmartRefreshLayout.setDefaultRefreshHeaderCreater(new DefaultRefreshHeaderCreater() {
                @Override
                public RefreshHeader createRefreshHeader(Context context, RefreshLayout layout) {
                    layout.setPrimaryColorsId(R.color.colorPrimary, android.R.color.white);//全局设置主题颜色
                    return new ClassicsHeader(context).setSpinnerStyle(SpinnerStyle.Translate);//指定为经典Header,默认是 贝塞尔雷达Header
                }
            });
        //设置全局的Footer构建器
        SmartRefreshLayout.setDefaultRefreshFooterCreater(new DefaultRefreshFooterCreater() {
                @Override
                public RefreshFooter createRefreshFooter(Context context, RefreshLayout layout) {
                    //指定为经典Footer,默认是 BallPulseFooter
                    return new ClassicsFooter(context).setSpinnerStyle(SpinnerStyle.Translate);
                }
            });
    }
}
5.代码中的实现:
public class MainActivity extends AppCompatActivity implements AdapterView.OnItemClickListener {

    private BaseRecyclerAdapter mAdpater;
    private RecyclerView mRecyclerView;
    private ArrayList al;
    private SmartRefreshLayout mRefreshLayout;
    private TestBean testBean;
    private static boolean isFirstEnter = true;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mRefreshLayout = (SmartRefreshLayout) findViewById(R.id.refreshLayout);
        mRecyclerView= (RecyclerView) findViewById(recyclerView);
        //状态栏透明和间距处理
        //标题栏处理
        StatusBarUtil.setPaddingSmart(this,findViewById(R.id.toolbar));
        StatusBarUtil.immersive(this, 0xff000000, 0.1f);
        getData();
        setAdapter();
        setListener();
    }
    private void getData() {
        al = new ArrayList<>();
       
        for (int i=0;i<50;i++){
            testBean = new TestBean();
            testBean.setName("条目"+i);
            al.add(testBean);
        }
    }

    private void setAdapter() {
        View view = findViewById(recyclerView);
        if (view instanceof RecyclerView) {
            RecyclerView recyclerView = (RecyclerView) view;
            recyclerView.setLayoutManager(new LinearLayoutManager(this));
            recyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.HORIZONTAL));
            recyclerView.setItemAnimator(new DefaultItemAnimator());
          
            recyclerView.setAdapter(mAdpater=new BaseRecyclerAdapter(al,simple_list_item_2,this) {
                @Override
                protected void onBindViewHolder(SmartViewHolder holder, TestBean model, int position) {
                    holder.text(android.R.id.text1, model.getName());
                    holder.text(android.R.id.text2, model.getName());
                    holder.textColorId(android.R.id.text2, R.color.colorAccent);
                }
            });
            mRecyclerView = recyclerView;
        }
        if (isFirstEnter) {
            isFirstEnter = false;
            //触发自动刷新
            mRefreshLayout.autoRefresh();
        }
    }
    private void setListener() {
        mRefreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                refreshlayout.finishRefresh(2000);
            }
        });
        mRefreshLayout.setOnLoadmoreListener(new OnLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                refreshlayout.finishLoadmore(2000);
            }
        });
    }
    
    @Override
    public void onItemClick(AdapterView parent, View view, int position, long id) {
        Intent intent=new Intent(this,WeiboPracticeActivity.class);
        this.startActivity(intent);
        mRefreshLayout.autoRefresh();
    }
}
 
  

 
  
 
  


你可能感兴趣的:(android开发)