加载10个项目乘10个Recyclerview项目

时间:2018-12-04 16:24:11

标签: android arraylist android-recyclerview pagination android-glide

JSON 文件中我几乎得到了(200-300个图像URL),我正在尝试按10张图像加载10张图像,以使图像快速加载,而不是全部加载图像(200- 300)。但是我有一个问题case在前10个项目后停止滚动,这是我尝试过的操作:

InfiniteScrollListener.java

RecyclerView

设置适配器的方法:

 private void initInfiniteScrollListener(LinearLayoutManager mLayoutManager) {
        infiniteScrollListener = new InfiniteScrollListener(mLayoutManager) {
            @Override
            public void onLoadMore(int page) {
                pb.setVisibility(View.VISIBLE);
                refreshContent();

            }
        };
        recyclerView.addOnScrollListener(infiniteScrollListener);
    }

获取数据:

private void refreshContent() {
    fetchRemoteData(new DataStatus() {
        @Override
        public void onSuccess(List<Post> posts) {
            loading.setVisibility(View.GONE);
            pb.setVisibility(View.GONE);
            if (adapter == null) {
                adapter = new MyAdapter(posts, getActivity(), new RecyclerViewClickListener() {
                    @Override
                    public void onClick(View view, Post post) {
                    }
                });
                recyclerView.setAdapter(adapter);
            } else {
                int position = adapter.getItemCount();
                adapter.getItems().addAll(posts);
                adapter.notifyItemRangeInserted(position, position + 10);
            }
        }

        @Override
        public void onError(Exception e) {
            slowinternetconnection();
        }
    });
}

我想要的是:

例如,从250张图像中,我希望每次用户滚动recyclerview时都加载10张图像,直到完成250张图像为止

1 个答案:

答案 0 :(得分:0)

  private void populateData() {
    int i = 0;
    while (i < 10) {
        rowsArrayList.add("Item " + i);
        i++;
    }
 }

  private void initAdapter() {

    recyclerViewAdapter = new RecyclerViewAdapter(rowsArrayList);
    recyclerView.setAdapter(recyclerViewAdapter);
}

private void initScrollListener() {
    recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
        @Override
        public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
            super.onScrollStateChanged(recyclerView, newState);
        }

        @Override
        public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
            super.onScrolled(recyclerView, dx, dy);

            LinearLayoutManager linearLayoutManager = (LinearLayoutManager) recyclerView.getLayoutManager();

            if (!isLoading) {
                if (linearLayoutManager != null && linearLayoutManager.findLastCompletelyVisibleItemPosition() == rowsArrayList.size() - 1) {
                    //bottom of list!
                    loadMore();
                    isLoading = true;
                }
            }
        }
    });


}

private void loadMore() {
    rowsArrayList.add(null);
    recyclerViewAdapter.notifyItemInserted(rowsArrayList.size() - 1);


    Handler handler = new Handler();
    handler.postDelayed(new Runnable() {
        @Override
        public void run() {
            rowsArrayList.remove(rowsArrayList.size() - 1);
            int scrollPosition = rowsArrayList.size();
            recyclerViewAdapter.notifyItemRemoved(scrollPosition);
            int currentSize = scrollPosition;
            int nextLimit = currentSize + 10;

            while (currentSize - 1 < nextLimit) {
                rowsArrayList.add("Item " + currentSize);
                currentSize++;
            }

            recyclerViewAdapter.notifyDataSetChanged();
            isLoading = false;
        }
    }, 2000);


}

在适配器类中

公共类RecyclerViewAdapter扩展了RecyclerView.Adapter {

private final int VIEW_TYPE_ITEM = 0;
private final int VIEW_TYPE_LOADING = 1;

public List<String> mItemList;


public RecyclerViewAdapter(List<String> itemList) {

    mItemList = itemList;
}

@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    if (viewType == VIEW_TYPE_ITEM) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_row, parent, false);
        return new ItemViewHolder(view);
    } else {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_loading, parent, false);
        return new LoadingViewHolder(view);
    }
}

@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int position) {

    if (viewHolder instanceof ItemViewHolder) {

        populateItemRows((ItemViewHolder) viewHolder, position);
    } else if (viewHolder instanceof LoadingViewHolder) {
        showLoadingView((LoadingViewHolder) viewHolder, position);
    }

}

@Override
public int getItemCount() {
    return mItemList == null ? 0 : mItemList.size();
}

/**
 * The following method decides the type of ViewHolder to display in the RecyclerView
 *
 * @param position
 * @return
 */
@Override
public int getItemViewType(int position) {
    return mItemList.get(position) == null ? VIEW_TYPE_LOADING : VIEW_TYPE_ITEM;
}


private class ItemViewHolder extends RecyclerView.ViewHolder {

    TextView tvItem;

    public ItemViewHolder(@NonNull View itemView) {
        super(itemView);

        tvItem = itemView.findViewById(R.id.tvItem);
    }
}

private class LoadingViewHolder extends RecyclerView.ViewHolder {

    ProgressBar progressBar;

    public LoadingViewHolder(@NonNull View itemView) {
        super(itemView);
        progressBar = itemView.findViewById(R.id.progressBar);
    }
}

private void showLoadingView(LoadingViewHolder viewHolder, int position) {
    //ProgressBar would be displayed

}

private void populateItemRows(ItemViewHolder viewHolder, int position) {

    String item = mItemList.get(position);
    viewHolder.tvItem.setText(item);

}

查看此链接 https://www.journaldev.com/24041/android-recyclerview-load-more-endless-scrolling

您可以先加载10个项目,然后刷新并加载下一个10。