View.GONE 留下空白空间

Max*_*tov 8 android adapter android-recyclerview

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

这就是我得到的

适配器:

@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);
    }
}
Run Code Online (Sandbox Code Playgroud)

行布局相同-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>
Run Code Online (Sandbox Code Playgroud)

主要活动 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>
Run Code Online (Sandbox Code Playgroud)

Max*_*tov 7

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

\n\n
viewHolder.itemView.setLayoutParams(new RecyclerView.LayoutParams(0, 0));\n
Run Code Online (Sandbox Code Playgroud)\n