Android 5.0特性(SwipeRefreshLayout+RecyclerView)

1,实现效果

  • SwipeRefreshLayout+RecyclerView 实现下拉刷新

 

2,实现逻辑

【1】添加依赖

compile 'com.android.support:cardview-v7:23.2.0'

compile 'com.android.support:recyclerview-v7:23.2.0'

compile 'com.android.support:appcompat-v7:23.4.0'

butterknife注入

 

【1】创建布局

  • SwipeRefreshLayout 包裹RecyclerView







    

        

        

    



 

 

【2】获取控件

  • 获取控件,设置布局管理者 ,设置Adapter

   rv.setLayoutManager(new LinearLayoutManager(this));

        adapter = new MyAdapter();

        rv.setAdapter(adapter);

【3】设置RecyclerView的Adapter

  • itme布局

 





    

    



    

    







设置adapter

  •  
import android.support.v7.widget.RecyclerView;

import android.view.LayoutInflater;

import android.view.View;

import android.view.ViewGroup;

import android.widget.ImageView;

import android.widget.RelativeLayout;

import android.widget.TextView;





import java.util.ArrayList;

import java.util.List;





import butterknife.BindView;

import butterknife.ButterKnife;





/**

* Created by Administrator on 2016/11/30.

*/

//是什么?是一堆  get方法 。由rv,lv,gv viewpager调用

// RecyclerView.Adapter封装了 getView优化

//条目的高度由什么决定 ?由内容的高度

public class MyAdapter extends RecyclerView.Adapter {

    //1.7 泛型自动推断

    private List mList = new ArrayList<>();





    public MyAdapter() {

        for (int i = 0; i < 30; i++) {

            mList.add("一碗牛肉面" + i);

        }

    }

    //条目数量

    @Override

    public int getItemCount() {

        return mList.size();

    }





    //RecyclerView.ViewHolder是缓存条目控件

    @Override

    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

//        View itemView=View.inflate(parent.getContext(),itemId,null);

        //1.条目布局

        //2.RecyclerView  传入给item参考宽度

        //3.是否把打气布局添加到父控件 true添加,报错 二次添加  false,不添加 仅参考宽度

        View itemView= LayoutInflater.from(parent.getContext()).inflate(R.layout.item_cardview,parent,false);

        MyViewHolder myViewHolder=new MyViewHolder(itemView);

        return myViewHolder;

    }





    @Override

    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {

        MyViewHolder hd= (MyViewHolder) holder;

        //数据

        String item = mList.get(position);

        hd.text.setText(item);

        //字段

    }





    public void clear() {

        mList.clear();

    }





    public void addAll(List list) {

        mList.addAll(list);

    }









    static class MyViewHolder extends RecyclerView.ViewHolder{

        @BindView(R.id.img)

        ImageView img;

        @BindView(R.id.text)

        TextView text;

        @BindView(R.id.activity_main)

        RelativeLayout activityMain;

        public MyViewHolder(View itemView) {

            super(itemView);

            ButterKnife.bind(this, itemView);

        }

    }

}



【4】处理业务逻辑

SwipeRefreshLayout设置进度圈颜色

SwipeRefreshLayout设置下拉刷新后松开手的业务逻辑

refreshlayout.setColorSchemeColors(R.color.colorPrimary);

        SwipeRefreshLayout.OnRefreshListener listener=new SwipeRefreshLayout.OnRefreshListener() {

            @Override

            public void onRefresh() {

                //下拉刷新在项目中一般是重新从网络获取第一页数据(清空再添加)

                getFirstPage();

            }

        };

        refreshlayout.setOnRefreshListener(listener);

松开重新加载数据





    public void getFirstPage() {

        new Handler().postDelayed(new Runnable() {

            @Override

            public void run() {

                List list = new ArrayList<>();

                for (int i = 0; i < 20; i++) {

                    list.add("下拉 出来一碗牛肉面"+i+" "+new Date().toLocaleString());

                }

                adapter.clear();

                adapter.addAll(list);

                adapter.notifyDataSetChanged();

                refreshlayout.setRefreshing(false);//关闭刷新

            }

        },3000);

    }

 

你可能感兴趣的:(Android,新特性)