'How to make visible a element when the RecyclerView is loaded?

I'm making an social app and I want to implement a system where when you click on the comment button a Input dropdown...

The problem is that it doesn't matter what button you press, it always make visible the last "Publication" you did.

XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto">

<com.google.android.material.card.MaterialCardView
    android:id="@+id/card"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="8dp">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <!-- Media -->
        <ImageView
            android:id="@+id/multimediaInComment"
            android:background="@drawable/family"
            android:visibility="gone"
            android:layout_width="match_parent"
            android:layout_height="194dp"
            android:contentDescription="Me gusta jesucristo"
            android:scaleType="centerCrop" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:padding="16dp">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal">

                <de.hdodenhof.circleimageview.CircleImageView
                    android:id="@+id/imgProfileInPublication"
                    android:layout_width="50dp"
                    android:layout_height="50dp"
                    android:src="@drawable/defautprofilepic"
                    />

                <TextView
                    android:id="@+id/txtUsernameInPublication"
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:textSize="20dp"
                    android:layout_marginTop="6dp"
                    android:layout_marginLeft="15dp"
                    android:gravity="center"
                    android:text="Username"
                    />

                <TextView
                    android:id="@+id/dateInPublication"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_marginTop="8dp"
                    android:layout_gravity="end"
                    android:textAlignment="textEnd"
                    android:text="Fecha"
                    android:textColor="?android:attr/textColorSecondary" />

            </LinearLayout>

            <TextView
                android:id="@+id/txtTextInPublication"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="16dp"
                android:text="Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Elit sed vulputate mi sit amet. Tristique senectus et netus et malesuada fames. Turpis egestas integer eget aliquet nibh praesent. In nulla posuere sollicitudin aliquam ultrices sagittis."
                android:textAppearance="?attr/textAppearanceBody2"
                android:textColor="?android:attr/textColorSecondary" />

        </LinearLayout>

        <!-- Buttons -->
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="8dp"
            android:orientation="horizontal">

            <com.google.android.material.button.MaterialButton
                android:id="@+id/btnlikeInPublication"
                style="?attr/borderlessButtonStyle"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                app:icon="@drawable/ic_baseline_thumb_up_24"
                android:layout_marginEnd="8dp"
                android:text="Like" />

            <com.google.android.material.button.MaterialButton
                android:id="@+id/btnCommentInPublication"
                style="?attr/borderlessButtonStyle"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                app:icon="@drawable/ic_baseline_mode_comment_24"
                android:text=" 5 comentarios"/>

            <com.google.android.material.button.MaterialButton
                android:id="@+id/btnDeletePublication"
                style="?attr/borderlessButtonStyle"
                android:visibility="gone"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                app:iconTint="@color/red_btn_bg_color"
                android:textColor="@color/red_btn_bg_color"
                app:icon="@drawable/ic_baseline_delete_24"/>

        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="8dp"
            android:orientation="horizontal">

            <com.google.android.material.textfield.TextInputLayout
                android:id="@+id/textInputLayoutCommentOnPublicationScroll"
                style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
                app:endIconMode="custom"
                android:visibility="gone"
                app:endIconDrawable="@drawable/ic_baseline_send_24"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                <com.google.android.material.textfield.TextInputEditText
                    android:id="@+id/txtCommentOnPublicationScroll"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:hint="Comment to username" />

            </com.google.android.material.textfield.TextInputLayout>

        </LinearLayout>

    </LinearLayout>

</com.google.android.material.card.MaterialCardView>

RECYCLE VIEW ADAPTER:

