如何正确填充 RecyclerView?

How to correctly populate RecyclerView?

我正在尝试创建一个简单的应用程序,其中在主要 activity 中有一个 ViewPager2,其中 3 个页面链接到一个 TabLayout。在每个页面中都有一个由卡片填充的 RecyclerView。出于测试目的,我尝试用相同的 3 张卡片填充每个页面,但由于某种原因,在第一页我得到了 3 张卡片,但在下一页我得到了 3 张卡片,但是有很多空白 space 在每张卡片之间,所以你一次只能看到一张卡片,我不明白为什么会这样。如果我使用 4 张或更多而不是 3 张卡片,在第一页中,当我向下滚动时它看起来不错,但在我向上滚动后它与其他页面有同样的问题,很多空白 space。这是我的代码:

public class MainActivity extends AppCompatActivity {

    String data[] = {"Destaque", "Perto de Si", "Brevemente"};
    String evento[] = {"Jola", "Bowling", "Concerto"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ViewPager2 viewPager = findViewById(R.id.pager);

        viewPager.setAdapter( new SwipeAdapter(this, evento));

        TabLayout tabLayout = findViewById(R.id.tabLayout);

        new TabLayoutMediator(
                tabLayout,
                viewPager,
                new TabLayoutMediator.TabConfigurationStrategy() {
                    @Override
                    public void onConfigureTab(@NonNull TabLayout.Tab tab, int position) {
                        tab.setText(data[position]);
                    }
                }
        ).attach();
    }

//

public class SwipeAdapter extends FragmentStateAdapter {

    private String evento[];


    public SwipeAdapter(MainActivity mainActivity, String evento[]) {
        super(mainActivity);
        this.evento = evento;
    }

    @NonNull
    @Override
    public Fragment createFragment(int position) {

        return new RecyclerFragment(evento);
    }

    @Override
    public int getItemCount() {
        return 3;
    }
}

//

public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.ViewHolder> {

    String eventos[];

    RecyclerAdapter(String eventos[]){

        this.eventos = eventos;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        Context context = parent.getContext();
        LayoutInflater inflater = LayoutInflater.from(context);

        // Inflate the custom layout
        View cardView = inflater.inflate(R.layout.card_layout, parent, false);

        // Return a new holder instance
        return new ViewHolder(cardView);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {

        String evento = eventos[position];

        TextView textView = holder.textView;
        ImageView imageView = holder.imageView;

        textView.setText(evento);
    }

    @Override
    public int getItemCount() {
        return eventos.length;
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {

        ImageView imageView;
        TextView textView;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);

            imageView = itemView.findViewById(R.id.card_image);
            textView = itemView.findViewById(R.id.card_text);
        }
    }
}
    
    }

//

public class RecyclerFragment extends Fragment {

    String eventos[];
    public RecyclerFragment(String eventos[]) {
        this.eventos = eventos;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_recycler, container, false);

        RecyclerView recyclerView = view.findViewById(R.id.recycler_fragment);

        RecyclerAdapter adapter = new RecyclerAdapter(eventos);

        recyclerView.setAdapter(adapter);

        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));

        return view;
    }
}

XML:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <androidx.viewpager2.widget.ViewPager2
        android:id="@+id/pager"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/tabLayout"
        app:layout_constraintVertical_bias="0.0" />

    <com.google.android.material.tabs.TabLayout
        android:id="@+id/tabLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="680dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.0">

        <com.google.android.material.tabs.TabItem
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Destaque" />

        <com.google.android.material.tabs.TabItem
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Perto de Si" />

        <com.google.android.material.tabs.TabItem
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Brevemente" />
    </com.google.android.material.tabs.TabLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

//

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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="match_parent"
    tools:context=".TestFragment">


    <com.google.android.material.card.MaterialCardView
        android:id="@+id/card_fragment"
        android:layout_width="match_parent"
        android:layout_height="300dp"
        android:layout_margin="16dp"
        app:cardElevation="5dp">

        <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <ImageView
                android:id="@+id/card_image"
                android:layout_width="match_parent"
                android:layout_height="200dp"
                android:layout_marginStart="8dp"
                android:layout_marginTop="8dp"
                android:layout_marginEnd="8dp"
                android:src="@mipmap/beer"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent" />

            <TextView
                android:id="@+id/card_text"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="8dp"
                android:text="TextView"
                app:layout_constraintStart_toStartOf="@+id/card_image"
                app:layout_constraintTop_toBottomOf="@+id/card_image" />

        </androidx.constraintlayout.widget.ConstraintLayout>

    </com.google.android.material.card.MaterialCardView>

</FrameLayout>

//

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".RecyclerFragment">

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

</FrameLayout>

card_layout 的变化。xml
framelayout身高android:layout_height="wrap_content"

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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">


    <com.google.android.material.card.MaterialCardView
        android:id="@+id/card_fragment"
        android:layout_width="match_parent"
        android:layout_height="300dp"
        android:layout_margin="16dp"
        app:cardElevation="5dp">

        <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <ImageView
                android:id="@+id/card_image"
                android:layout_width="match_parent"
                android:layout_height="200dp"
                android:layout_marginStart="8dp"
                android:layout_marginTop="8dp"
                android:layout_marginEnd="8dp"
                android:src="@mipmap/ic_launcher"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent" />

            <TextView
                android:id="@+id/card_text"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="8dp"
                android:text="TextView"
                app:layout_constraintStart_toStartOf="@+id/card_image"
                app:layout_constraintTop_toBottomOf="@+id/card_image" />

        </androidx.constraintlayout.widget.ConstraintLayout>

    </com.google.android.material.card.MaterialCardView>

</FrameLayout>