Recyclerview:在Blogger API中加载更多内容并不平滑

时间:2018-03-03 13:44:14

标签: android android-recyclerview

我已成功使用Blogger的API获取帖子的下一页,但我无法顺利滚动recyclelerview,看起来很落后。

我尝试使用recyclerViewData.setNestedScrollingEnabled(false)adapter.notifyDataSetChanged(),但仍然无法使用。

我的观点是如何实现reclerview的顺利进行。任何建议将不胜感激。谢谢是提前。

这是我的代码

private void getData() {
            showLoading(true);
            final Call<ResponseBlogPost> postList = apiService.getListPost(GlobalVariable.APP_KEY);
            postList.enqueue(new Callback<ResponseBlogPost>() {
                @Override
                public void onResponse(Call<ResponseBlogPost> call, Response<ResponseBlogPost> response) {
                    showLoading(false);
                    ResponseBlogPost responseBlogPost = response.body();
                    initDataView(responseBlogPost);
                }

                @Override
                public void onFailure(Call<ResponseBlogPost> call, Throwable t) {
                    showLoading(false);
                    Toast.makeText(MainActivity.this, t.getMessage(), Toast.LENGTH_SHORT).show();
                }
            });
        }

private void initDataView(ResponseBlogPost listpost){
            GlobalFunction.saveString(this,GlobalVariable.TOKEN_PAGINATION, listpost.getNextPageToken());
            final String nextPageToken = GlobalFunction.getStrings(this, GlobalVariable.TOKEN_PAGINATION);
            itemsList.addAll(listpost.getItems());
            adapter = new MainAdapter(itemsList) {
                @Override
                public void load() {
                    if(nextPageToken==null){
                        return;
                    }
                    getNextListPost();
                }
            };

            recyclerViewData.setAdapter(adapter);
            recyclerViewData.setHasFixedSize(true);
            staggeredGridLayoutManager = new StaggeredGridLayoutManager(2,1);
            staggeredGridLayoutManager.setGapStrategy(0);
            recyclerViewData.setLayoutManager(staggeredGridLayoutManager);
        }


private void getNextListPost(){
            showLoading(true);
            final String nextPageToken = GlobalFunction.getStrings(this, GlobalVariable.TOKEN_PAGINATION);
            Call<ResponseBlogPost>  call = apiService.getNexPageListPost(GlobalVariable.APP_KEY,nextPageToken);
            call.enqueue(new Callback<ResponseBlogPost>() {
                @Override
                public void onResponse(Call<ResponseBlogPost> call, Response<ResponseBlogPost> response) {
                    showLoading(false);
                    ResponseBlogPost responseModel = response.body();
                    if(nextPageToken!=null){
                        initDataView2(responseModel);
                    }else{
                        Toast.makeText(MainActivity.this, "tidak ada data lagi ya", Toast.LENGTH_SHORT).show();
                    }
                }

                @Override
                public void onFailure(Call<ResponseBlogPost> call, Throwable t) {
                    showLoading(false);
                    Toast.makeText(MainActivity.this, t.getMessage(), Toast.LENGTH_SHORT).show();
                }
            });
        }


private void initDataView2(ResponseBlogPost listpost){
            GlobalFunction.saveString(this,GlobalVariable.TOKEN_PAGINATION, listpost.getNextPageToken());
            final String nextPageToken = GlobalFunction.getStrings(this, GlobalVariable.TOKEN_PAGINATION);
            itemsList.addAll(listpost.getItems());
            adapter = new MainAdapter(itemsList) {
                @Override
                public void load() {
                    if(nextPageToken!=null){
                        getNextListPost();
                    }
                }
            };

            recyclerViewData.setAdapter(adapter);
            recyclerViewData.setHasFixedSize(true);
            staggeredGridLayoutManager = new StaggeredGridLayoutManager(2, 1);
            staggeredGridLayoutManager.setGapStrategy(0);
            recyclerViewData.setLayoutManager(staggeredGridLayoutManager);

}

我的适配器代码:

public abstract class MainAdapter extends RecyclerView.Adapter<MainAdapter.MainViewHolder>{

    private List<BlogPostModel> responseBlogPost;

    public MainAdapter(List<BlogPostModel> responseBlogPost) {
        this.responseBlogPost = responseBlogPost;
    }

    public abstract void load();

    @Override
    public MainViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_main,parent,false);
        return new MainViewHolder(view);
    }

    @Override
    public void onBindViewHolder(final MainViewHolder holder, int position) {

        final BlogPostModel model = responseBlogPost.get(position);
        ArrayList<String> urlImage = pullLinks(model.getContent());
        String firstImage = "";
        for (int i = 0; i < urlImage.size(); i++) {
            firstImage = urlImage.get(1);
            GlideCustomLoading.setImageFromURL(holder.itemView.getContext(), urlImage.get(0)
                    ,holder.avLoadingIndicatorView, holder.ivItemPost,holder.tvFailedLoadImage);
        }

        holder.tvTitleItemPost.setText(model.getTitle());
        final String finalFirstImage = firstImage;
        holder.llItem.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String id = String.valueOf(model.getId());
                DetailPostActivity.start(holder.itemView.getContext(), id, finalFirstImage, model.getTitle(), model.getUrl());
            }
        });


        if(position>=getItemCount()-1){
            load();
        }
    }

    @Override
    public int getItemCount() {
        return responseBlogPost.size();
    }

    public class MainViewHolder extends RecyclerView.ViewHolder{

        CardView cardViewItemPost;
        ImageView ivItemPost;
        TextView tvTitleItemPost;
        AVLoadingIndicatorView avLoadingIndicatorView;
        TextView tvFailedLoadImage;
        LinearLayout llItem;

        public MainViewHolder(View itemView) {
            super(itemView);

            cardViewItemPost = itemView.findViewById(R.id.cardview_item_post);
            ivItemPost =  itemView.findViewById(R.id.iv_image_post);
            tvTitleItemPost = itemView.findViewById(R.id.tv_title_post);
            avLoadingIndicatorView = itemView.findViewById(R.id.avi_load);
            tvFailedLoadImage = itemView.findViewById(R.id.tv_gagal_menampilkan_gambar);
            llItem = itemView.findViewById(R.id.ll_item_post);

        }
    }

    private ArrayList pullLinks(String html) {
        ArrayList links = new ArrayList();
        Elements srcs = Jsoup.parse(html).select("[src]"); //get All tags containing "src"
        for (int i = 0; i < srcs.size(); i++) {
            links.add(srcs.get(i).attr("abs:src")); // get links of selected tags
        }
        return links;
    }

}

1 个答案:

答案 0 :(得分:0)

虽然我不熟悉Jsoup,但我有理由相信它是你的滚动延迟的来源。

您在pullLinks方法中调用方法onBindViewHolder,每次将新视图添加到RecyclerView时都会调用该方法。我假设pullLinks做了一些非常密集的工作,需要一些时间才能完成。

您最好做的是将所有数据添加到responseBlogPost列表中的每个项目,然后再将其加载到适配器中。