在回收类之外使用recycleView视图

时间:2017-07-22 12:06:52

标签: android android-layout android-recyclerview

有没有办法在一边使用recycleView视图呢?

我的意思是,在我的viewHolder中,这是我的完整适配器:

    public MyAdapter(Context context, Bitmap bitmap, List<String> list) {
            this.listItem = listItem;
            this.bitmap = bitmap;
            this.list = list;
            this.context = context;
        }

        @Override
        public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            // Log.d(logTag, "view group " + parent);
            View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.image_row, parent, false);
            return new MyViewHolder(v);
        }

        @Override
        public void onBindViewHolder(final MyViewHolder holder, final int position) {
            holder.progressBar.setVisibility(View.VISIBLE);
            Glide.with(context)
                    .load(new File(list.get(position)))
                    // .centerCrop()
                    .into(holder.imageView);
            holder.imageView.setVisibility(View.VISIBLE);
            initiateView.passMyProgressBar(holder.progressBar);
            Log.d(TAG, "onBindViewHolder: " + list);
            holder.imageView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View view) {
                    alertView("Are you sure you want to remove this image?" + holder.getAdapterPosition(), holder.getAdapterPosition());
                    return true;
                }
            });
            holder.uploaded.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {

                }
            });

            //uploadImage(bitmap, position, holder.progressBar, holder.imageView, holder.uploaded, list);
        }

        @Override
        public void onViewRecycled(MyViewHolder holder) {
            super.onViewRecycled(holder);
            //notifyDataSetChanged();
        }

        @Override
        public int getItemCount() {
            return list.size();
        }


        public class MyViewHolder extends RecyclerView.ViewHolder {
            private ImageView imageView, uploaded;
            private ProgressBar progressBar;


            public MyViewHolder(View itemView) {
                super(itemView);
                imageView = itemView.findViewById(R.id.imageViewRow);
                progressBar = itemView.findViewById(R.id.progressBar);
                uploaded = itemView.findViewById(R.id.uploadedImg);
            }
        }

//        public void setOnItemClickListener(final AdapterView.OnItemClickListener mItemClickListener) {
//            this.mItemClickListener = mItemClickListener;
//        }

        public void removeAt(int position) {
            list.remove(position);
            notifyItemRemoved(position);
            notifyItemRangeChanged(position, list.size());
        }
    }

我可以在外层使用它们,如:

ProgressBar progressBar = recycleViewLayoutView.findViewById(R.id.progressBar);
progressBar.setVisiblity(VISIBLE);

如果没有,我可以创建听众或其他什么,我知道这不是好办法,但是网络服务迫使我这样做。

1 个答案:

答案 0 :(得分:0)

您需要在适配器中使用接口才能实现此目的。它可能是这样的 -

public interface MyDelegate {
    void passMyProgressBar (ProgressBar pBar);
}

实例化它,然后在onBindViewHolder添加此行 -

myDelegateInstance.passMyProgressBar(holder.progressBar);

现在,您必须在您希望ProgressBar可访问的类中实现上述接口。