我有一个使用数据绑定的适配器。我从下面的片段中调用下面的代码中的方法moveTagToTop(int hashTagId)
来交换项目,然后我想更改未发生的特定项目的背景颜色。我的适配器中有2个View Holder,在完成HashTagViewHolder
方法中我想做的事情后,我想访问moveTagToTop
并更改视图持有人中约束布局的背景颜色,使用数据绑定。请帮助我。
public class HashtagAdapter extends HashTagBaseAdapter {
public static final int ITEM_HASHTAG = 0;
public static final int ITEM_TRENDING = 2;
private final List<Hashtag> hashtags;
private final HashTagsListener listener;
ItemHashtagTrendingBinding binding;
ItemHashtagBinding binding2;
;
public HashtagAdapter(BaseActivity activity, List<Hashtag> hashtags, HashTagsListener hashTagsListener) {
super(activity);
this.hashtags = hashtags;
this.listener = hashTagsListener;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater layoutInflater =
LayoutInflater.from(parent.getContext());
switch (viewType) {
case ITEM_HASHTAG:
ItemHashtagBinding itemHashtagBinding =
ItemHashtagBinding.inflate(layoutInflater, parent, false);
return new HashTagViewHolder(itemHashtagBinding);
case ITEM_TRENDING:
ItemHashtagTrendingBinding itemHashtagTrendingBinding =
ItemHashtagTrendingBinding.inflate(layoutInflater, parent, false);
return new HashTagViewPercentageHolder(itemHashtagTrendingBinding);
}
return null;
}
@Override
public int getItemViewType(int position) {
return hashtags.get(position).getType();
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
holder.bind(hashtags.get(position));
}
@Override
public int getItemCount() {
return hashtags.size();
}
public void updateHashTag(Hashtag hashtag) {
for (int i = 0; i < hashtags.size(); i++) {
if (hashtags.get(i).equals(hashtag)) {
notifyItemChanged(i);
break;
}
}
}
public void addHashTags(List<Hashtag> hashtags) {
int previousSize = getItemCount();
this.hashtags.addAll(hashtags);
notifyItemRangeInserted(previousSize, hashtags.size());
}
private class HashTagViewHolder extends BaseRecyclerAdapter.ViewHolder<Hashtag> {
public HashTagViewHolder(ItemHashtagBinding dataBinding) {
super(dataBinding);
binding2 = dataBinding;
}
@Override
public void bind(Hashtag hashtag) {
binding2.setHashTag(hashtag);
binding2.tvHashtag.setOnClickListener(view -> listener.onClick(hashtag));
binding2.ivSubscribe.setOnClickListener(view -> {
hashtag.setSubscribed(!hashtag.isSubscribed());
listener.onSubscribed(hashtag);
notifyItemChanged(getAdapterPosition());
});
}
}
private class HashTagViewPercentageHolder extends BaseRecyclerAdapter.ViewHolder<Hashtag> {
public HashTagViewPercentageHolder(ItemHashtagTrendingBinding dataBinding) {
super(dataBinding);
binding = dataBinding;
}
@Override
public void bind(Hashtag hashtag) {
binding.setHashTag(hashtag);
binding.tvHashtag.setOnClickListener(view -> listener.onClick(hashtag));
binding.ivSubscribe.setOnClickListener(view -> {
if (hashtag.getId() != -1) {
hashtag.setSubscribed(!hashtag.isSubscribed());
}
listener.onSubscribed(hashtag);
notifyItemChanged(getAdapterPosition());
});
}
}
public void moveTagToTop(int hashTagId) {
Log.d("***HashTagAdapter", "Entered");
for (int i = 0; i < hashtags.size(); i++) {
if (hashtags.get(i).getId() == hashTagId) {
if (Constants.UPDATED_AUDIENCE_POS+1 <= 3) {
Collections.swap(hashtags, i, Constants.UPDATED_AUDIENCE_POS + 1);
notifyItemMoved(i, Constants.UPDATED_AUDIENCE_POS + 1);
Constants.UPDATED_AUDIENCE_POS = Constants.UPDATED_AUDIENCE_POS + 1;
// binding2.getRoot().setBackgroundColor(context.getResources().getColor(R.color.colorPrimary));
} else {
{
Toast.makeText(context, context.getResources().getString(R.string.no_more_than_3), Toast.LENGTH_LONG).show();
}
}
}
/*Collections.swap(re.getItems(), position, 0);
notifyItemMoved(position, 0);*/
}
}
}