I am having an issue with RecyclerView and the visibility of some views of their CardViews.
As you can see in images 1 and 2, I have a CardView with some TextViews in a RelativeLayout and an ImageButton that onClick, change the visibility of this RelativeLayout to GONE, and set VISIBLE a LinearLayout with a set of EditTexts.
The problem is that the RecyclerView is leaving space between the CardViews although they are not using it.
The code of the Card View is the following:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingVertical="15dp"
android:paddingHorizontal="5dp">
<CheckBox
android:id="@+id/checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginEnd="5dp"/>
<RelativeLayout
android:id="@+id/addressInfo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toEndOf="@id/checkbox">
<ImageButton
android:id="@+id/editAddress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_edit"
android:background="@null"
android:layout_alignParentEnd="true"/>
<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="EMILI BELLOT"
android:textColor="@color/black"
android:textSize="18sp"
android:layout_marginBottom="5dp"/>
<TextView
android:id="@+id/address"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/name"
android:text="Carrer Churruca, 45"
android:textColor="@color/black"
android:textSize="14sp"/>
<TextView
android:id="@+id/CP"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/address"
android:text="08301"
android:textColor="@color/black"
android:textSize="14sp"
android:layout_marginEnd="10dp"/>
<TextView
android:id="@+id/city"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/address"
android:layout_toEndOf="@id/CP"
android:text="Mataró"
android:textColor="@color/black"
android:textSize="14sp"
android:layout_marginBottom="15dp"/>
<TextView
android:id="@+id/country"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/CP"
android:text="España"
android:textColor="@color/black"
android:textSize="14sp"
android:layout_marginEnd="10dp"/>
</RelativeLayout>
<ImageButton
android:id="@+id/hideModify"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_hide_desplegable"
android:background="@null"
android:layout_alignParentEnd="true"
android:visibility="gone"/>
<LinearLayout
android:id="@+id/modifyAddress"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_toEndOf="@id/checkbox"
android:visibility="gone">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/modifyAddress"
android:textColor="@color/black"
android:textStyle="bold"
android:textSize="16sp"
android:layout_marginEnd="10dp"/>
<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/nombre"
android:layout_marginHorizontal="20dp"
android:layout_marginVertical="10dp"
app:hintTextColor="@color/black">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/nameInput"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:drawablePadding="10dp"
android:maxLines="1"/>
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/apellido"
android:layout_marginHorizontal="20dp"
android:layout_marginVertical="10dp"
app:hintTextColor="@color/black">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/surnameInput"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:drawablePadding="10dp"
android:maxLines="1"/>
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/DireccionObligatoria"
android:layout_marginHorizontal="20dp"
android:layout_marginVertical="10dp"
app:hintTextColor="@color/black">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/addressInput"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:drawablePadding="10dp"
android:maxLines="1"/>
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/CodigoPostal"
android:layout_marginHorizontal="20dp"
android:layout_marginVertical="10dp"
app:hintTextColor="@color/black">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/cpInput"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:drawablePadding="10dp"
android:maxLines="1"/>
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/Poblacion"
android:layout_marginHorizontal="20dp"
android:layout_marginVertical="10dp"
app:hintTextColor="@color/black">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/cityInput"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:drawablePadding="10dp"
android:maxLines="1"/>
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/Pais"
android:layout_marginHorizontal="20dp"
android:layout_marginVertical="10dp"
app:hintTextColor="@color/black">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/countryInput"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:drawablePadding="10dp"
android:maxLines="1"/>
</com.google.android.material.textfield.TextInputLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="30dp"
android:paddingHorizontal="30dp"
android:text="@string/Obligatorio"/>
<Button
android:id="@+id/saveAddress"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/button_white"
android:text="@string/GuardarDireccion"
android:textColor="@color/primary"
android:textStyle="bold"
android:layout_margin="20dp"/>
</LinearLayout>
</RelativeLayout>
</androidx.cardview.widget.CardView>
</LinearLayout>
RecyclerView is like this:
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rv_addresses"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@null"/>
And the adapter that control the visibility is:
public class AdapterOrderAddress extends RecyclerView.Adapter<AdapterOrderAddress.ViewHolder> {
ArrayList<String> addressesKeyList;
ArrayList<Address> addresses;
Context context;
public AdapterOrderAddress(Context context, ArrayList<String> addressesKeyList, ArrayList<Address> addresses){
this.context = context;
this.addressesKeyList = addressesKeyList;
this.addresses = addresses;
}
@NonNull
@Override
public AdapterOrderAddress.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_address_order, parent,false);
return new AdapterOrderAddress.ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull AdapterOrderAddress.ViewHolder holder, int position) {
String key = addressesKeyList.get(position);
Address a = addresses.get(position);
String contact = a.getNombre() + " " + a.getApellidos();
holder.name.setText(contact.toUpperCase());
holder.address.setText(a.getDireccion());
holder.CP.setText(a.getCodigoPostal());
holder.city.setText(a.getPoblacion());
holder.country.setText(a.getPais());
holder.nameInput.setText(a.getNombre());
holder.surnameInput.setText(a.getApellidos());
holder.addressInput.setText(a.getDireccion());
holder.cpInput.setText(a.getCodigoPostal());
holder.cityInput.setText(a.getPoblacion());
holder.countryInput.setText(a.getPais());
holder.editAddress.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
holder.editAddress.setVisibility(View.GONE);
holder.addressInfo.setVisibility(View.GONE);
holder.hideModify.setVisibility(View.VISIBLE);
holder.modifyAddress.setVisibility(View.VISIBLE);
}
});
holder.hideModify.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
holder.hideModify.setVisibility(View.GONE);
holder.modifyAddress.setVisibility(View.GONE);
holder.editAddress.setVisibility(View.VISIBLE);
holder.addressInfo.setVisibility(View.VISIBLE);
}
});
}
@Override
public int getItemCount() {
return addresses.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
TextView name, address, CP, city, country;
TextInputEditText nameInput, surnameInput, addressInput, cpInput, cityInput, countryInput;
CheckBox checkBox;
ImageButton editAddress, hideModify;
RelativeLayout addressInfo;
LinearLayout modifyAddress;
public ViewHolder(@NonNull View itemView) {
super(itemView);
addressInfo = itemView.findViewById(R.id.addressInfo);
modifyAddress = itemView.findViewById(R.id.modifyAddress);
name = itemView.findViewById(R.id.name);
address = itemView.findViewById(R.id.address);
CP = itemView.findViewById(R.id.CP);
city = itemView.findViewById(R.id.city);
country = itemView.findViewById(R.id.country);
nameInput = itemView.findViewById(R.id.nameInput);
surnameInput = itemView.findViewById(R.id.surnameInput);
addressInput = itemView.findViewById(R.id.addressInput);
cpInput = itemView.findViewById(R.id.cpInput);
cityInput = itemView.findViewById(R.id.cityInput);
countryInput = itemView.findViewById(R.id.countryInput);
editAddress = itemView.findViewById(R.id.editAddress);
hideModify = itemView.findViewById(R.id.hideModify);
checkBox = itemView.findViewById(R.id.checkbox);
}
}
}
Hope someone can help me.
Your LinearLayout
should have a height of wrap_content
instead of match_parent
.