アイテムが選択されたときに、recyclerview アダプターに動的テキストビューを追加したい。しかし、動的ビューは別のビューに追加され、動的に作成されたrecyclerviewビューをスクロールすると、そのビューで毎回変更されます。これは私のアダプタークラスです
public class MultiSelectRecyclerViewAdapter SelectableAdapter<MultiSelectRecyclerViewAdapter.ViewHolder> {
private static List<Ingredient> mArrayList;
private static Context mContext;
private ViewHolder.ClickListener clickListener;
static ImageView add_img;
static ImageView remove_img;
static TextView textView_count;
RelativeLayout relativeLayout;
static boolean flag = false;
static HashMap<String,TextView> mTextViewList = new HashMap<>();
private ViewHolder mViewHolder;
public MultiSelectRecyclerViewAdapter (Context context, List<Ingredient> arrayList, ViewHolder.ClickListener clickListener) {
this.mArrayList = arrayList;
this.mContext = context;
this.clickListener = clickListener;
}
// Create new views
@Override
public MultiSelectRecyclerViewAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
int viewType) {
View itemLayoutView = LayoutInflater.from(parent.getContext()).inflate(
R.layout.cardview_toppings, null);
ViewHolder viewHolder = new ViewHolder(itemLayoutView,clickListener);
this.mViewHolder = viewHolder;
return viewHolder;
}
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
@Override
public void onBindViewHolder(final ViewHolder viewHolder, int position) {
if(isSelected(position))
{
Ion.with(mContext).load(mArrayList.get(position).getThumbnail()).withBitmap().asBitmap()
.setCallback(new FutureCallback<Bitmap>() {
@Override
public void onCompleted(Exception e, Bitmap result) {
viewHolder.thumbnail.setImageBitmap(result);
}
});
viewHolder.textView.setText(mArrayList.get(position).getIngredient_name());
viewHolder.textView_count.getId();
for (Map.Entry<String, TextView> entry : mTextViewList.entrySet()) {
entry.getValue().setVisibility(isSelected(position) ? View.VISIBLE : View.GONE);
entry.getValue().setBackgroundResource(R.drawable.badge_circle);
}
}else {
Ion.with(mContext).load(mArrayList.get(position).getThumbnail()).withBitmap().asBitmap()
.setCallback(new FutureCallback<Bitmap>() {
@Override
public void onCompleted(Exception e, Bitmap result) {
viewHolder.thumbnail.setImageBitmap(result);
}
});
viewHolder.textView.setText(mArrayList.get(position).getIngredient_name());
}
}
@Override
public int getItemCount() {
return mArrayList.size();
}
public static class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener,View.OnLongClickListener {
public TextView tvName;
private ClickListener listener;
private final View selectedOverlay;
public ImageView thumbnail;
public RelativeLayout relativeLayout;
public TextView textView;
public RelativeLayout dynamic_relativeLayout;
public TextView textView_count;
public ViewHolder(View itemLayoutView, ClickListener listener) {
super(itemLayoutView);
this.listener = listener;
tvName = (TextView) itemLayoutView.findViewById(R.id.bottom_name);
selectedOverlay = (View) itemLayoutView.findViewById(R.id.selected_overlay);
thumbnail = (ImageView) itemLayoutView.findViewById(R.id.thumbnail);
textView = (TextView) itemLayoutView.findViewById(R.id.bottom_name);
relativeLayout = (RelativeLayout) itemView.findViewById(R.id.thumbnail_relative);
dynamic_relativeLayout = (RelativeLayout) itemView.findViewById(R.id.dynamic_layout);
textView_count = new TextView(mContext);
itemLayoutView.setOnClickListener(this);
itemLayoutView.setOnLongClickListener (this);
}
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
@Override
public void onClick(View v) {
if (listener != null) {
flag = true;
setImagePerview(mArrayList.get(getAdapterPosition ()).getPreview(), mContext);
int id = View.generateViewId();
textView_count.setId(id);
RelativeLayout.LayoutParams params_text = new RelativeLayout.LayoutParams(50, 50);
params_text.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE);
textView_count.setLayoutParams(params_text);
textView_count.setBackgroundResource(R.drawable.badge_circle);
textView_count.setGravity(Gravity.CENTER);
mTextViewList.put(String.valueOf(id),textView_count);
relativeLayout.addView(textView_count);
listener.onItemClicked(getAdapterPosition ());
}
}
@Override
public boolean onLongClick (View view) {
if (listener != null) {
return listener.onItemLongClicked(getAdapterPosition ());
}
return false;
}
public interface ClickListener {
public void onItemClicked(int position);
public boolean onItemLongClicked(int position);
}
}
}