I have a Recyclerview and a list with 5000 items, but I only want to load 100 items at a time

I applied Recyclerview in my test application. Currently, I am pulling about 5,000 data elements into a model and loading my view into this list. Everything is fine, and it works great, but I don’t really want to load all 5000 items. I would prefer to load 100 items, and as soon as the user hits from below, load the next 100 and essentially make it an ever-growing list.

I can implement onScrollListener compared to Recyclerview to perform detection when I get to the end, but my problem (as simple as it sounds) is that the best way to tell Recyclerview load is only 100 until I say?

My adapter:

 public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> { private List<ImageFeed> mFeedList; public MyAdapter(List<ImageFeed> feedList) { this.mFeedList = feedList; } public void setFeedList(List<ImageFeed> feedList) { mFeedList = feedList; } @Override public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup vGroup, int i) { View v = LayoutInflater.from(vGroup.getContext()).inflate(R.layout.recycler_list, vGroup, false); return new MyAdapter.ViewHolder(v); } @Override public void onBindViewHolder(final MyAdapter.ViewHolder viewHolder, int i) { String mTitle = mFeedList.get(i).getTitle(); ...//Do other stuff } @Override public int getItemCount() { return mFeedList.size(); } public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener { protected ImageView mImageView; protected TextView mTitle; protected ProgressBar mLoader; private int mItemId; public ViewHolder(View itemView) { super(itemView); this.mImageView = (ImageView) itemView.findViewById(R.id.imgItem); this.mTitle = (TextView) itemView.findViewById(R.id.txtTitle); this.mLoader = (ProgressBar) itemView.findViewById(R.id.mLoaderProgress); this.mImageView.setOnClickListener(this); itemView.setOnClickListener(this); } public void setItem(int item) { this.mItemId = item; } @Override public void onClick(View v) { if (v instanceof ImageView) { Toast.makeText(v.getContext(), "Image view clicked: " + this.mItemId, Toast.LENGTH_SHORT).show(); } else { Toast.makeText(v.getContext(), this.mItemId + " ", Toast.LENGTH_SHORT).show(); } } } } 

Reflecting on the script, I would think that a transition to a smaller list is possible, but it does not feel completely right.

+5
source share
1 answer

Okay, so it wasn’t particularly difficult - I think my main problem was what I was thinking about the script ...

So, the first thing I do is to get the relevant data (in this case via JSON) and save it in a JSON array for manipulation later - at the moment I am still getting the complete set (Ie 5000), but it is easily replaced.

This is done through AsyncTask with an undefined progress bar. After its completion, I parse JSON to 20 elements and load them into the adapter.

As soon as I finished, I have a Recyclerview.onScrollListener ....

 mRecyclerView.setOnScrollListener(new RecyclerView.OnScrollListener() { @Override public void onScrolled(RecyclerView recyclerView, int dx, int dy) { super.onScrolled(recyclerView, dx, dy); mOnScreenItems = mRecyclerView.getChildCount(); mTotalItemsInList = llm.getItemCount(); mFirstVisibleItem = llm.findFirstVisibleItemPosition(); if (mLoadingItems) { if (mTotalItemsInList > mPreviousTotal) { mLoadingItems = false; mPreviousTotal = mTotalItemsInList; } } if (!mLoadingItems && (mTotalItemsInList - mOnScreenItems) <= (mFirstVisibleItem + mVisibleThreshold)) { new AsyncLoadTask().execute(); mLoadingItems = true; } } @Override public void onScrollStateChanged(RecyclerView recyclerView, int newState) { super.onScrollStateChanged(recyclerView, newState); } }); 

When it approaches the end or falls to the bottom, a new AsynTask is launched to load additional items into the list. Then onPostExecute() updates the adapter using the mMyAdapter.notifyDataSetChanged(); method mMyAdapter.notifyDataSetChanged(); that gives a nice smooth update. Many utilities to do, but the basics exist.

Thanks for the guys guidance!

+6
source

Source: https://habr.com/ru/post/1214095/


All Articles