从ViewHolder类传递Glide中的上下文?

时间:2018-03-01 04:40:29

标签: android firebase firebase-storage android-glide

我试图在with()内传递上下文,但是"这个"不起作用,getContext()getActivity()也不起作用。我的类是一个扩展RecycleView的ViewHolder。我如何获得上下文?

protected void bind(I item, @Nullable OnContactClickListener<I> listener) {
            Author author = item.getContact().getAuthor();
            Glide.with(this /* context */)
                    .using(new FirebaseImageLoader())
                    .load(storageRef.child("/"+author.getName()+"/pic.jpg"))
                    .error(new IdenticonDrawable(author.getId().getBytes()))
                    .into(avatar);
            String contactName = author.getName();
            name.setText(contactName);

4 个答案:

答案 0 :(得分:3)

如此简单地获取avtar视图的上下文。

protected void bind(I item, @Nullable OnContactClickListener<I> listener) {
            Author author = item.getContact().getAuthor();
            Glide.with(avatar.getContext())
                    .using(new FirebaseImageLoader())
                    .load(storageRef.child("/"+author.getName()+"/pic.jpg"))
                    .error(new IdenticonDrawable(author.getId().getBytes()))
                    .into(avatar);
            String contactName = author.getName();
            name.setText(contactName);

答案 1 :(得分:1)

从这一行

name.setText(contactName);

我可以说名字是TextView。您可以使用view.getContext()从视图中获取Contextview.getContext()返回运行视图的上下文。

Glide.with(name.getContext())

答案 2 :(得分:1)

你可以这样试试:

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
    private final Context context;

    public MyAdapter(Context context) {
        this.context = context;
    }

    @NonNull
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new ViewHolder(LayoutInflater.from(this.context).inflate(R.layout.frames_adapter_item, parent, false));
    }

    public void onBindViewHolder(@NonNull final ViewHolder holder, @SuppressLint("RecyclerView") int position) {


        holder.imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {



            }
        });

        holder.progress_bar.setVisibility(View.GONE);

        Glide.with(getApplicationContext()).load(arrayOfFile.get(position)).asBitmap().into(new SimpleTarget<Bitmap>() {
                @Override
                public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
                    if (resource != null) {
                        imageView.setImageBitmap(resource);
                    }
                }

            });


    }

    public int getItemCount() {
        return 1;
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        final ImageView imageView;
        final ProgressBar progress_bar;

        ViewHolder(View itemView) {
            super(itemView);
            this.imageView = itemView.findViewById(R.id.imageView);
            this.progress_bar = itemView.findViewById(R.id.progress_bar);
        }
    }
}

答案 3 :(得分:1)

您可以使用

String text = "abcdEfg";
Pattern pattern = Pattern.compile("[a-z]");
Matcher matches = pattern.matcher(text);
StringBuffer sb = new StringBuffer();
while (matches.find()) {
    matches.appendReplacement(sb, matches.group().toUpperCase());
    System.out.println(text); // some of ABCDEFG
}
matches.appendTail(sb);
System.out.println(sb); // ABCDEFG