使用viewholder中的滑动从服务器加载图像

时间:2017-08-13 08:27:41

标签: android imageview retrofit android-adapter android-glide

public class MyAdapter_home extends RecyclerView.Adapter<MyAdapter_home.MyAdapter_HomeViewHolder> {


private List<Datum> data;
private List<Datum2>data2;
private int rowLayout;
private Context context;
PopupMenu popupMenu ;


public MyAdapter_home(List<Datum> data, int rowLayout, Context context) {
    this.data = data;
    this.rowLayout = rowLayout;
    this.context = context;
}



@Override
public MyAdapter_HomeViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.card_item_home, parent, false);
    return new MyAdapter_HomeViewHolder(view);
}

@Override
public void onBindViewHolder(final MyAdapter_HomeViewHolder holder, final int position) {



    holder.userNameTV.setText(data.get(position).getUserInfo().getFullName());
    holder.msgBodyTV.setText(data.get(position).getBody());
    holder.originator_iD.setText(data.get(position).getUserInfo().getId().toString());
    holder.imV.setImageURI(Uri.parse(Uri.parse(data.get(position).getUserInfo().getAvatar()).getPath()));
    holder.owner_type_ET.setText("1");
    holder.subject_ET.setText("Message");




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

            Intent intent = new Intent(view.getContext(), Single_Message.class);
            intent.putExtra("ORIGINATOR_ID", holder.originator_iD.getText().toString());
            intent.putExtra("OWNER_TYPE", holder.owner_type_ET.getText().toString());
            intent.putExtra("SUBJECT", holder.subject_ET.getText().toString());
            intent.putExtra("USERNAME", holder.userNameTV.getText().toString());
            view.getContext().startActivity(intent);


        }
    });

    holder.mCardView.setOnLongClickListener(new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(final View view) {

            popupMenu = new PopupMenu(view.getContext(), view);
            popupMenu.setOnDismissListener(new PopupMenu.OnDismissListener() {
                @Override
                public void onDismiss(PopupMenu menu) {

                }
            });
            popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
                @Override
                public boolean onMenuItemClick(MenuItem item) {
                    switch (item.getItemId()) {
                        case R.id.lang_java:
                            Intent intent = view.getContext().getPackageManager().getLaunchIntentForPackage("com.freemig.social");
                            if (intent != null) {
                                // We found the activity now start the activity
                                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                                view.getContext().startActivity(intent);
                            } else {
                                // Bring user to the market or let them choose an app?
                                intent = new Intent(Intent.ACTION_VIEW);
                                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                                intent.setData(Uri.parse("market://details?id=" + "com.freemig.social"));
                                view.getContext().startActivity(intent);
                            }
                            return true;
                        case R.id.lang_android:
                            Toast.makeText(view.getContext(), "Mute Conversation clicked",
                                    Toast.LENGTH_SHORT).show();
                            return true;
                        case R.id.lang_python:
                            Toast.makeText(view.getContext(), "Remove User clicked",
                                    Toast.LENGTH_SHORT).show();
                            return true;
                        case R.id.lang_ruby:
                            Toast.makeText(view.getContext(), "Block User clicked",
                                    Toast.LENGTH_SHORT).show();
                            return true;
                    }
                    return false;
                }
            });
            popupMenu.inflate(R.menu.popup_home_long_click);
            popupMenu.show();
            return false;
        }
    });
}

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

public static class MyAdapter_HomeViewHolder extends RecyclerView.ViewHolder {

    TextView userNameTV;
    TextView msgBodyTV;
    CardView mCardView;
    TextView originator_iD;
    ImageView imV;
    String s;

    ConversationAPIService conversationAPIService;
    EditText owner_type_ET, subject_ET;


    public MyAdapter_HomeViewHolder(View itemView) {
        super(itemView);
        userNameTV = (TextView) itemView.findViewById(R.id.username);
        msgBodyTV = itemView.findViewById(R.id.msgbody);
        mCardView = (CardView) itemView.findViewById(R.id.card_view);
        originator_iD = itemView.findViewById(R.id.frIdET);
        imV = itemView.findViewById(R.id.iv_image);
        owner_type_ET = (EditText) itemView.findViewById(R.id.owner_typeET);
        subject_ET = (EditText) itemView.findViewById(R.id.subjectET);
    }
}
}

这是我的recyclerView适配器类。我想从这里显示图像头像。我不明白怎么做。我尝试了什么 -

  1. Glide文档但不理解
  2. 在stackoverflow中,每个人都使用之前定义的静态URL。但我的网址是随机生成的以及如何使用该网址
  3. 我尝试使用位图。但无法弄明白。怎么做?

5 个答案:

答案 0 :(得分:2)

您可以这样记录,

Uri uri = Uri.parse(Uri.parse(data.get(position).getUserInfo().getAvatar()).getPath());
Glide.with(context).load(uri).into(holder.imV);

答案 1 :(得分:2)

您可以使用Picasso库。使用Picasso加载图片非常简单,您可以这样做:

Uri imageURI = Uri.parse(Uri.parse(data.get(position).getUserInfo().getAvatar()).getPath());
Picasso.with(context).load("imageURI").into(holder.imV);

在他们的website中,您可以获得所有细节。

另一个库是Glide。您也可以使用Glide来加载图片。

您还可以使用Glide

Uri imageURI = Uri.parse(Uri.parse(data.get(position).getUserInfo().getAvatar()).getPath());
Glide.with(context).load(imageURI).asBitmap().placeholder(R.drawable.placeholder)
         .error(R.drawable.error).into(holder.imV);

答案 2 :(得分:2)

您还可以使用const filter = {} filter[Op.or] = [ { date: { [Op.gt]: sinceDate } }, { [Op.and]: { date: sinceDate, id: { [Op.gt]: sinceId } } } const result = await Posts.findAll({ where: filter }) 库。

UniversalImageLoader

有关更多信息,请参阅 Android-Universal-Image-Loader

OR

您可以在应用程序中使用Glide Library。 你在任何适配器类中使用了load image,所以试试这个。

    universalImageLoader imageLoader =new universalImageLoader();

  imageLoader.loadImage(imageUri, targetSize, options, new SimpleImageLoadingListener() {
      @Override
      public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
        // use your bitmap …
      }
    });

有关更多信息,请参阅glide

答案 3 :(得分:1)

试试这个:

 GlideApp
    .with(context)
    .load(url)
    .centerCrop() //cropping center image 
    .placeholder(R.drawable.loading_spinner) //placeholder while loading image 
    .into(viewHodler.myImageView);

答案 4 :(得分:0)

试试这个:

Glide.with(context)
.load("your image url")
.into(holder.imV);