RecyclerView is not using appropriate element from ViewTreeObserver?

118 Views Asked by At

I am creating contact app. When creating new Contact, user can turn on camera and take a photo. There is a ContactsActivity which displays all Contacts using RecyclerView. Inside RecyclerView (ContactsAdapter), I am recalculating size of taken image and I'm placing that scaled image into a ImageView, which is part of a layout for RecyclerView.

For quite some time, I was getting ArithmeticException, since getWidth() and getHeight() were returning 0. I got that working by using ViewTreeObserver.

I have a helper method (PictureUtils.getScaledBitmap()) which does resizing taken photo. I'm not showing it here for the sake of clarity.

The core issue I'm facing, is when I create two Contacts, and move to ContactsActivity, I see those two Contacts, but each Contact has first photo taken!

Contact class:

    public class Contact {

    private int id, mAvatar;
    private String mFirstname;
    private String mLastname;
    private String mEmail;
    private String mCurrentPhotoPath;
}

This is what I have in onBindViewHolder() in RecyclerView:

    @Override
    public void onBindViewHolder(ViewHolder holder, int position){
        //Set the values inside the given view
        CardView cardView = holder.cardView;
        currentContact = mContacts.get(position); //mContacts is collection passed to the constructor of RecyclerView

        ImageView imageView = (ImageView)cardView.findViewById(R.id.image_profile);


        ViewTreeObserver observer = imageView.getViewTreeObserver();
        observer.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {

            @Override
            public void onGlobalLayout() {

                if (currentContact.getCurrentPhotoPath() != null) {
                    Log.i("WIDTH",String.valueOf(imageView.getWidth())); //Here I get desired value
                    Log.i("HEIGHT",String.valueOf(imageView.getHeight())); //Here as well
                    Log.i("Photo path: ", currentContact.getCurrentPhotoPath()); //Here, I get path/IMG_0, for both images, I need to get path/IMG_0 and path/IMG_1

                    Bitmap bitmap = PictureUtils.getScaledBitmap(currentContact.getCurrentPhotoPath(), width, height);

                    imageView.setImageBitmap(bitmap);


                } else {
                    if (currentContact.getAvatar() != 0){
                        Drawable drawable = cardView.getResources().getDrawable(mContacts.get(position).getAvatar());
                        imageView.setImageDrawable(drawable);
                    }
                }

                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                    imageView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                }
            }
        });
}

How to display for each Contact its photo?

Edit (for a commenter below)

Here is ViewHolder, it is contained inside RecyclerView:

 public static class ViewHolder extends RecyclerView.ViewHolder {

    private CardView cardView;
    private LinearLayout mLinearLayout;

    public ViewHolder(CardView v) {
        super(v);
        cardView = v;
        mLinearLayout = cardView.findViewById(R.id.linear_layout);
    }

}
0

There are 0 best solutions below