【问题标题】:Unable to scroll until fully recycler loaded在完全加载回收站之前无法滚动
【发布时间】:2020-05-09 17:38:47
【问题描述】:

您好,在加载回收站视图列表项时我无法滚动。

每当我滚动它时,我都会把它推到 recyclerview 的顶部,直到它被加载为什么?请帮忙

第一个 url 加载后我的进度条 GONE 应该在所有 6 个 URL 都加载后消失

‏‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎‏‎ ‏ ‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎ ‎

    Executor ex = Executors.newFixedThreadPool(1);
        String lstUrls = "www.example.com/rss1\n"
+"www.example.com/rss2\n"
+"www.example.com/rss3\n"
+"www.example.com/rss4\n"
+"www.example.com/rss5\n"
+"www.example.com/rss6\n";

        String[] urls = lstUrls.split("\n");
        final ArrayList<News> listOfNews = new ArrayList<>();
        for (final String url : urls) {
            try {
                ex.execute(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            Document doc = Jsoup
                                    .connect(url)
                                    .header("Accept-Encoding", "gzip, deflate")
                                    .userAgent("Mozilla/5.0 (Windows NT 6.2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.69 Safari/537.36")
                                    .maxBodySize(0)
                                    .timeout(600000)
                                    .ignoreContentType(true)
                                    .get();

                            Elements itemElements = doc.getElementsByTag("item");
                            for (int i = 0; i < itemElements.size(); i++) {
                                Element item = itemElements.get(i);
                                String title = item.child(0).text();
                                String link = item.child(1).text();
                                String pubDate = item.child(3).text();
                                String src = item.child(5).text();

                                String titlee = title.substring(0, title.lastIndexOf("-"));

                                Date date = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss zzz").parse(pubDate);
                                String newString = new SimpleDateFormat("dd MMM yyyy, HH:mm").format(date);



                                System.out.println("Article\r-\r" + (i) + "\n");
                                System.out.println(titlee);
                                System.out.println(link);
                                System.out.println(newString);
                                System.out.println(src);
                                listOfNews.add(new News(titlee,newString,src,link));

                                mutableLiveNews.postValue(listOfNews);
                                views.setVisibility(View.GONE);
                            }

                            Collections.sort(listOfNews, new Comparator<News>() {
                                @Override
                                public int compare(News lhs, News rhs) {
                                    SimpleDateFormat formatter = new SimpleDateFormat("dd MMM yyyy, HH:mm");

                                    try {
                                        Date date1 = formatter.parse(rhs.getTime());
                                        Date date2 = formatter.parse(lhs.getTime());

                                        return date1.compareTo(date2);

                                    } catch (ParseException e) {
                                        e.printStackTrace();
                                    }
                                    return 0;
                                }
                            });

                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                });
            } catch (Exception e) {
                e.printStackTrace();
            }

        }


        mutableLiveNews.observe(this,
                new Observer<List<News>>() {
                    @Override
                    public void onChanged(List<News> news) {
                        RecyclerView recyclerView = view.findViewById(R.id.rv_news);
                        RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getActivity());
                        recyclerView.setLayoutManager(layoutManager);

                        RecyclerView.Adapter mAdapter = new SampleAdapter(getContext(),news);
                        recyclerView.setAdapter(mAdapter);
                        mAdapter.notifyDataSetChanged();
                        progressBar.setVisibility(View.GONE);


                    }
                });

【问题讨论】:

    标签: android android-studio android-layout android-recyclerview


    【解决方案1】:

    因为您每次在 onChanged() 方法中加载新项目时都会创建新的 recyclerView。您必须实例化 recyclereView 一次,每次加载新项目时只需调用 adapter.notifyItemInserted();adapter.notifyItemRangeInserted(); 之一 并且你必须检查它是否是最后一项然后调用progressBar.setVisibility(View.GONE);

    【讨论】:

    猜你喜欢
    • 2017-02-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-03
    • 2019-05-05
    相关资源
    最近更新 更多