新闻页实现下拉刷新效果

问题引入:

之前在项目中写了爬取新闻页的功能,但是没有添加下拉刷新的功能,加上之后,在下来刷新处再次调用获取新闻子线程已达到刷新功能,但是,出现问题了:下拉之后,新闻是再次获取了,但是新闻栏目数量从原来的数量加倍了。

解决方法:

再次调用获取新闻子线程时,需在导入到适配器之前将之前的RecyclerView中的item项的数据清除掉,实例如下:

public class HomePageFragment extends Fragment {
    private RecyclerView mRecyclerView;

    private List<String> news_Title = new ArrayList<>();
    private List<String> news_Link = new ArrayList<>();
    private static final String ARG_TITLE = "title";
    private String mTitle;

    private String news_target = "http://article.zhaopin.com/myresume/";//智联招聘热点排行
    private Document doc;
    private Elements link;
    private Elements title;
    private String link_head = "http:";
    private int news_list_size = 30; //新闻列表个数

    private SwipeRefreshLayout swipeRefreshLayout;

    ArrayList<Integer> images = new ArrayList<Integer>() {{
        add(R.mipmap.news_1);
        add(R.mipmap.news_2);
        add(R.mipmap.news_3);
        add(R.mipmap.news_4);
    }};

    ArrayList<String> titles = new ArrayList<String>() {{
        add("热点资讯实时排行");
        add("求职技巧,你不可错过");
        add("面试攻略看这里");
        add("求职方向");
    }};

    public HomePageFragment() {
        // Required empty public constructor
    }

    public static HomePageFragment getInstance(String title) {
        HomePageFragment fra = new HomePageFragment();
        Bundle bundle = new Bundle();
        bundle.putString(ARG_TITLE, title);
        fra.setArguments(bundle);
        return fra;
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_home_page, container,false);
        findView(view);
        new Thread(getNews).start();
        mRecyclerView = (RecyclerView) view.findViewById(R.id.recyclerview);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(mRecyclerView.getContext()));
        //下拉刷新
        swipeRefreshLayout.setColorSchemeResources(R.color.colorPrimary);
        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {

                new Thread(getNews).start();

            }
        });
        return view;
    }

    //初始化View
    private void findView(View view) {
        Banner banner = view.findViewById(R.id.banner);
        //设置banner样式
        banner.setBannerStyle(BannerConfig.CIRCLE_INDICATOR_TITLE);
        //设置图片加载器
        banner.setImageLoader(new GlideImageLoader());
        //设置图片集合
        banner.setImages(images);
        //设置banner动画效果
        banner.setBannerAnimation(Transformer.DepthPage);
        //设置标题集合(当banner样式有显示title时)
        banner.setBannerTitles(titles);
        //设置自动轮播,默认为true
        banner.isAutoPlay(true);
        //设置轮播时间
        banner.setDelayTime(2500);
        //设置指示器位置(当banner模式中有指示器时)
        banner.setIndicatorGravity(BannerConfig.RIGHT);
        //banner设置方法全部调用完毕时最后调用
        banner.start();

        swipeRefreshLayout = view.findViewById(R.id.swipe_refresh);
    }

    private Handler handle = new Handler() {
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 0:
                    Document doc = (Document)msg.obj;
                    //
                    Elements element1 = doc.select("div.hide");
                    Elements element2 = element1.select("ul.ul_12_2");
                    Elements element3 = element2.select("li");
                    link = element3.select("a");
                    title = element3.select("a");

                    news_Title.clear();
                    news_Link.clear();

                    for(int i=0;i<news_list_size;i++)
                    {
                        String newsTitle = title.get(i).text();
                        String newsLink = link_head + link.get(i).attr("href"); //获取a标签中的href属性的值

                        //清除之前的数据,否则新闻“刷新”之后直接在之前的新闻之后添加了
                        news_Title.add(newsTitle);
                        news_Link.add(newsLink);
                    }
                    mRecyclerView.setAdapter(new RecyclerAdapter(mRecyclerView.getContext(), news_Title, news_Link));
                    swipeRefreshLayout.setRefreshing(false);
            }
        }
    };

    Runnable getNews = new Runnable() {
        @Override
        public void run() {
            try
            {
                doc = Jsoup.connect(news_target).get();
                Message msg = new Message();
                msg.what = 0;
                msg.obj = doc;
                handle.sendMessage(msg);
            }
            catch(MalformedURLException e)
            {
                System.out.println(e);
            }
            catch(IOException e)
            {
                System.out.println(e);
            }
        }
    };

}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值