如何解决RecyclerView 和 GONE 可见性问题
我遇到了 RecyclerView 及其 CardView 某些视图的可见性问题。
正如您在图 1 和图 2 中看到的,我有一个 CardView,其中有一些 TextViews 在一个 RelativeLayout 和一个 ImageButton,它在点击时,将这个 RelativeLayout 的可见性更改为 GONE,并使用一组 EditTexts 设置 VISIBLE 一个 LinearLayout。
问题是 RecyclerView 在 CardViews 之间留出了空间,尽管它们没有使用它。
卡片视图的代码如下:
<?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 是这样的:
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rv_addresses"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@null"/>
而控制可见性的适配器是:
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);
}
}
}
希望有人能帮助我。
解决方法
您的 LinearLayout
的高度应为 wrap_content
而不是 match_parent
。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。