在recyclerview中显示之前下载所有图像

时间:2017-09-06 08:50:44

标签: android android-recyclerview android-glide

我有RecyclerView,我将在其中显示9张图片。所有这些图像都在服务器上。我正在使用Glide下载适配器onBindViewHolder中的图片。问题是如果互联网比Glide慢,则需要时间来下载图像和显示。在下载图像的时候,我的Recyclerview是空的,并没有显示任何内容。我希望显示Progressbar,直到所有图像都下载完毕。下载完所有图片后,将隐藏Progressbar并显示所有9张图片。

这是我的适配器:

public class PuzzleGridAdapter extends RecyclerView.Adapter<PuzzleGridAdapter.MyViewHolder> implements ItemTouchHelperAdapter {
    List<String> itemList;
    LayoutInflater inflater;
    Context context;
    private final OnStartDragListener mDragStartListener;

    public PuzzleGridAdapter(Context context, OnStartDragListener dragStartListener) {
        this.context = context;
        mDragStartListener = dragStartListener;
        inflater = LayoutInflater.from(context);
        itemList = new ArrayList<>();
    }

    public void getItemList(List<String> nameList) {
        int currentSize = itemList.size();
        itemList.clear();
        itemList.addAll(nameList);
        notifyItemRangeRemoved(0, currentSize);
        notifyItemRangeInserted(0, nameList.size());
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = inflater.inflate(R.layout.custom_puzzle_row, parent, false);
        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(final MyViewHolder holder, int position) {
        Log.e("PuzzleGridAdapter", itemList.get(position));

//        holder.textView.setTag(itemList.get(position));
//        new DownloadTask().execute(holder.textView);
        Glide.with(context).load(itemList.get(position)).asBitmap().override(200,200).listener(new RequestListener<String, Bitmap>() {
            @Override
            public boolean onException(Exception e, String model, Target<Bitmap> target, boolean isFirstResource) {
                Log.e("PuzzleGridAdapter", e.getMessage());
                return false;
            }

            @Override
            public boolean onResourceReady(Bitmap resource, String model, Target<Bitmap> target, boolean isFromMemoryCache, boolean isFirstResource) {
                Log.e("PuzzleGridAdapter", "ready");
                return false;
            }
        }).diskCacheStrategy(DiskCacheStrategy.RESULT).into(holder.textView);
        //holder.textView.setImageResource(itemList.get(position));
        // Start a drag whenever the handle view it touched
        holder.cardView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (MotionEventCompat.getActionMasked(event) == MotionEvent.ACTION_DOWN) {
                    mDragStartListener.onStartDrag(holder);
                }
                return false;
            }
        });
    }


    @Override
    public int getItemCount() {
        Log.e("PuzzleGridAdapter", itemList.size() + "");
        return itemList.size();
    }

    public List<String> getUpdateList() {
        return itemList;
    }

    @Override
    public boolean onItemMove(int fromPosition, int toPosition) {
        String item = itemList.remove(fromPosition);
        itemList.add(toPosition, item);
        notifyItemMoved(fromPosition, toPosition);
        return true;
    }

    @Override
    public void onItemDismiss(int position) {
        itemList.remove(position);
        notifyItemRemoved(position);
    }

    public class MyViewHolder extends RecyclerView.ViewHolder implements ItemTouchHelperViewHolder {

        CardView cardView;
        ImageView textView;

        public MyViewHolder(View itemView) {
            super(itemView);
            cardView = (CardView) itemView.findViewById(R.id.gameCard);
            textView = (ImageView) itemView.findViewById(R.id.kolamImage);
        }

        @Override
        public void onItemSelected() {
            itemView.setBackgroundColor(Color.LTGRAY);
        }

        @Override
        public void onItemClear() {
            itemView.setBackgroundColor(Color.WHITE);
        }
    }
}

在下载所有内容之前,如何显示Progressbar

1 个答案:

答案 0 :(得分:0)

从示例https://stackoverflow.com/questions/aa获取帮助,并将此类代码添加到adpter:

it

在回收商视图项的Imageview上添加进度条。