如何在回收站视图适配器中添加动态文本视图



我想在recyclerview适配器中添加动态textview,当项目被选中时。但是动态视图是在另一个视图中添加的,当我滚动循环视图时,动态创建的视图每次都在那个视图中改变。这是我的适配器类

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);
        }
      }
    }
this is a common issues as you need to sync access to the holder. There are a few quite good libraries to handle this and other cases of bitmap caching, reusing, pooling.

您可以使用滑翔本地和网络图像:

   **Glide.with(context).load(items.get(i).getImage()).into(viewHolder.image);**

或仅用于网络图像Volley或仅切换到其NetworkImageView小部件,然后在适配器中:

**image.setImageUrl(items.get(i).getImage(), imageLoader);**

它们可能使用不同的方法但它们都知道该怎么做

相关内容

  • 没有找到相关文章

最新更新