Fragment 中的 NestedScrollView 中的文本在我第二次打开 Fragment 时重叠。第一次它工作正常

Text in NestedScrollView in Fragment overlaps the 2nd time that I open the Fragment. First time it works fine

我在 ScrollView 中有一个 NestedScrollView,因为我有一个 Activity,其中包含三个隐藏的 fragments,其中包含文本,当您单击其中一个时它打开以显示文本的片段。但问题是有相当多的文本所以我使用 NestedScrollView 所以 reader 可以读取所有文本,并且第一次它工作正常(文本不重叠),然后第二次,当我关闭片段并重新打开它时,文本开始重叠...

如前所述,第一次没有发生,然后我关闭片段并重新打开它,然后发生了这种情况:

为什么会发生这种情况,我该如何解决?

里面的片段Activity

<androidx.core.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:scrollbars="none"
    tools:context=".Fragment.PrivacyFragment">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="22dp"
            android:layout_marginTop="5dp"
            android:padding="8dp"
            android:textColor="@color/colorBlack"
            android:textSize="22sp"
            android:textStyle="bold" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="22dp"
            android:layout_marginTop="5dp"
            android:layout_marginEnd="20dp"
            android:padding="8dp"
            android:textColor="@color/colorBlack"
            android:textSize="18sp"
            android:textStyle="bold" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="22dp"
            android:layout_marginEnd="20dp"
            android:padding="8dp"
            android:textColor="@color/colorBlack"
            android:textSize="16sp" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="22dp"
            android:layout_marginEnd="20dp"
            android:padding="8dp"
            android:textColor="@color/colorBlack"
            android:textSize="18sp"
            android:textStyle="bold" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="22dp"
            android:layout_marginEnd="20dp"
            android:padding="8dp"
            android:textColor="@color/Grey"
            android:textSize="18sp"
            android:textStyle="bold" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="22dp"
            android:layout_marginEnd="20dp"
            android:padding="8dp"
            android:textColor="@color/colorBlack"
            android:textSize="16sp" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="22dp"
            android:layout_marginEnd="20dp"
            android:padding="8dp"
            android:textColor="@color/Grey"
            android:textSize="18sp"
            android:textStyle="bold" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="22dp"
            android:layout_marginEnd="20dp"
            android:padding="8dp"
            android:textColor="@color/colorBlack"
            android:textSize="16sp" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="22dp"
            android:layout_marginEnd="20dp"
            android:padding="8dp"
            android:textColor="@color/Grey"
            android:textSize="18sp"
            android:textStyle="bold" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="22dp"
            android:layout_marginEnd="20dp"
            android:padding="8dp"
            android:textColor="@color/colorBlack"
            android:textSize="16sp" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="22dp"
            android:layout_marginTop="5dp"
            android:layout_marginEnd="20dp"
            android:padding="8dp"
            android:textColor="@color/colorBlack"
            android:textSize="18sp"
            android:textStyle="bold" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="22dp"
            android:layout_marginEnd="20dp"
            android:padding="8dp"
            android:textColor="@color/colorBlack"
            android:textSize="16sp" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="22dp"
            android:layout_marginTop="5dp"
            android:layout_marginEnd="20dp"
            android:padding="8dp"
            android:textColor="@color/colorBlack"
            android:textSize="18sp"
            android:textStyle="bold" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="22dp"
            android:layout_marginEnd="20dp"
            android:padding="8dp"
            android:textColor="@color/colorBlack"
            android:textSize="16sp" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="22dp"
            android:layout_marginTop="5dp"
            android:layout_marginEnd="20dp"
            android:padding="8dp"
            android:textColor="@color/colorBlack"
            android:textSize="18sp"
            android:textStyle="bold" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="22dp"
            android:layout_marginEnd="20dp"
            android:padding="8dp"
            android:textColor="@color/colorBlack"
            android:textSize="16sp" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="22dp"
            android:layout_marginTop="5dp"
            android:layout_marginEnd="20dp"
            android:padding="8dp"
            android:textColor="@color/colorBlack"
            android:textSize="18sp"
            android:textStyle="bold" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="22dp"
            android:layout_marginEnd="20dp"
            android:padding="8dp"
            android:textColor="@color/colorBlack"
            android:textSize="16sp" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="22dp"
            android:layout_marginTop="5dp"
            android:layout_marginEnd="20dp"
            android:padding="8dp"
            android:textColor="@color/colorBlack"
            android:textSize="18sp"
            android:textStyle="bold" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="22dp"
            android:layout_marginEnd="20dp"
            android:padding="8dp"
            android:textColor="@color/colorBlack"
            android:textSize="16sp" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="22dp"
            android:layout_marginTop="5dp"
            android:layout_marginEnd="20dp"
            android:padding="8dp"
            android:textColor="@color/colorBlack"
            android:textSize="18sp"
            android:textStyle="bold" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="22dp"
            android:layout_marginEnd="20dp"
            android:padding="8dp"
            android:textColor="@color/colorBlack"
            android:textSize="16sp" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="22dp"
            android:layout_marginTop="5dp"
            android:layout_marginEnd="20dp"
            android:padding="8dp"
            android:textColor="@color/colorBlack"
            android:textSize="18sp"
            android:textStyle="bold" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="22dp"
            android:layout_marginEnd="20dp"
            android:padding="8dp"
            android:textColor="@color/colorBlack"
            android:textSize="16sp" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="22dp"
            android:layout_marginTop="5dp"
            android:layout_marginEnd="20dp"
            android:padding="8dp"
            android:textColor="@color/colorBlack"
            android:textSize="18sp"
            android:textStyle="bold" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="22dp"
            android:layout_marginEnd="20dp"
            android:padding="8dp"
            android:textColor="@color/colorBlack"
            android:textSize="16sp" />

    </LinearLayout>

