gpt4 book ai didi

android - View.GONE 留下空白空间

转载 作者:行者123 更新时间:2023-12-03 23:09:24 25 4
gpt4 key购买 nike

我使用 Firebase 和 RadioButton 获得 2 种类型的数据,我对显示的数据进行排序。一切似乎都正常,但是当隐藏其中一种数据类型时仍然有一个空白空间。告诉我如何正确隐藏/显示数据。

That's what I get

适配器:

@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int
viewType) {
switch (viewType) {
case Person.PersonType.TYPE_1:
View userType1 = LayoutInflater.from(parent.getContext())
.inflate(R.layout.driver_row, parent, false);
return new PersonType1ViewHolder(userType1);
case Person.PersonType.TYPE_2:
View userType2 = LayoutInflater.from(parent.getContext())
.inflate(R.layout.fare_row, parent, false);
return new PersonType2ViewHolder(userType2);
}
return super.onCreateViewHolder(parent, viewType);
}

@Override
protected void populateViewHolder(RecyclerView.ViewHolder viewHolder,
Person model,
int position) {
if (viewHolder instanceof PersonType1ViewHolder) {
((PersonType1ViewHolder) viewHolder).time.setText("Full time: " +
model.getTime());
if (activate) {
viewHolder.itemView.setVisibility(View.GONE);
} else {
viewHolder.itemView.setVisibility(View.VISIBLE);
}
return;
}
if (viewHolder instanceof PersonType2ViewHolder) {
((PersonType2ViewHolder) viewHolder).time.setText("Full time2: " +
model.getTime());
if (activate2) {
viewHolder.itemView.setVisibility(View.GONE);
} else {
viewHolder.itemView.setVisibility(View.VISIBLE);
}
private class PersonType1ViewHolder extends RecyclerView.ViewHolder {
TextView time;
PersonType1ViewHolder(View itemView) {
super(itemView);
time = itemView.findViewById(R.id.tv_time);
}
}

private class PersonType2ViewHolder extends RecyclerView.ViewHolder {
TextView time;
PersonType2ViewHolder(View itemView) {
super(itemView);
time = itemView.findViewById(R.id.tv_time_two);
}
}

行布局相同 - wrap_content
<androidx.cardview.widget.CardView 
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">

<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">

<TextView
android:id="@+id/tv_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />

</RelativeLayout>
</androidx.cardview.widget.CardView>

主 Activity XML
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:context=".MainActivity">

<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rv_trip_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@id/radio_button_group" />

<Button
android:id="@+id/add_trip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:onClick="addTrip"
android:text="add" />

<RadioGroup
android:id="@+id/radio_button_group"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@id/add_trip"
android:layout_centerHorizontal="true"
android:orientation="horizontal">

<RadioButton
android:id="@+id/rb_all"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="all" />

<RadioButton
android:id="@+id/rb_driver"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="driver" />

<RadioButton
android:id="@+id/rb_fare"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="fare" />
</RadioGroup>

</RelativeLayout>

最佳答案

我不知道这有多正确,但我只是以编程方式设置参数

viewHolder.itemView.setLayoutParams(new RecyclerView.LayoutParams(0, 0));

关于android - View.GONE 留下空白空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59672835/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com