我在将respondlerview添加到constraintlayout然后从片段TestDriveFragment执行android.app.FragmentTransaction.replace到片段Experiment2Fragment我的下一个片段丢失了它的约束时遇到了问题,但是当我不使用recylerview时,一切都很好。
layout fragment_test_drive.XML:
<?xml version="1.0" encoding="utf-8"?>
<layout
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.support.constraint.ConstraintLayout
android:id="@+id/drawer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<android.support.v7.widget.RecyclerView
android:id="@+id/rvAccount"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0" />
<android.support.design.widget.FloatingActionButton
android:id="@+id/fabAccount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:clickable="true"
app:backgroundTint="@color/colorAccent"
app:fabSize="mini"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:srcCompat="@drawable/ic_add" />
</android.support.constraint.ConstraintLayout>
</layout>
然后我将此代码放在fab onclick上以更改为片段b:
MaintActivity.Java
public void fragmentTrans(Fragment a, boolean b){
private Fragment fragment = a;
private android.app.FragmentManager fragmentManager = getFragmentManager();
private android.app.FragmentTransaction ft = fragmentManager.beginTransaction();
ft.replace(R.id.fragment_frame,fragment);
String backStateName = fragment.getClass().getName();
if (b == true) {
ft.addToBackStack(backStateName);
Log.d(Tag, String.valueOf(b));
}
ft.commit();
}
TestDriveFragment.java
binding.fabAccount.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
private MainActivity activity = (MainActivity) getActivity();
activity.fragmentTrans(new Experiment2Fragment(), true);
}
});
两个结果屏幕截图:
Result fragment_experiment2 when Recycerview Removed from fragment_test_drive:
Result fragment_experiment2 when Recycerview used at fragment_test_drive
现在我通过将Recyclerview的可见性设置为Inivisbile来获得临时解决方案。但我仍然很好奇为什么会发生这种情况