</androidx.core.widget.NestedScrollView>

Activity

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/button_gradient_two">

    <RelativeLayout
        android:id="@+id/relative_layout_one"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="10dp"
        android:layout_marginTop="25dp"
        android:layout_marginEnd="10dp">

    </RelativeLayout>

    <ScrollView
        android:id="@+id/scroll_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:scrollbars="none">

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

            <ImageView
                android:id="@+id/arrow_back"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginStart="10dp"
                android:layout_marginTop="25dp"
                android:padding="8dp"
                android:src="@drawable/icon_search_100" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerHorizontal="true"
                android:layout_marginTop="25dp"
                android:padding="8dp"
                android:text="Settings"
                android:textColor="@color/White"
                android:textSize="22sp"
                android:textStyle="bold" />

            <de.hdodenhof.circleimageview.CircleImageView
                android:id="@+id/image_profile"
                android:layout_width="100dp"
                android:layout_height="100dp"
                android:layout_centerHorizontal="true"
                android:layout_marginTop="100dp"
                android:elevation="16dp"
                android:src="@drawable/profile_placeholder" />

            <com.google.android.material.card.MaterialCardView
                style="@style/CustomCardViewStyle"
                android:layout_width="match_parent"
                android:layout_height="1000dp"
                android:layout_marginTop="150dp"
                app:cardElevation="12dp">

                <RelativeLayout
                    android:id="@+id/relative_layout_two"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content">

                    <TextView
                        android:id="@+id/name"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_centerHorizontal="true"
                        android:layout_marginTop="50dp"
                        android:gravity="center_horizontal"
                        android:text="Jack Sparrow"
                        android:textColor="@color/Black"
                        android:textSize="32sp"
                        android:textStyle="bold" />

                    <TextView
                        android:id="@+id/username"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_below="@+id/name"
                        android:layout_centerHorizontal="true"
                        android:gravity="center_horizontal"
                        android:text="jacksparrow"
                        android:textColor="@color/Grey"
                        android:textSize="22sp"
                        android:textStyle="" />

                    <TextView
                        android:id="@+id/bio"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_below="@+id/username"
                        android:layout_centerHorizontal="true"
                        android:layout_marginTop="4dp"
                        android:gravity="center_horizontal"
                        android:text="This is my bio!"
                        android:textColor="@color/Grey"
                        android:textStyle="" />

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_below="@+id/bio"
                        android:orientation="vertical">

                        <TextView
                            android:id="@+id/privacy"
                            style="@style/SelectableItemBackground"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:padding="12dp"
                            android:text="@string/privacy"
                            android:textColor="@color/colorBlack"
                            android:textSize="18sp" />

                        <FrameLayout
                            android:id="@+id/container_privacy"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:visibility="gone" />

                        <TextView
                            android:id="@+id/security"
                            style="@style/SelectableItemBackground"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:padding="12dp"
                            android:text="@string/security"
                            android:textColor="@color/colorBlack"
                            android:textSize="18sp" />

                        <FrameLayout
                            android:id="@+id/container_security"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:visibility="gone" />

                        <TextView
                            android:id="@+id/help"
                            style="@style/SelectableItemBackground"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:padding="12dp"
                            android:text="@string/help"
                            android:textColor="@color/colorBlack"
                            android:textSize="18sp" />

                        <FrameLayout
                            android:id="@+id/container_help"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:visibility="gone" />

                        <TextView
                            android:id="@+id/about"
                            style="@style/SelectableItemBackground"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:padding="12dp"
                            android:text="@string/about"
                            android:textColor="@color/colorBlack"
                            android:textSize="18sp" />

                        <FrameLayout
                            android:id="@+id/container_about"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:visibility="gone" />

                    </LinearLayout>

                </RelativeLayout>

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

        </RelativeLayout>

    </ScrollView>

</RelativeLayout>

设置Activity

public class SettingsActivity extends AppCompatActivity {

private ImageView mImageProfile, mBackArrow;
private TextView mName, mUsername, mBio, mPrivacy, mSecurity, mHelp, mAbout;
private FirebaseUser mFirebaseUser;
private FrameLayout mPrivacyContainer, mSecurityContainer, mHelpContainer, mAboutContainer;

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

        mFirebaseUser = FirebaseAuth.getInstance().getCurrentUser();

