Java RecyclerView项目内容弄混了
我有一个RecyclerView,其中包含具有交互内容的项目。 每个项目都包含一个每5秒更新一次的textview,但是当RecyclerView中有5个以上的项目时,数据就会混淆 举例来说: 正确: |1-100| |2-283| |3-382| 当它被混淆时: |2-283| |1-100| |3-382| 有点像这样,只是搞混了 调用de adapter.notifyDataSetChanged()时会发生这种情况 我的适配器: 公共类FavoritesAdapter扩展了RecyclerView.Adapter{Java RecyclerView项目内容弄混了,java,android,xml,android-recyclerview,Java,Android,Xml,Android Recyclerview,我有一个RecyclerView,其中包含具有交互内容的项目。 每个项目都包含一个每5秒更新一次的textview,但是当RecyclerView中有5个以上的项目时,数据就会混淆 举例来说: 正确: |1-100| |2-283| |3-382| 当它被混淆时: |2-283| |1-100| |3-382| 有点像这样,只是搞混了 调用de adapter.notifyDataSetChanged()时会发生这种情况 我的适配器: 公共类FavoritesAdapter扩展了Recycler
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);
}
}
私有列表频道;
私人语境;
公共收藏夹适配器(上下文、列表频道){
this.context=上下文;
这个。通道=通道;
}
@凌驾
公共收藏夹ViewHolder onCreateViewHolder(ViewGroup ViewGroup,int viewType){
视图v=LayoutInflater.from(viewGroup.getContext()).flate(R.layout.favoriteitem_布局,视图组,false);
FavoriteSvieworlder pvh=新的FavoriteSvieworlder(v);
返回pvh;
}
@凌驾
BindViewHolder上的公共无效(最终收藏夹ViewHolder,最终int位置){
final DBHelper DBHelper=新的DBHelper(上下文);
if(数据断开连接(上下文)){
Scheduler.getInstance().doAsync(新的Scheduler.Task()){
@凌驾
公共频道运行(对象…参数){
返回方法.getData(channels.get(position).getChannelId(),channels.get(position).getChannelName(),true,context);
}
},new Scheduler.Executable(){
@凌驾
公共无效执行(通道结果){
如果(result.getId()!=“”){
如果(!result.channelSubs.equalsIgnoreCase(“-500_RTSS_ERROR”)){
holder.channelsubview.setText(result.getSubs());
holder.ChannelTitleSubView.setText(String.valueOf(channels.get(position.getChannelName())+“”的使用者);
channels.get(position).setChannelSubs(result.getSubs());
updateFavorites(channels.get(position));
}
否则{
holder.channelsubview.setText(channels.get(position.getChannelSubs());
holder.channelTitleSubsView.setText(String.valueOf(channels.get(position).getChannelName())+“'s Suscribers(缓存)”;
}
}
否则{
holder.channelsubview.setText(channels.get(position.getChannelSubs());
holder.channelTitleSubsView.setText(String.valueOf(channels.get(position).getChannelName())+“'s Suscribers(缓存)”;
}
}
});
}
否则{
holder.channelsubview.setText(channels.get(position.getChannelSubs());
holder.channelTitleSubsView.setText(String.valueOf(channels.get(position).getChannelName())+“'s Suscribers(缓存)”;
}
Picasso.with(context).load(channels.get(position.getChannelAvatar())
.error(R.drawable.youtube\u default\u avatar)
.placeholder(R.drawable.youtube\u default\u avatar)
.进入(保持器通道视图);
holder.channelCardView.setOnClickListener(单击Listener);
holder.channelCardView.setTag(holder);
}
View.OnClickListener clickListener=新建视图。OnClickListener(){
@凌驾
公共void onClick(视图){
FavoritesViewHolder持有者=(FavoritesViewHolder)视图。getTag();
int position=holder.getPosition();
收藏夹频道=频道。获取(位置);
if(数据断开连接(上下文)){
方法.getYouTubeData(channel.getChannelId(),channel.getChannelName(),channel.getChannelAvatar(),context);
}
否则{
Toast.makeText(context,R.string.error\u connection\u no\u internet,Toast.LENGTH\u LONG).show();
}
}
};
@凌驾
public int getItemCount(){
返回通道;
}
@凌驾
附加ToRecyclerView(RecyclerView RecyclerView)上的公共无效{
super.onAttachedToRecyclerView(recyclerView);
}
公共静态类FavoritesViewHolder扩展了RecyclerView.ViewHolder{
CardView频道CardView;
文本视图;
TextView频道TitleSubView;
ImageView频道AvatarView;
FavoritesViewWolder(查看项目视图){
超级(项目视图);
channelCardView=(CardView)itemView.findViewById(R.id.channelCardView);
ChannelSubView=(TextView)itemView.findViewById(R.id.subsAmount);
ChannelTitleSubView=(TextView)itemView.findViewById(R.id.SubText);
channelAvatarView=(ImageView)itemView.findViewById(R.id.channelAvatarView);
}
}
}您是否可以检查是否正在编辑传递给适配器构造函数的初始列表的顺序/排序
List<Favorites> channels = getChannels();
adapter = new FavoritesAdapter(context, channels);
recyclerView.setAdapter(adapter)
// edit or sort *channels*
adapter.notifyDataSetChanged();
List channels=getChannels();
适配器=新的收藏夹适配器(上下文、通道);
recyclerView.setAdapter(适配器)
//编辑或排序*频道*
adapter.notifyDataSetChanged();
您正在对onBindViewHolder
进行异步调用,因此当异步任务的响应到来时,它会从public void onBindViewHolder(最终收藏夹ViewHolder,最终int位置)读取错误的位置
因为到那时,onBindViewHolder
返回的位置已更改
您可以做的是,将位置作为参数传递到async task
,并在async task
的结果中传递相同的位置。这样,对位置p(比如)的调用将应用于p
除此之外,还可以尝试在RecycleServiceWholder上使用getAdapterPosition
而不是getPosition
(已弃用并造成混乱)。请发布适配器类。您成功修复了吗?我也有同样的经历