我有一個RecyclerView包含交互式內容的項目。 每個項目都包含一個每5秒更新一次的文本視圖,但是當RecyclerView中有5個以上的項目時,數據會混淆在一起。RecyclerView項目內容混合起來
例如:
正確: | 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);
}
}
}
張貼一些代碼.. –
張貼您的適配器類請 – Jeffalee
@RohitArya完成! –