我的回收站视图有问题。当我向下滚动时一切正常,但是当我滚动回顶部时,我设置为图像的可绘制对象正在更改。这是我的适配器代码:
@Override
public FeedsHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.feed_item, parent, false);
final FeedsHolder holder = new FeedsHolder(view);
//Query
QueryOptions options = new QueryOptions();
options.setRelated(Arrays.asList("usersThatLike"));
BackendlessDataQuery query = new BackendlessDataQuery();
query.setQueryOptions(options);
Backendless.Data.of(Feeds.class).find(query, new AsyncCallback<BackendlessCollection<Feeds>>() {
@Override
public void handleResponse(BackendlessCollection<Feeds> response) {
String userId = Backendless.UserService.CurrentUser().getObjectId();
for (Feeds feed : response.getData()) {
feed = list.get(holder.getAdapterPosition());
List<BackendlessUser> likedUsers = feed.getUsersThatLike();
for (BackendlessUser user : likedUsers)
if (user.getObjectId().equals(userId)) {
//** GOOGLE MATERIAL ICON WITH FILLED HEART
setBackground(holder.like, ContextCompat.getDrawable(context, R.drawable.ic_favorite_black_24dp));
} else if (!user.getObjectId().equals(userId)) {
//** GOOGLE MATERIAL ICON WITH EMPTY HEART
setBackground(holder.like, ContextCompat.getDrawable(context, R.drawable.ic_favorite_border_black_24dp));
}
}
}
@Override
public void handleFault(BackendlessFault backendlessFault) {
}
});
return holder;
}
而不是我在 onBindViewHolder 中设置了 holder.like imageview 的默认图像:
@Override
public void onBindViewHolder(final FeedsHolder holder, final int position) {
holder.like.setBackground(ContextCompat.getDrawable(context, R.drawable.ic_favorite_border_black_24dp));
}
我认为这就是问题所在......
但我不知道如何解决这个问题,因为没有这行代码,我的其他行 recyclerview 将有不同的图像:有些会充满心,有些会空。
所以请帮我找到解决方案。
编辑:发布我的适配器代码
public class FeedAdapter extends RecyclerView.Adapter<FeedAdapter.FeedsHolder> {
private List<Feeds> list;
private Context context;
private static List<Long> selectedIds;
List<BackendlessUser> backendlessUserList;
private List<Comments> cake = new ArrayList<>();
private static String TAG = "MainActivity";
public FeedAdapter(Context context, List<Feeds> list) {
this.context = context;
this.list = list;
}
@Override
public FeedsHolder onCreateViewHolder(ViewGroup parent, final int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.feed_item, parent, false);
final FeedsHolder holder = new FeedsHolder(view);
//Query
QueryOptions options = new QueryOptions();
options.setRelated(Arrays.asList("usersThatLike"));
BackendlessDataQuery query = new BackendlessDataQuery();
query.setQueryOptions(options);
Backendless.Data.of(Feeds.class).find(query, new AsyncCallback<BackendlessCollection<Feeds>>() {
@Override
public void handleResponse(BackendlessCollection<Feeds> response) {
myFeeds = response;
String userId = Backendless.UserService.CurrentUser().getObjectId();
for (Feeds feed : response.getData()) {
feed = list.get(holder.getAdapterPosition());
List<BackendlessUser> likedUsers = feed.getUsersThatLike();
backendlessUserList = likedUsers;
for (BackendlessUser user : likedUsers)
if (user.getObjectId().equals(userId)) {
setBackground(holder.like, ContextCompat.getDrawable(context, R.drawable.ic_favorite_black_24dp));
} else if (!user.getObjectId().equals(userId)) {
setBackground(holder.like, ContextCompat.getDrawable(context, R.drawable.ic_favorite_border_black_24dp));
}
}
}
@Override
public void handleFault(BackendlessFault backendlessFault) {
}
});
return holder;
}
@Override
public void onBindViewHolder(final FeedsHolder holder, final int position) {
//Starting Feeds
final Feeds feeds = list.get(position);
holder.like.setBackground(ContextCompat.getDrawable(context, R.drawable.ic_favorite_border_black_24dp));
//Name
holder.name.setText(feeds.getOwner());
@Override
public int getItemCount() {
return list.size();
}
public class FeedsHolder extends RecyclerView.ViewHolder {
//Setup Views
CardView card;
TextView name;
public FeedsHolder(View itemView) {
super(itemView);
//Assign views by ID
card = (CardView) itemView.findViewById(R.id.cv);
name = (TextView) itemView.findViewById(R.id.commenter);
}
}
}