上下拉刷新

来源:互联网 发布:最新网络歌曲打包下载 编辑:程序博客网 时间:2024/04/28 09:39

主类

package com.mydemo;import android.support.v4.widget.SwipeRefreshLayout;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.support.v7.widget.LinearLayoutManager;import android.support.v7.widget.RecyclerView;import com.mydemo.utils.GsonObjectCallback;import com.mydemo.utils.OkHttp3Utils;import java.io.IOException;import java.util.List;import okhttp3.Call;public class MainActivity extends AppCompatActivity {    RecyclerView rc;    SwipeRefreshLayout swf;    LinearLayoutManager layoutManager;    MyNewAdapter adapter;    String url="http://news-at.zhihu.com/api/4/news/latest";    String urlMore="http://news-at.zhihu.com/api/4/news/before/20131119";    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        rc= (RecyclerView) findViewById(R.id.rc);        swf= (SwipeRefreshLayout) findViewById(R.id.swf);        layoutManager = new LinearLayoutManager(MainActivity.this);        rc.setLayoutManager(layoutManager);        getServerData();        swf.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener()  {            @Override            public void onRefresh() {                OkHttp3Utils.doGet(urlMore, new GsonObjectCallback<NewsBean>() {                    @Override                    public void onUi(NewsBean newsBean) {                        List<NewsBean.StoriesBean> stories = newsBean.getStories();                        adapter.refreshMore(stories);                    }                    @Override                    public void onFailed(Call call, IOException e) {                    }                });                if(swf.isRefreshing()){                    //隐藏掉下拉刷新进度条                    swf.setRefreshing(false);                }            }        });//RecycleView天机滚动监听        rc.addOnScrollListener(new RecyclerView.OnScrollListener(){            private int lastPOsition;            @Override            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {                super.onScrollStateChanged(recyclerView, newState);                if(lastPOsition+1==adapter.getItemCount()&&newState==RecyclerView.SCROLL_STATE_IDLE){                    OkHttp3Utils.doGet(urlMore, new GsonObjectCallback<NewsBean>() {                        @Override                        public void onUi(NewsBean newsBean) {                            List<NewsBean.StoriesBean> stories = newsBean.getStories();                            adapter.loadMore(stories);                        }                        @Override                        public void onFailed(Call call, IOException e) {                        }                    });                }            }            @Override            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {                super.onScrolled(recyclerView, dx, dy);                lastPOsition = layoutManager.findLastVisibleItemPosition();            }        });    }    private void getServerData() {        OkHttp3Utils.doGet(url, new GsonObjectCallback<NewsBean>() {            @Override            public void onUi(NewsBean newsBean) {                if(adapter==null){                    List<NewsBean.TopStoriesBean> top_stories = newsBean.getTop_stories();                    adapter = new MyNewAdapter(MainActivity.this,newsBean,top_stories);                    rc.setAdapter(adapter);                }else{                    adapter.notifyDataSetChanged();                }            }            @Override            public void onFailed(Call call, IOException e) {            }        });    }}

适配器----------------------------------------------------------------


package com.mydemo;import android.content.Context;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.TextView;import java.util.List;public class MyNewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{    List<NewsBean.TopStoriesBean> top_stories;    Context context;    NewsBean newsBean;    List<NewsBean.StoriesBean> stories;    public MyNewAdapter(Context context, NewsBean newsBean, List<NewsBean.TopStoriesBean> top_stories) {        this.context = context;        this.newsBean = newsBean;        this.top_stories=top_stories;    }    @Override    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {        if(viewType==0){            View view = LayoutInflater.from(context).inflate(R.layout.footer_item, parent, false);            return new VHFooter(view);        }else{            View view = LayoutInflater.from(context).inflate(R.layout.item, parent, false);            return new  Content(view);        }    }    @Override    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {        int type = getItemViewType(position);        if(type==0){            return;        }        if(type==1) {            Content content=new Content(holder.itemView);            stories = newsBean.getStories();            content.tv_newTitle.setText(stories.get(position).getTitle());            content.tv_newTime.setText(stories.get(position).getGa_prefix());        }    }    @Override    public int getItemCount() {        return newsBean.getStories().size()+1;    }    @Override    public int getItemViewType(int position) {        if(position==getItemCount()-1){            return 0;        }else        {            return 1;        }    }    private class Content extends RecyclerView.ViewHolder{        private final ImageView iv_new;        private final TextView tv_newTitle;        private final TextView tv_newTime;        public Content(View itemView) {            super(itemView);            iv_new = (ImageView) itemView.findViewById(R.id.iv_new);            tv_newTitle = (TextView) itemView.findViewById(R.id.tv_newTitle);            tv_newTime = (TextView) itemView.findViewById(R.id.tv_NewTime);        }    }    private class VHFooter extends RecyclerView.ViewHolder{        public VHFooter(View itemView) {            super(itemView);        }    }    public void loadMore(List<NewsBean.StoriesBean> s ){        for (NewsBean.StoriesBean str : s){            stories.add(str);        }        //更新界面        notifyDataSetChanged();    }    public void refreshMore(List<NewsBean.StoriesBean> s){        for (NewsBean.StoriesBean str : s){            stories.add(0,str);        }        //更新界面        notifyDataSetChanged();    }}bean类参考 
package com.mydemo;import java.util.List;public class NewsBean {    /**     * date : 20171025     * stories : [{"title":"装修不要电视墙、吊顶、壁纸,咦,怎么好像廉价出租屋?","ga_prefix":"102519","images":["https://pic4.zhimg.com/v2-20e7b66c633a90d49a048b4453a591af.jpg"],"multipic":true,"type":0,"id":9653932},{"images":["https://pic1.zhimg.com/v2-08f97fddae1aa35993d4dfb14ea31054.jpg"],"type":0,"id":9653750,"ga_prefix":"102518","title":"为什么很多女生会喜欢听甜言蜜语?"},{"images":["https://pic1.zhimg.com/v2-b4747aa8b8e4f5d9af2115430862b9f0.jpg"],"type":0,"id":9653557,"ga_prefix":"102517","title":"郭襄寻不到杨过出家为尼,为何不回到家人身边抵抗敌人同生共死?"},{"images":["https://pic2.zhimg.com/v2-4f639930a1cf7e6e2f530343cfacb439.jpg"],"type":0,"id":9653800,"ga_prefix":"102516","title":"你想当一个活着的怪物,还是做一个死去的好人?"},{"images":["https://pic2.zhimg.com/v2-3af4452834bd47d965de04e32b056fe9.jpg"],"type":0,"id":9653394,"ga_prefix":"102515","title":"如今「坏人」已经可以列出 18 种,他们擅长的东西你都不要信"},{"images":["https://pic3.zhimg.com/v2-7b58d1a1a38b6302d76d2698b0a08d8a.jpg"],"type":0,"id":9653576,"ga_prefix":"102514","title":"把树抱住后使劲摇摇摇\u2026\u2026你看人家这摘果子的方法清奇的"},{"images":["https://pic1.zhimg.com/v2-714718adf58cc1a111c422d2514e2354.jpg"],"type":0,"id":9653776,"ga_prefix":"102513","title":"记住这些知识点,去日本迷失在车站里也能会心一笑"},{"images":["https://pic2.zhimg.com/v2-36c115eaf6a006719f71a94cd2993f59.jpg"],"type":0,"id":9653346,"ga_prefix":"102512","title":"大误 · 让地球从内到外冷下来"},{"images":["https://pic3.zhimg.com/v2-c98d605967a61c53abd811e683e62dde.jpg"],"type":0,"id":9653785,"ga_prefix":"102511","title":"歌手是怎么创作歌曲的?"},{"images":["https://pic3.zhimg.com/v2-ed8af5cd65b6bca6c9b4966b05e1b72a.jpg"],"type":0,"id":9653414,"ga_prefix":"102510","title":"虽说「人心狠毒」,为什么很少见到真正有毒的哺乳动物?"},{"images":["https://pic4.zhimg.com/v2-c48d2c752ec7b8b183055667b76596c7.jpg"],"type":0,"id":9653540,"ga_prefix":"102509","title":"没和身边人一起进大律所,可后来,我却学得比他们多"},{"images":["https://pic3.zhimg.com/v2-8d3803d6014153f1aa9835b47ccd7db2.jpg"],"type":0,"id":9653658,"ga_prefix":"102508","title":"没有「爆裂脑花」、细思 bug 极多\u2026\u2026唯一的亮点只剩特工代号"},{"images":["https://pic3.zhimg.com/v2-8569d560d951c65cc1c712b8976c8fba.jpg"],"type":0,"id":9653620,"ga_prefix":"102507","title":"身为杂食性动物的两脚兽,我们要吃蔬菜水果,喵汪星人呢?"},{"images":["https://pic2.zhimg.com/v2-340313b0e29d374f9a7fbe3cb45483e1.jpg"],"type":0,"id":9653718,"ga_prefix":"102507","title":"姚老板的球队能卖多少钱,起决定作用的是他另一个身份"},{"images":["https://pic3.zhimg.com/v2-d839d13157eb525ed60c34e39fee4d1a.jpg"],"type":0,"id":9653251,"ga_prefix":"102507","title":"《天才枪手》式的造富神话:非典型批片、中国采购团和弯道超车梦"},{"images":["https://pic1.zhimg.com/v2-ea05acac99ff29b8d1b60de506dbcfe4.jpg"],"type":0,"id":9653582,"ga_prefix":"102506","title":"瞎扯 · 如何正确地吐槽"}]     * top_stories : [{"image":"https://pic3.zhimg.com/v2-20786b64172211a00c6e611b1bfbb2b6.jpg","type":0,"id":9653557,"ga_prefix":"102517","title":"郭襄寻不到杨过出家为尼,为何不回到家人身边抵抗敌人同生共死?"},{"image":"https://pic4.zhimg.com/v2-97d9c4d8c3c673b10772682e5ac0c137.jpg","type":0,"id":9653785,"ga_prefix":"102511","title":"歌手是怎么创作歌曲的?"},{"image":"https://pic2.zhimg.com/v2-e7582788c34b9d40b7b849ea3458d0dd.jpg","type":0,"id":9653718,"ga_prefix":"102507","title":"姚老板的球队能卖多少钱,起决定作用的是他另一个身份"},{"image":"https://pic1.zhimg.com/v2-e5b5e2342378517d1ddeb3f26496367c.jpg","type":0,"id":9653251,"ga_prefix":"102507","title":"《天才枪手》式的造富神话:非典型批片、中国采购团和弯道超车梦"},{"image":"https://pic2.zhimg.com/v2-2f8827e1dd120aecea73713fd27f67d1.jpg","type":0,"id":9653576,"ga_prefix":"102514","title":"把树抱住后使劲摇摇摇\u2026\u2026你看人家这摘果子的方法清奇的"}]     */    private String date;    private List<StoriesBean> stories;    private List<TopStoriesBean> top_stories;    public String getDate() {        return date;    }    public void setDate(String date) {        this.date = date;    }    public List<StoriesBean> getStories() {        return stories;    }    public void setStories(List<StoriesBean> stories) {        this.stories = stories;    }    public List<TopStoriesBean> getTop_stories() {        return top_stories;    }    public void setTop_stories(List<TopStoriesBean> top_stories) {        this.top_stories = top_stories;    }    public static class StoriesBean {        /**         * title : 装修不要电视墙、吊顶、壁纸,咦,怎么好像廉价出租屋?         * ga_prefix : 102519         * images : ["https://pic4.zhimg.com/v2-20e7b66c633a90d49a048b4453a591af.jpg"]         * multipic : true         * type : 0         * id : 9653932         */        private String title;        private String ga_prefix;        private boolean multipic;        private int type;        private int id;        private List<String> images;        public String getTitle() {            return title;        }        public void setTitle(String title) {            this.title = title;        }        public String getGa_prefix() {            return ga_prefix;        }        public void setGa_prefix(String ga_prefix) {            this.ga_prefix = ga_prefix;        }        public boolean isMultipic() {            return multipic;        }        public void setMultipic(boolean multipic) {            this.multipic = multipic;        }        public int getType() {            return type;        }        public void setType(int type) {            this.type = type;        }        public int getId() {            return id;        }        public void setId(int id) {            this.id = id;        }        public List<String> getImages() {            return images;        }        public void setImages(List<String> images) {            this.images = images;        }    }    public static class TopStoriesBean {        /**         * image : https://pic3.zhimg.com/v2-20786b64172211a00c6e611b1bfbb2b6.jpg         * type : 0         * id : 9653557         * ga_prefix : 102517         * title : 郭襄寻不到杨过出家为尼,为何不回到家人身边抵抗敌人同生共死?         */        private String image;        private int type;        private int id;        private String ga_prefix;        private String title;        public String getImage() {            return image;        }        public void setImage(String image) {            this.image = image;        }        public int getType() {            return type;        }        public void setType(int type) {            this.type = type;        }        public int getId() {            return id;        }        public void setId(int id) {            this.id = id;        }        public String getGa_prefix() {            return ga_prefix;        }        public void setGa_prefix(String ga_prefix) {            this.ga_prefix = ga_prefix;        }        public String getTitle() {            return title;        }        public void setTitle(String title) {            this.title = title;        }    }}