【问题标题】:Recyclerview is becoming small after switch Fragments切换 Fragments 后 Recyclerview 变小了
【发布时间】:2021-12-26 08:41:57
【问题描述】:

当我在片段之间切换时,我一直在尝试在片段中实现 recyclerview,如图所示,recyclerview 的大小正在减小

下面给出的代码我没有用户recyclerview.setHasFixedSize(true);故意的,因为如果我使用它,它不会从数据库中检索数据,从而使 recyclerview 为空

XML 代码

?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:background="@color/apptheme"
android:orientation="vertical"
tools:context=".ProgrammesAndSubjects.Subjects_Under_ProgrammeFragment">


<TextView
    android:id="@+id/add_subject"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="end"
    android:layout_marginEnd="@dimen/_7sdp"
    android:layout_marginTop="@dimen/_7sdp"
    android:includeFontPadding="false"
    android:text="Add \n Subjects"
    android:textAlignment="center"
    android:textColor="@color/appcolor"
    android:textSize="@dimen/_12sdp"
    android:textStyle="bold"
    tools:ignore="RtlCompat" />


<ProgressBar
    android:id="@+id/top_class"
    android:layout_width="50dp"
    android:layout_height="50dp"
    android:layout_gravity="center"
    android:outlineSpotShadowColor="@color/white" />


<androidx.recyclerview.widget.RecyclerView
    android:id="@+id/recyclerView_classes"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginBottom="@dimen/_6sdp"
    android:padding="10dp"

    android:divider="@null"
    android:scrollbars="vertical" />


<ProgressBar
    android:id="@+id/bottomprogressbar_classes"
    android:layout_width="50dp"
    android:layout_height="50dp"
    android:layout_gravity="center"
    android:layout_marginTop="-40dp"
    android:outlineSpotShadowColor="@color/white" />

 </LinearLayout>

JAVA 代码

  @Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);


    addsubjects = getView().findViewById(R.id.add_subject);
    progressBar = getView().findViewById(R.id.top_class);
    bottomProgressbar = getView().findViewById(R.id.bottomprogressbar_classes);
    recyclerView = getView().findViewById(R.id.recyclerView_classes);

    RecyclerView.LayoutManager mlayoutManager = new LinearLayoutManager(getContext());

    recyclerView.setLayoutManager(mlayoutManager);

}

**它有一个已设置的普通适配器**

【问题讨论】:

    标签: android android-fragments android-recyclerview


    【解决方案1】:

    当我在 Fragments 之间切换时 reyclerview 的大小正在减小 Fragments reyclerview 的大小正在减小

    不是recyclerView 正在调整大小,而是viewPager 的页面在您切换到其他页面时正在调整大小。在你切换到Student's 页面后,它会调整到那个大小,然后它的大小会保持不变并且不会变回全高。


    要解决此问题,您必须手动设置页面高度,因为 viewPager 不会自行设置。

    由于您没有标记任何viewPager 标签,我希望它是ViewPager2。您可以使用ViewPager2 中的setPageTransformer 方法来设置新的页面大小。

    viewPager2.setPageTransformer((page, position) -> page.post(() -> {
                int wMeasureSpec = View.MeasureSpec
                        .makeMeasureSpec(page.getWidth(), View.MeasureSpec.EXACTLY);
    
                int hMeasureSpec = View.MeasureSpec
                        .makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
    
                page.measure(wMeasureSpec, hMeasureSpec);
                ViewGroup.LayoutParams layoutParams = viewPager2.getLayoutParams();
                layoutParams.height = page.getMeasuredHeight();
                viewPager2.setLayoutParams (layoutParams);
                viewPager2.invalidate();
            }));
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多