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

huangapple go评论83阅读模式
英文:

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

问题

I've got a NestedScrollView inside of a ScrollView because I have an Activity with three hidden fragments that contain text, and when you click one of the fragments it opens to reveal the text. But the thing is that there is quite a bit of text so I'm using NestedScrollView so the reader can read all of it, and the first time it works fine (text doesn't overlap), and then the second time when I close the fragment and re-open it the text starts overlapping...

So as mentioned, the first time it doesn't happen, and then I close the fragment and reopen it, and this happens:

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

So why is this happening and how can I fix it?

Fragment inside 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">

    <!-- Your nested scroll view content here -->

</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">

    <!-- Your activity layout content here -->

</RelativeLayout>

SettingsActivity.java

public class SettingsActivity extends AppCompatActivity {

    // Your Java code here

}
英文:

I've got a NestedScrollView inside of a ScrollView because I have an Activity with three hidden fragments that contain text, and when you click one of the fragments it opens to reveal the text. But the thing is that there is quite a bit of text so I'm using NestedScrollView so the reader can read all of it, and the first time it works fine (text doesn't overlap), and then second time when I close the fragment and re-open it the text starts overlapping...

So as mentioned, first time it doesn't happen, and then I close the fragment and reopen it and this happens:

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

So why is this happening and how can I fix it?

Fragment inside Activity

&lt;androidx.core.widget.NestedScrollView xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
xmlns:tools=&quot;http://schemas.android.com/tools&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;
android:orientation=&quot;vertical&quot;
android:scrollbars=&quot;none&quot;
tools:context=&quot;.Fragment.PrivacyFragment&quot;&gt;
&lt;LinearLayout
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;
android:orientation=&quot;vertical&quot;&gt;
&lt;TextView
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_marginStart=&quot;22dp&quot;
android:layout_marginTop=&quot;5dp&quot;
android:padding=&quot;8dp&quot;
android:textColor=&quot;@color/colorBlack&quot;
android:textSize=&quot;22sp&quot;
android:textStyle=&quot;bold&quot; /&gt;
&lt;TextView
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_marginStart=&quot;22dp&quot;
android:layout_marginTop=&quot;5dp&quot;
android:layout_marginEnd=&quot;20dp&quot;
android:padding=&quot;8dp&quot;
android:textColor=&quot;@color/colorBlack&quot;
android:textSize=&quot;18sp&quot;
android:textStyle=&quot;bold&quot; /&gt;
&lt;TextView
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_marginStart=&quot;22dp&quot;
android:layout_marginEnd=&quot;20dp&quot;
android:padding=&quot;8dp&quot;
android:textColor=&quot;@color/colorBlack&quot;
android:textSize=&quot;16sp&quot; /&gt;
&lt;TextView
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_marginStart=&quot;22dp&quot;
android:layout_marginEnd=&quot;20dp&quot;
android:padding=&quot;8dp&quot;
android:textColor=&quot;@color/colorBlack&quot;
android:textSize=&quot;18sp&quot;
android:textStyle=&quot;bold&quot; /&gt;
&lt;TextView
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_marginStart=&quot;22dp&quot;
android:layout_marginEnd=&quot;20dp&quot;
android:padding=&quot;8dp&quot;
android:textColor=&quot;@color/Grey&quot;
android:textSize=&quot;18sp&quot;
android:textStyle=&quot;bold&quot; /&gt;
&lt;TextView
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_marginStart=&quot;22dp&quot;
android:layout_marginEnd=&quot;20dp&quot;
android:padding=&quot;8dp&quot;
android:textColor=&quot;@color/colorBlack&quot;
android:textSize=&quot;16sp&quot; /&gt;
&lt;TextView
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_marginStart=&quot;22dp&quot;
android:layout_marginEnd=&quot;20dp&quot;
android:padding=&quot;8dp&quot;
android:textColor=&quot;@color/Grey&quot;
android:textSize=&quot;18sp&quot;
android:textStyle=&quot;bold&quot; /&gt;
&lt;TextView
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_marginStart=&quot;22dp&quot;
android:layout_marginEnd=&quot;20dp&quot;
android:padding=&quot;8dp&quot;
android:textColor=&quot;@color/colorBlack&quot;
android:textSize=&quot;16sp&quot; /&gt;
&lt;TextView
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_marginStart=&quot;22dp&quot;
android:layout_marginEnd=&quot;20dp&quot;
android:padding=&quot;8dp&quot;
android:textColor=&quot;@color/Grey&quot;
android:textSize=&quot;18sp&quot;
android:textStyle=&quot;bold&quot; /&gt;
&lt;TextView
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_marginStart=&quot;22dp&quot;
android:layout_marginEnd=&quot;20dp&quot;
android:padding=&quot;8dp&quot;
android:textColor=&quot;@color/colorBlack&quot;
android:textSize=&quot;16sp&quot; /&gt;
&lt;TextView
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_marginStart=&quot;22dp&quot;
android:layout_marginTop=&quot;5dp&quot;
android:layout_marginEnd=&quot;20dp&quot;
android:padding=&quot;8dp&quot;
android:textColor=&quot;@color/colorBlack&quot;
android:textSize=&quot;18sp&quot;
android:textStyle=&quot;bold&quot; /&gt;
&lt;TextView
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_marginStart=&quot;22dp&quot;
android:layout_marginEnd=&quot;20dp&quot;
android:padding=&quot;8dp&quot;
android:textColor=&quot;@color/colorBlack&quot;
android:textSize=&quot;16sp&quot; /&gt;
&lt;TextView
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_marginStart=&quot;22dp&quot;
android:layout_marginTop=&quot;5dp&quot;
android:layout_marginEnd=&quot;20dp&quot;
android:padding=&quot;8dp&quot;
android:textColor=&quot;@color/colorBlack&quot;
android:textSize=&quot;18sp&quot;
android:textStyle=&quot;bold&quot; /&gt;
&lt;TextView
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_marginStart=&quot;22dp&quot;
android:layout_marginEnd=&quot;20dp&quot;
android:padding=&quot;8dp&quot;
android:textColor=&quot;@color/colorBlack&quot;
android:textSize=&quot;16sp&quot; /&gt;
&lt;TextView
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_marginStart=&quot;22dp&quot;
android:layout_marginTop=&quot;5dp&quot;
android:layout_marginEnd=&quot;20dp&quot;
android:padding=&quot;8dp&quot;
android:textColor=&quot;@color/colorBlack&quot;
android:textSize=&quot;18sp&quot;
android:textStyle=&quot;bold&quot; /&gt;
&lt;TextView
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_marginStart=&quot;22dp&quot;
android:layout_marginEnd=&quot;20dp&quot;
android:padding=&quot;8dp&quot;
android:textColor=&quot;@color/colorBlack&quot;
android:textSize=&quot;16sp&quot; /&gt;
&lt;TextView
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_marginStart=&quot;22dp&quot;
android:layout_marginTop=&quot;5dp&quot;
android:layout_marginEnd=&quot;20dp&quot;
android:padding=&quot;8dp&quot;
android:textColor=&quot;@color/colorBlack&quot;
android:textSize=&quot;18sp&quot;
android:textStyle=&quot;bold&quot; /&gt;
&lt;TextView
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_marginStart=&quot;22dp&quot;
android:layout_marginEnd=&quot;20dp&quot;
android:padding=&quot;8dp&quot;
android:textColor=&quot;@color/colorBlack&quot;
android:textSize=&quot;16sp&quot; /&gt;
&lt;TextView
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_marginStart=&quot;22dp&quot;
android:layout_marginTop=&quot;5dp&quot;
android:layout_marginEnd=&quot;20dp&quot;
android:padding=&quot;8dp&quot;
android:textColor=&quot;@color/colorBlack&quot;
android:textSize=&quot;18sp&quot;
android:textStyle=&quot;bold&quot; /&gt;
&lt;TextView
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_marginStart=&quot;22dp&quot;
android:layout_marginEnd=&quot;20dp&quot;
android:padding=&quot;8dp&quot;
android:textColor=&quot;@color/colorBlack&quot;
android:textSize=&quot;16sp&quot; /&gt;
&lt;TextView
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_marginStart=&quot;22dp&quot;
android:layout_marginTop=&quot;5dp&quot;
android:layout_marginEnd=&quot;20dp&quot;
android:padding=&quot;8dp&quot;
android:textColor=&quot;@color/colorBlack&quot;
android:textSize=&quot;18sp&quot;
android:textStyle=&quot;bold&quot; /&gt;
&lt;TextView
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_marginStart=&quot;22dp&quot;
android:layout_marginEnd=&quot;20dp&quot;
android:padding=&quot;8dp&quot;
android:textColor=&quot;@color/colorBlack&quot;
android:textSize=&quot;16sp&quot; /&gt;
&lt;TextView
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_marginStart=&quot;22dp&quot;
android:layout_marginTop=&quot;5dp&quot;
android:layout_marginEnd=&quot;20dp&quot;
android:padding=&quot;8dp&quot;
android:textColor=&quot;@color/colorBlack&quot;
android:textSize=&quot;18sp&quot;
android:textStyle=&quot;bold&quot; /&gt;
&lt;TextView
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_marginStart=&quot;22dp&quot;
android:layout_marginEnd=&quot;20dp&quot;
android:padding=&quot;8dp&quot;
android:textColor=&quot;@color/colorBlack&quot;
android:textSize=&quot;16sp&quot; /&gt;
&lt;TextView
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_marginStart=&quot;22dp&quot;
android:layout_marginTop=&quot;5dp&quot;
android:layout_marginEnd=&quot;20dp&quot;
android:padding=&quot;8dp&quot;
android:textColor=&quot;@color/colorBlack&quot;
android:textSize=&quot;18sp&quot;
android:textStyle=&quot;bold&quot; /&gt;
&lt;TextView
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_marginStart=&quot;22dp&quot;
android:layout_marginEnd=&quot;20dp&quot;
android:padding=&quot;8dp&quot;
android:textColor=&quot;@color/colorBlack&quot;
android:textSize=&quot;16sp&quot; /&gt;
&lt;/LinearLayout&gt;
&lt;/androidx.core.widget.NestedScrollView&gt;

Activity

&lt;RelativeLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;match_parent&quot;
android:background=&quot;@drawable/button_gradient_two&quot;&gt;
&lt;RelativeLayout
android:id=&quot;@+id/relative_layout_one&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_marginStart=&quot;10dp&quot;
android:layout_marginTop=&quot;25dp&quot;
android:layout_marginEnd=&quot;10dp&quot;&gt;
&lt;/RelativeLayout&gt;
&lt;ScrollView
android:id=&quot;@+id/scroll_view&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;
android:scrollbars=&quot;none&quot;&gt;
&lt;RelativeLayout
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;&gt;
&lt;ImageView
android:id=&quot;@+id/arrow_back&quot;
android:layout_width=&quot;wrap_content&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_marginStart=&quot;10dp&quot;
android:layout_marginTop=&quot;25dp&quot;
android:padding=&quot;8dp&quot;
android:src=&quot;@drawable/icon_search_100&quot; /&gt;
&lt;TextView
android:layout_width=&quot;wrap_content&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_centerHorizontal=&quot;true&quot;
android:layout_marginTop=&quot;25dp&quot;
android:padding=&quot;8dp&quot;
android:text=&quot;Settings&quot;
android:textColor=&quot;@color/White&quot;
android:textSize=&quot;22sp&quot;
android:textStyle=&quot;bold&quot; /&gt;
&lt;de.hdodenhof.circleimageview.CircleImageView
android:id=&quot;@+id/image_profile&quot;
android:layout_width=&quot;100dp&quot;
android:layout_height=&quot;100dp&quot;
android:layout_centerHorizontal=&quot;true&quot;
android:layout_marginTop=&quot;100dp&quot;
android:elevation=&quot;16dp&quot;
android:src=&quot;@drawable/profile_placeholder&quot; /&gt;
&lt;com.google.android.material.card.MaterialCardView
style=&quot;@style/CustomCardViewStyle&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;1000dp&quot;
android:layout_marginTop=&quot;150dp&quot;
app:cardElevation=&quot;12dp&quot;&gt;
&lt;RelativeLayout
android:id=&quot;@+id/relative_layout_two&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;&gt;
&lt;TextView
android:id=&quot;@+id/name&quot;
android:layout_width=&quot;wrap_content&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_centerHorizontal=&quot;true&quot;
android:layout_marginTop=&quot;50dp&quot;
android:gravity=&quot;center_horizontal&quot;
android:text=&quot;Jack Sparrow&quot;
android:textColor=&quot;@color/Black&quot;
android:textSize=&quot;32sp&quot;
android:textStyle=&quot;bold&quot; /&gt;
&lt;TextView
android:id=&quot;@+id/username&quot;
android:layout_width=&quot;wrap_content&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_below=&quot;@+id/name&quot;
android:layout_centerHorizontal=&quot;true&quot;
android:gravity=&quot;center_horizontal&quot;
android:text=&quot;jacksparrow&quot;
android:textColor=&quot;@color/Grey&quot;
android:textSize=&quot;22sp&quot;
android:textStyle=&quot;&quot; /&gt;
&lt;TextView
android:id=&quot;@+id/bio&quot;
android:layout_width=&quot;wrap_content&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_below=&quot;@+id/username&quot;
android:layout_centerHorizontal=&quot;true&quot;
android:layout_marginTop=&quot;4dp&quot;
android:gravity=&quot;center_horizontal&quot;
android:text=&quot;This is my bio!&quot;
android:textColor=&quot;@color/Grey&quot;
android:textStyle=&quot;&quot; /&gt;
&lt;LinearLayout
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_below=&quot;@+id/bio&quot;
android:orientation=&quot;vertical&quot;&gt;
&lt;TextView
android:id=&quot;@+id/privacy&quot;
style=&quot;@style/SelectableItemBackground&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;
android:padding=&quot;12dp&quot;
android:text=&quot;@string/privacy&quot;
android:textColor=&quot;@color/colorBlack&quot;
android:textSize=&quot;18sp&quot; /&gt;
&lt;FrameLayout
android:id=&quot;@+id/container_privacy&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;
android:visibility=&quot;gone&quot; /&gt;
&lt;TextView
android:id=&quot;@+id/security&quot;
style=&quot;@style/SelectableItemBackground&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;
android:padding=&quot;12dp&quot;
android:text=&quot;@string/security&quot;
android:textColor=&quot;@color/colorBlack&quot;
android:textSize=&quot;18sp&quot; /&gt;
&lt;FrameLayout
android:id=&quot;@+id/container_security&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;
android:visibility=&quot;gone&quot; /&gt;
&lt;TextView
android:id=&quot;@+id/help&quot;
style=&quot;@style/SelectableItemBackground&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;
android:padding=&quot;12dp&quot;
android:text=&quot;@string/help&quot;
android:textColor=&quot;@color/colorBlack&quot;
android:textSize=&quot;18sp&quot; /&gt;
&lt;FrameLayout
android:id=&quot;@+id/container_help&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;
android:visibility=&quot;gone&quot; /&gt;
&lt;TextView
android:id=&quot;@+id/about&quot;
style=&quot;@style/SelectableItemBackground&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;
android:padding=&quot;12dp&quot;
android:text=&quot;@string/about&quot;
android:textColor=&quot;@color/colorBlack&quot;
android:textSize=&quot;18sp&quot; /&gt;
&lt;FrameLayout
android:id=&quot;@+id/container_about&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;
android:visibility=&quot;gone&quot; /&gt;
&lt;/LinearLayout&gt;
&lt;/RelativeLayout&gt;
&lt;/com.google.android.material.card.MaterialCardView&gt;
&lt;/RelativeLayout&gt;
&lt;/ScrollView&gt;
&lt;/RelativeLayout&gt;

SettingsActivity

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 -&gt; {
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 -&gt; {
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 -&gt; {
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 -&gt; {
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(&quot;Users&quot;).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();
}
});
}
}

答案1

得分: 1

您正在将两个片段叠加在一起。
在执行片段事务时,使用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);
}
英文:

You are adding two fragments on top of each other.
Instead of add use replace when executing the fragment transaction:

From:

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);
}

To:

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);
}

答案2

得分: 1

你之所以出现重叠的片段是因为每次都添加了一个新的片段。

您只需要检查片段是否已经存在,如果存在,则无需添加新的片段。您可以在添加片段时分配一个“标签”。代码如下-

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

或者

不要使用.add()方法,而是使用.replace()方法,以确保每次都替换片段。

根据您的Activity代码,您需要选择正确的方式,但请确保没有在不销毁之前重新创建片段。

愉快的编码!

英文:

You are getting the fragment overlapped because there is a new fragment added each time.

You simply need to do a check if the fragment exits already then there is no need to add a new fragment. You can assign a 'tag' when adding fragment. Code as follows-

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

OR

Rather then using .add() method use .replace() to make sure fragment got replaced each time.

According to your Activity code, you need to choose the correct way, but make sure there is no fragment getting re-created without destroying previous one.

Happy Coding !!

huangapple
  • 本文由 发表于 2020年7月29日 17:53:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/63150959.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定