Java RecyclerView和GONE可见性问题
我对RecyclerView及其CardView的某些视图的可见性有问题 正如您在图1和图2中看到的,我有一个CardView,在RelativeLayout中有一些文本视图,还有一个ImageButton,它可以单击,将此RelativeLayout的可见性更改为GONE,并使用一组EditText设置一个可见的LinearLayout 问题是RecyclerView在CardView之间留有空间,尽管他们不使用它 卡片视图的代码如下所示:Java RecyclerView和GONE可见性问题,java,android-recyclerview,visibility,cardview,Java,Android Recyclerview,Visibility,Cardview,我对RecyclerView及其CardView的某些视图的可见性有问题 正如您在图1和图2中看到的,我有一个CardView,在RelativeLayout中有一些文本视图,还有一个ImageButton,它可以单击,将此RelativeLayout的可见性更改为GONE,并使用一组EditText设置一个可见的LinearLayout 问题是RecyclerView在CardView之间留有空间,尽管他们不使用它 卡片视图的代码如下所示: <?xml version="
<?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);
}
}
}
公共类适配器OrderAddress扩展了RecyclerView.Adapter{
ArrayList地址KeyList;
数组列表地址;
语境;
公共适配器OrderAddress(上下文上下文、ArrayList地址KeyList、ArrayList地址){
this.context=上下文;
this.addressesKeyList=addressesKeyList;
this.address=地址;
}
@非空
@凌驾
public AdapterOrderAddress.ViewHolder onCreateViewHolder(@NonNull ViewGroup父级,int-viewType){
View=LayoutFlater.from(parent.getContext()).flate(R.layout.row\u address\u order,parent,false);
返回新的AdapterOrderAddress.ViewHolder(视图);
}
@凌驾
BindViewHolder上的公共无效(@NonNull AdapterOrderAddress.ViewHolder,int位置){
String key=addressesKeyList.get(位置);
地址a=地址。获取(位置);
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.input.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(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
holder.editAddress.setVisibility(View.GONE);
holder.addressInfo.setVisibility(View.GONE);
holder.hideModify.setVisibility(View.VISIBLE);
holder.modifyAddress.setVisibility(View.VISIBLE);
}
});
holder.hideModify.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
holder.hideModify.setVisibility(View.GONE);
holder.modifyAddress.setVisibility(View.GONE);
holder.editAddress.setVisibility(View.VISIBLE);
holder.addressInfo.setVisibility(View.VISIBLE);
}
});
}
@凌驾
public int getItemCount(){
返回地址。size();
}
公共类ViewHolder扩展了RecyclerView.ViewHolder{
TextView名称、地址、CP、城市、国家;
文本输入文本名称输入、姓氏输入、地址输入、cpInput、城市输入、国家输入;
复选框;
ImageButton编辑地址,隐藏修改;
相对地址信息;
线性布局修改地址;
公共视图持有者(@NonNull View itemView){
超级(项目视图);
addressInfo=itemView.findViewById(R.id.addressInfo);
modifyAddress=itemView.findViewById(R.id.modifyAddress);
name=itemView.findviewbyd(R.id.name);
地址=itemView.findViewById(R.id.address);
CP=itemView.findviewbyd(R.id.CP);
city=itemView.findviewbyd(R.id.city);
country=itemView.findviewbyd(R.id.country);
nameInput=itemView.findviewbyd(R.id.nameInput);
姓氏输入=itemView.findViewById(R.id.namesinput);
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.findviewbyd(R.id.hideModify);
checkBox=itemView.findviewbyd(R.id.checkBox);
}
}
}
希望有人能帮助我。您的
线性布局
的高度应为包裹内容
,而不是匹配父项