我有一个包含具有互动内容的项目的RecyclerView。 每个项目都包含一个每5秒更新一次的文本视图,但当RecyclerView中有超过5个项目时,数据会混淆。
以示例:
正确: | 1 - 100 |
| 2 - 283 |
| 3 - 382 |
当它混淆时:
| 2 - 283 |
| 1 - 100 |
| 3 - 382 |
它有点像,它只是混淆了。
当我调用de adapter.notifyDataSetChanged();
时会发生这种情况我的适配器:
公共类FavoritesAdapter扩展了RecyclerView.Adapter {
private List<Favorites> channels;
private Context context;
public FavoritesAdapter(Context context, List<Favorites> channels) {
this.context = context;
this.channels = channels;
}
@Override
public FavoritesViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.favoriteitem_layout, viewGroup, false);
FavoritesViewHolder pvh = new FavoritesViewHolder(v);
return pvh;
}
@Override
public void onBindViewHolder(final FavoritesViewHolder holder, final int position) {
final DBHelper dbHelper = new DBHelper(context);
if (Data.isConnected(context)) {
Scheduler.getInstance().doAsync(new Scheduler.Task<Object, Channel>() {
@Override
public Channel run(Object... params) {
return Methods.getData(channels.get(position).getChannelId(), channels.get(position).getChannelName(), true, context);
}
}, new Scheduler.Executable<Channel>() {
@Override
public void execute(Channel result) {
if(result.getId() != "") {
if(!result.channelSubs.equalsIgnoreCase("-500_RTSS_ERROR")) {
holder.channelSubsView.setText(result.getSubs());
holder.channelTitleSubsView.setText(String.valueOf(channels.get(position).getChannelName()) + "'s Suscribers");
channels.get(position).setChannelSubs(result.getSubs());
dbHelper.updateFavorites(channels.get(position));
}
else {
holder.channelSubsView.setText(channels.get(position).getChannelSubs());
holder.channelTitleSubsView.setText(String.valueOf(channels.get(position).getChannelName()) + "'s Suscribers (Cached)");
}
}
else {
holder.channelSubsView.setText(channels.get(position).getChannelSubs());
holder.channelTitleSubsView.setText(String.valueOf(channels.get(position).getChannelName()) + "'s Suscribers (Cached)");
}
}
});
}
else {
holder.channelSubsView.setText(channels.get(position).getChannelSubs());
holder.channelTitleSubsView.setText(String.valueOf(channels.get(position).getChannelName()) + "'s Suscribers (Cached)");
}
Picasso.with(context).load(channels.get(position).getChannelAvatar())
.error(R.drawable.youtube_default_avatar)
.placeholder(R.drawable.youtube_default_avatar)
.into(holder.channelAvatarView);
holder.channelCardView.setOnClickListener(clickListener);
holder.channelCardView.setTag(holder);
}
View.OnClickListener clickListener = new View.OnClickListener() {
@Override
public void onClick(View view) {
FavoritesViewHolder holder = (FavoritesViewHolder) view.getTag();
int position = holder.getPosition();
Favorites channel = channels.get(position);
if(Data.isConnected(context)) {
Methods.getYouTubeData(channel.getChannelId(), channel.getChannelName(), channel.getChannelAvatar(), context);
}
else {
Toast.makeText(context, R.string.error_connection_no_internet, Toast.LENGTH_LONG).show();
}
}
};
@Override
public int getItemCount() {
return channels.size();
}
@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
}
public static class FavoritesViewHolder extends RecyclerView.ViewHolder {
CardView channelCardView;
TextView channelSubsView;
TextView channelTitleSubsView;
ImageView channelAvatarView;
FavoritesViewHolder(View itemView) {
super(itemView);
channelCardView = (CardView) itemView.findViewById(R.id.channelCardView);
channelSubsView = (TextView) itemView.findViewById(R.id.subsAmount);
channelTitleSubsView = (TextView) itemView.findViewById(R.id.subsText);
channelAvatarView = (ImageView) itemView.findViewById(R.id.channelAvatarView);
}
}
}
答案 0 :(得分:0)
您可以检查是否正在编辑传递给适配器构造函数的初始列表的顺序/排序。
List<Favorites> channels = getChannels();
adapter = new FavoritesAdapter(context, channels);
recyclerView.setAdapter(adapter)
// edit or sort *channels*
adapter.notifyDataSetChanged();
答案 1 :(得分:0)
您正在asynchronous
进行onBindViewHolder
来电,因此当异步任务的响应出现时,它会从public void onBindViewHolder(final FavoritesViewHolder holder, final int position)
读取错误的位置,因为到那时onBindViewHolder
返回的排名已更改。
您可以做的是,将位置作为参数传递给async task
,并在async task
的结果中传递相同的位置。这样,对于位置 p (比如说)的调用将应用于 p 。
除此之外,还尝试在RecyclerViewHolder上使用getAdapterPosition
代替getPosition
(不赞成并造成混淆)。