Interview Question for Android Developer - SDE1 at Slice
Home
Refer
Jobs
Alumni
Resume
Notifications

Implement a RecyclerView with an endless scrolling feature that loads data from an API that retrieves data in batches of 10 items at a time. Additionally, ensure that the fetched data is cached for offline use and that there are no duplicate items displayed on the screen.

🚀 Best Answers Get Featured in our LinkedIn Community based on Your Consent, To Increase Your Chances of Getting Interviewed. 🚀

To implement a RecyclerView with an endless scrolling feature that loads data from an API in batches of 10 items, while ensuring that the fetched data is cached for offline use and no duplicate items are displayed, follow the given steps:1. Import the RecyclerView library in the project gradle file.`implementation 'androidx.recyclerview:recyclerview:1.2.0'`2. Create a RecyclerView layout in XML and add it to the main layout file.
```xml
```3. Create a custom adapter by extending the RecyclerView.Adapter class. Override the onCreateViewHolder() to inflate the row layout, and onBindViewHolder() to set the data to the row items.
```javapublic class CustomAdapter extends RecyclerView.Adapter {
private List dataList;
public CustomAdapter(List dataList) {
this.dataList = dataList;
}
@Override public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_row, viewGroup, false);
return new ViewHolder(view);
}
@Override public void onBindViewHolder(ViewHolder viewHolder, int i) {
Data data = dataList.get(i);
viewHolder.textView.setText(data.getText());
}
@Override public int getItemCount() {
return dataList.size();
}
public class ViewHolder extends RecyclerView.ViewHolder{
TextView textView;
public ViewHolder(View itemView) {
super(itemView);
textView = itemView.findViewById(R.id.text_view);
}
}
}

```4. Initialize the RecyclerView in the activity and set the custom adapter to it.
```javarecyclerView = findViewById(R.id.recycler_view);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
customAdapter = new CustomAdapter(dataList);
recyclerView.setAdapter(customAdapter);

```5. Implement the endless scrolling feature by adding a RecyclerView.OnScrollListener and overriding the onScrolled() method. In the method, check if the last visible item position is nearing the end of the dataset and call the API to load the next batch of data.
```javarecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
LinearLayoutManager linearLayoutManager = (LinearLayoutManager) recyclerView.getLayoutManager();
if (!isLoading && !isLastPage) {
if (linearLayoutManager != null && linearLayoutManager.findLastCompletelyVisibleItemPosition() == dataList.size() - 1) {
loadMore();
isLoading = true;
}
}
}
}
);

```6. Implement caching using libraries such as OkHttp or Glide. These libraries automatically check if the data is available in cache and fetch it from there if it is. If not, they fetch the data from the server and cache it for future use.
```javaOkHttpClient client = new OkHttpClient.Builder() .cache(new Cache(context.getCacheDir(), cacheSize)) .build();
Request request = new Request.Builder() .url(apiUrl) .build();
client.newCall(request).enqueue(new Callback() {
@Override public void onFailure(Call call, IOException e) {
e.printStackTrace();
}
@Override public void onResponse(Call call, Response response) {
ResponseBody responseBody = response.body();
if (responseBody == null) return;
String result = responseBody.string();
// parse the JSON response // add data to the adapter // notify adapter of data change }
}
);

```7. To ensure no duplicate items are displayed, use a Set to keep track of unique items and check if the item already exists before adding it to the dataset.
```javaSet dataSet = new HashSet<>();
// code to load and add data to the adapterif (dataSet.add(data)) {
dataList.add(data);
customAdapter.notifyItemInserted(dataList.size() - 1);
}

```Some useful resources for RecyclerView and caching:- [Android Developers Guide on RecyclerView](https://developer.android.com/guide/topics/ui/layout/recyclerview)- [CodePath Guide on Endless Scrolling with RecyclerView](https://guides.codepath.com/android/Endless-Scrolling-with-AdapterViews-and-RecyclerView)- [OkHttp Caching Guide](https://square.github.io/okhttp/4.x/okhttp/okhttp3/-cache/)

© 2024 Referral Solutions, Inc. Incorporated. All rights reserved.