-2

我想每 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();

}
4

2 回答 2

1

If you want to animate the changes between both the old and new list then you could use DiffUtil without needing to call any notify methods. Create a class that extends DiffUtil.Callback like below.

public class MyDiffCallback extends DiffUtil.Callback {

    List<Channel> oldList;
    List<Channel> newList;

    MyDiffCallback(List<Channel> oldList, List<Channel> newList) {
        oldList = this.oldList;
        newList = this.newList;
    }

    public int getOldListSize() {
        return oldList.size;
    }

    public int getNewListSize() {
        return newList.size;
    }

    @Override
    public boolean areItemsTheSame(int oldItemPosition, int newItemPosition) {
        // If you have an id for the data then it should be used here.
        return oldList.get(oldItemPosition).id == newList.get(newItemPosition).id;
    }

    @Override
    public boolean areContentsTheSame(int oldItemPosition, int newItemPosition) {
        // Replace id with whatever is appropriate to check if the contents are the same.
        return oldList.get(oldItemPosition).id  == newList.get(newItemPosition).id;
    }
}

Then create a method inside the adapter to calculate the differences and swap the data.

public void swap(List<Channel> channelList) {
    MyDiffCallback diffCallback = new MyDiffCallback(mChannelsList, channelList);
    DiffUtil.DiffResult diffResult = DiffUtil.calculateDiff(diffCallback);
    mChannelsList.clear();
    mChannelsList.addAll(channelList);
    diffResult.dispatchUpdatesTo(this);
}

Now you can do adapter.swap(newList), where newList should include the old and new data.

To add new items to the list without including the old data, just remove the mChannelsList.clear() from swap() and give it the new data you want added to the list.

于 2018-08-17T01:23:57.507 回答
0

打电话adapter.notifyDataSetChanged()

于 2018-08-16T23:55:48.007 回答