英文:
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:
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:
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">
<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>
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 -> {
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();
}
});
}
}
答案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("First Fragment") == null)
fragmentManager.beginTransaction().add(R.id.fragment, new FirstFragment(), "First Fragment").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 !!
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论