片段内的片段不适用于 ScrollView

Fragment Inside a Fragment Not Working with ScrollView

当我在带有 ScrollView 的片段中使用片段时它不起作用。 在我的案例中 FirstFragment 没问题。里面的片段没有全屏显示,BooksFragment 中的很多内容都消失了

这是我的 FirstFragment 的 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:background="#DADADA"
tools:context=".FirstFragment">

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">


        <com.google.android.material.tabs.TabLayout
            android:id="@+id/tabLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentStart="true"
            android:layout_alignParentTop="true"
            android:layout_alignParentEnd="true"
            app:tabGravity="fill"
            app:tabMode="fixed">

        </com.google.android.material.tabs.TabLayout>

        <androidx.viewpager2.widget.ViewPager2
            android:id="@+id/viewPager"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/tabLayout"
            android:layout_alignParentStart="true"
            android:layout_alignParentEnd="true" />

    </RelativeLayout>

</ScrollView>

这是我的 FirstFragment 的 JAVA :

public class FirstFragment extends Fragment {


public FirstFragment() {
    // Required empty public constructor
}


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

}

static class ViewPagerAdapter extends FragmentStateAdapter {
    private final List<Fragment> fragmentList = new ArrayList<>();
    private final List<String> fragmentTitleList = new ArrayList<>();

    public ViewPagerAdapter(@NonNull FragmentManager fragmentManager, @NonNull Lifecycle lifecycle) {
        super(fragmentManager, lifecycle);
    }

    public void addFragment(Fragment fragment, String title) {
        fragmentList.add(fragment);
        fragmentTitleList.add(title);
    }

    @NonNull
    @Override
    public Fragment createFragment(int position) {
        return fragmentList.get(position);
    }

    @Override
    public int getItemCount() {
        return fragmentList.size();
    }
}

ViewPagerAdapter adapter;
ViewPager2 viewPager;
TabLayout tabLayout;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment

    View view = inflater.inflate(R.layout.fragment_first, container, false);
    viewPager = view.findViewById(R.id.viewPager);
    tabLayout = view.findViewById(R.id.tabLayout);
    initView();
    return view;
}

private void initView() {
    adapter = new ViewPagerAdapter(getActivity().getSupportFragmentManager(), getActivity().getLifecycle());
    adapter.addFragment(new BooksFragment(), "Books");
    adapter.addFragment(new ComputerFragment(), "Computers");
    viewPager.setAdapter(adapter);

    new TabLayoutMediator(tabLayout, viewPager, ((tab, position) -> {
        tab.setText(adapter.fragmentTitleList.get(position));
    })).attach();
}

}

这是我的 BooksFragment 的 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:id="@+id/frameLayout2"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".BooksFragment">

<TextView
    android:id="@+id/textView"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:text="lorem ipsum lorem ipsum how sldj as kye akd lorem ipsum lorem ipsum how sldj as kye akd lorem ipsum lorem ipsum how sldj as kye akd lorem ipsum lorem ipsum how sldj as kye akdlorem ipsum lorem ipsum how sldj as kye akd lorem ipsum lorem ipsum how sldj as kye akd lorem ipsum lorem ipsum how sldj as kye akd lorem ipsum lorem ipsum how sldj as kye akd lorem ipsum lorem ipsum how sldj as kye akd lorem ipsum lorem ipsum how sldj as kye akd lorem ipsum lorem ipsum how sldj as kye akd lorem ipsum lorem ipsum how sldj as kye akd lorem ipsum lorem ipsum how sldj as kye akd lorem ipsum lorem ipsum how sldj as kye akd lorem ipsum lorem ipsum how sldj as kye akd lorem ipsum lorem ipsum how sldj as kye akd lorem ipsum lorem ipsum how sldj as kye akd"
    android:lineSpacingExtra="20dp"
    android:textSize="20sp"
    android:layout_margin="16dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

 </androidx.constraintlayout.widget.ConstraintLayout>

这个问题如果用简单的词来说就是这样:

My First Fragment is Fine but Inside Fragment's Content is not shown so How can I fix that? Any Help Will be Appreciated. If you have any doubts or question related to my post you can tell me.

示例:

你们已经知道我在 BooksFragment 的 Xml 中有很长的文字: 但在这张图片中你明白了。

在这张图片中连一个文字都没有显示

在这张图片中显示文字 因为它的文字长度非常小。

警报:

Please Don't Think that grey background is his (booksFragment,ComputerFragment). FirstFragment's Bg is Grey! not BooksFragment and ComputerFragment

已编辑:

我添加了不同颜色的这张图片,这样你就知道什么是什么尺寸了。

这里是颜色定义

你的代码工作正常,但你的代码有一些小错误,所以你可以按照这个代码 -

<?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:background="#BBF8FF"
tools:context=".FirstFragment">

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">


        <com.google.android.material.tabs.TabLayout
            android:id="@+id/tabLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:tabGravity="fill"
            android:layout_alignParentTop="true"
            android:layout_alignParentEnd="true"
            android:layout_alignParentStart="true"
            app:tabMode="fixed"/>

        <androidx.viewpager2.widget.ViewPager2
            android:id="@+id/viewPager"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_below="@id/tabLayout"
            android:layout_alignParentStart="true"
            android:layout_alignParentEnd="true"
            android:layout_alignParentBottom="true"/>

      </RelativeLayout>

  </ScrollView>
 </androidx.constraintlayout.widget.ConstraintLayout>