java - RecyclerView 和 GONE 可见性问题
问题描述
我遇到了 RecyclerView 问题以及他们的 CardViews 的某些视图的可见性。
正如您在图像 1 和 2 中看到的那样,我有一个 CardView,在 RelativeLayout 中有一些 TextViews 和一个 onClick 的 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
。
推荐阅读
- sass - 一系列属性作为映射值
- vue.js - Vue js将HTML按钮名称属性作为PROPS传递
- vue.js - VueJS将计算数据作为道具传递返回未定义
- c# - 基于对象参数的模拟方法返回
- svg - 如何检测 SVG 是否包含特定符号?
- probability - 使用唯一 32 字节数组的 32 位 CRC 的冲突概率
- java - Java XML 文件编辑
- mysql - 如何将文件从C复制到网络文件夹?
- nullpointerexception - 为什么当表格单元格内容溢出到下一页时,isSplitTypePreventInhibited 中会抛出 NullPointer 异常?
- android - Android - 在设置应用程序的可访问性类别中以编程方式更改“隐藏状态栏”设置