public class PublicationsRecView extends 
RecyclerView.Adapter<PublicationsRecView.ViewHolderDatos> {
// DATOS
private ArrayList<Publicacion> listDatos;
private PublicationCardBinding binding;
private Persona persona;

// FIREBASE
private MyFirebaseHelper myFirebaseHelper;

public PublicationsRecView(Persona personaActual, ArrayList<Publicacion> listDatos) {

    // INICIALIZO VARIABLES
    myFirebaseHelper = new MyFirebaseHelper();

    this.persona = personaActual;
    this.listDatos = listDatos;
}

@Override
public PublicationsRecView.ViewHolderDatos onCreateViewHolder(ViewGroup parent, int viewType) {

    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.publication_card,null,false);

    // BINDEAMOS LA VISTA

    binding = PublicationCardBinding.inflate(LayoutInflater.from(parent.getContext()), parent, false);
    view = binding.getRoot();

    return new PublicationsRecView.ViewHolderDatos(view);
}

@Override
public void onBindViewHolder(PublicationsRecView.ViewHolderDatos holder, int position) {

    holder.asignarDatos(listDatos.get(position));
}

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

public class ViewHolderDatos extends RecyclerView.ViewHolder {


    public ViewHolderDatos(View itemView) {

        // NO SE NECESITA, UTILIZO BINDING
        super(itemView);
    }

    public void asignarDatos(Publicacion publicacion) {

        binding.txtUsernameInPublication.setText("@"+publicacion.getUsername_autor());
        binding.txtTextInPublication.setText(publicacion.getTexto());
        binding.dateInPublication.setText("22/05/2020");

        // SI LA PUBLICACIÓN NO ESTÁ VACÍA SE MUESTRA LA FOTO
        if(publicacion.getMultimedia()!=null){

            binding.multimediaInComment.setVisibility(View.VISIBLE);
        }

        // PARA BORRAR SOLO LAS PUBLICACIONES PROPIAS
        if(persona.getId_persona().equals(publicacion.getId_persona())){

            binding.btnDeletePublication.setVisibility(View.VISIBLE);
            binding.btnDeletePublication.setOnClickListener(view -> {

                myFirebaseHelper.eliminarPublicacion(publicacion.getId_publicacion()).addOnCompleteListener(new OnCompleteListener<Void>() {
                    @Override
                    public void onComplete(@NotNull Task<Void> task) {

                        if(task.isSuccessful()){

                            new KAlertDialog(binding.btnDeletePublication.getContext(), KAlertDialog.CUSTOM_IMAGE_TYPE)
                                    .setTitleText("Oookay!")
                                    .setContentText("Publicacion eliminada")
                                    .setCustomImage(R.drawable.ic_baseline_delete_24, binding.btnDeletePublication.getContext())
                                    .show();
                        }else{

                            new KAlertDialog(binding.btnDeletePublication.getContext(), KAlertDialog.CUSTOM_IMAGE_TYPE)
                                    .setTitleText("Ooops!")
                                    .setContentText("Error al borrar la publicación")
                                    .setCustomImage(R.drawable.ic_baseline_error_24, binding.btnDeletePublication.getContext())
                                    .show();
                        }
                    }
                });
            });
        }

        // PARA EL DESPLEGABLE DE LOS COMENTARIOS

        // SI ESTÁ DESPLEGADO LO CIERRA, SINO LO ABRE
        binding.txtCommentOnPublicationScroll.setHint("Comment to "+publicacion.getUsername_autor());
        binding.btnCommentInPublication.setOnClickListener(view -> {

            System.out.println("--------" + publicacion.toString());

            if(binding.textInputLayoutCommentOnPublicationScroll.getVisibility() == View.GONE){

                binding.textInputLayoutCommentOnPublicationScroll.setVisibility(View.VISIBLE);
                binding.txtCommentOnPublicationScroll.requestFocus();
            }else{

                binding.textInputLayoutCommentOnPublicationScroll.setVisibility(View.GONE);
            }

        });

    }
}

}

This is a photo where I clicked on the first comment button... Emulator img

Thank you in advance, I'm new here.



Solution 1:[1]

I fixed it!

The problem is using a binding in this case, so I replaced:

binding.multimediaInComment.setVisibility(View.VISIBLE)

for

Button multimediaInComment;
multimediaInComment = itemView.findViewById(R.id.multimediaInComment);

multimediaInComment.setVisibility(View.VISIBLE)

And now it's working :)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 HttpDefi