I want to update the views of my RecyclerView view every 30 seconds. I use the this statement:
adapter.notifyItemRangeChanged(0, channelsInGroup.size());
and it does not work. I could get it working by copying the contents to a new arraylist, passing the new arraylist to the adapter, and then emptying
the new arraylist and repopulating it after the update, and finally calling notifyItemRangeChanged
:
channelsInGroup.clear();
g = channelsLab.getGroupByTitle(mGroupTitle)
List<Channel> channelListNew = new ArrayList<Channel>();
for (Channel c : g.getChannels()) {
channelListNew.add(c);
}
channelsInGroup.addAll(channelListNew);
adapter.notifyItemRangeChanged(0, channelsInGroup.size());
How can I update the views in the RecyclerView without having to pass a copy of the arraylist? I want to use the original g.getChannels()
which gets updated periodically.
Related code snippets:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
mRootView = inflater.inflate(R.layout.fragment_channel_epg, container, false);
RecyclerView recyclerView = mRootView.findViewById(R.id.recyclerView);
LinearLayoutManager horizontalLayoutManagaer
= new LinearLayoutManager(getContext(), LinearLayoutManager.HORIZONTAL, false);
recyclerView.setLayoutManager(horizontalLayoutManagaer);
adapter = new MyRecyclerViewAdapter(getContext(), channelsInGroup);
recyclerView.setAdapter(adapter);
return mRootView;
}
public class MyRecyclerViewAdapter extends RecyclerView.Adapter<MyRecyclerViewAdapter.ViewHolder> {
private List<Channel> mChannelsList;
private LayoutInflater mInflater;
MyRecyclerViewAdapter(Context context, List<Channel> channelList) {
this.mInflater = LayoutInflater.from(context);
this.mChannelsList = channelList;
}
@Override
@NonNull
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = mInflater.inflate(R.layout.epg_row_of_playing_channel, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
if (mChannelsList.get(position).getProgrammeListNext24Hours() != null) {
holder.myTextView.setText(mChannelsList.get(position).getProgrammeListNext24Hours().get(0).getTitle().get(0));
}
else {
holder.myTextView.setText("yok");
}
}
@Override
public int getItemCount() {
return mChannelsList.size();
}
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
TextView myTextView;
ViewHolder(View itemView) {
super(itemView);
myTextView = itemView.findViewById(R.id.programme_description_in_epg_bar);
}
}
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
channelsLab = ChannelsLab.get(BKD_ChannelEPGFragment.this.getContext());
g = channelsLab.getGroupByTitle(mGroupTitle);
channelsInGroup = g.getChannels();
}