        mImageProfile = findViewById(R.id.image_profile);
        mName = findViewById(R.id.name);
        mUsername = findViewById(R.id.username);
        mBio = findViewById(R.id.bio);
        mPrivacy = findViewById(R.id.privacy);
        mSecurity = findViewById(R.id.security);
        mHelp = findViewById(R.id.help);
        mAbout = findViewById(R.id.about);

        mImageProfile.setAnimation(AnimationUtils.loadAnimation(getApplicationContext(), R.anim.profile_animation_profile_picture));
        mName.setAnimation(AnimationUtils.loadAnimation(getApplicationContext(), R.anim.profile_animation_name));
        mUsername.setAnimation(AnimationUtils.loadAnimation(getApplicationContext(), R.anim.profile_animation_username));
        mBio.setAnimation(AnimationUtils.loadAnimation(getApplicationContext(), R.anim.profile_animation_bio));

        mPrivacyContainer = findViewById(R.id.container_privacy);
        mSecurityContainer = findViewById(R.id.container_security);
        mHelpContainer = findViewById(R.id.container_help);
        mAboutContainer = findViewById(R.id.container_about);

        mPrivacy.setOnClickListener(v -> {
            getSupportFragmentManager().beginTransaction().add(R.id.container_privacy, new PrivacyFragment(), null).addToBackStack(null).commit();
            if (mPrivacyContainer.getVisibility() == View.GONE) {
                mPrivacyContainer.setVisibility(View.VISIBLE);
            } else {
                mPrivacyContainer.setVisibility(View.GONE);
            }
        });

        mSecurity.setOnClickListener(v -> {
            getSupportFragmentManager().beginTransaction().add(R.id.container_security, new SecurityFragment(), null).addToBackStack(null).commit();
            if (mSecurityContainer.getVisibility() == View.GONE) {
                mSecurityContainer.setVisibility(View.VISIBLE);
            } else {
                mSecurityContainer.setVisibility(View.GONE);
            }
        });

        mHelp.setOnClickListener(v -> {
            getSupportFragmentManager().beginTransaction().add(R.id.container_help, new HelpFragment(), null).addToBackStack(null).commit();
            if (mHelpContainer.getVisibility() == View.GONE) {
                mHelpContainer.setVisibility(View.VISIBLE);
            } else {
                mHelpContainer.setVisibility(View.GONE);
            }
        });

        mAbout.setOnClickListener(v -> {
            getSupportFragmentManager().beginTransaction().add(R.id.container_about, new AboutFragment(), null).addToBackStack(null).commit();
            if (mAboutContainer.getVisibility() == View.GONE) {
                mAboutContainer.setVisibility(View.VISIBLE);
            } else {
                mAboutContainer.setVisibility(View.GONE);
            }
        });

        getUserInfo();
        setBackArrow();
    }

    private void getUserInfo() {
        DatabaseReference reference = FirebaseDatabase.getInstance().getReference("Users").child(mFirebaseUser.getUid());
        reference.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                User user = dataSnapshot.getValue(User.class);
                if (user != null) {
                    mName.setText(user.getFullname());
                    mUsername.setText(user.getUsername());
                    mBio.setText(user.getBio());

                    if (user.getImageurl() == null) {
                        mImageProfile.setImageResource(R.drawable.profile_placeholder);
                    } else {
                        Glide.with(getApplicationContext()).load(user.getImageurl()).into(mImageProfile);
                    }
                }
            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {

            }
        });
    }

    private void setBackArrow() {
        mBackArrow = findViewById(R.id.arrow_back);
        mBackArrow.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }
}



  

您正在添加两个彼此重叠的片段。 在执行片段事务时,使用 replace 而不是 add

发件人:

getSupportFragmentManager().beginTransaction().add(R.id.container_privacy, new PrivacyFragment(), null).addToBackStack(null).commit();
            if (mPrivacyContainer.getVisibility() == View.GONE) {
                mPrivacyContainer.setVisibility(View.VISIBLE);
            } else {
                mPrivacyContainer.setVisibility(View.GONE);
            }

收件人:

getSupportFragmentManager().beginTransaction().replace(R.id.container_privacy, new PrivacyFragment(), null).addToBackStack(null).commit();
            if (mPrivacyContainer.getVisibility() == View.GONE) {
                mPrivacyContainer.setVisibility(View.VISIBLE);
            } else {
                mPrivacyContainer.setVisibility(View.GONE);
            }

你得到的片段是重叠的,因为每次都会添加一个新的片段。

您只需检查片段是否已经存在,就无需添加新片段。您可以在添加片段时分配一个 'tag'。代码如下-

if (fragmentManager.findFragmentByTag("First Fragment") == null)
         fragmentManager.beginTransaction().add(R.id.fragment, new FirstFragment(), "First Fragment").commit();

然后使用 .add() 方法使用 .replace() 确保片段每次都被替换。

根据您的 Activity 代码,您需要选择正确的方式,但要确保在不破坏前一个片段的情况下不会重新创建片段。

编码愉快!!