我想每 30 秒更新一次 RecyclerView 视图的视图。我使用这个语句:
adapter.notifyItemRangeChanged(0, channelsInGroup.size());
它不起作用。我可以通过将内容复制到新的arraylist,将新的arraylist传递给适配器,然后清空新的arraylist并在更新后重新填充它,最后调用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());
如何在不传递数组列表副本的情况下更新 RecyclerView 中的视图?我想使用g.getChannels()定期更新的原始版本。
相关代码片段:
@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();
}