为什么可见性改变 before/after 嵌套片段插入
Why does visibility change before/after nested fragment insertion
我正在尝试解决与动画嵌套片段相关的更大问题,因此我创建了一个单独的项目并尝试在模块中解决它。
第一部分是在 childFragment
中切换可见性 - 我一直在尝试不同类型的代码,但不确定为什么要添加 layoutReturn.setVisibility(View.INVISIBLE);
父片段已隐藏。然而,当我在插入子片段后添加这一行时:
@Override
public void onClick(View v) {
//layoutReturn.setVisibility(View.INVISIBLE);
FragmentManager childFrag = getFragmentManager();
FragmentTransaction childFragTrans = childFrag.beginTransaction();
addressCard_fragment addressCardChild = new addressCard_fragment();
childFragTrans.add(R.id.bottom_row_fragment_container, addressCardChild);
//childFragTrans.setCustomAnimations(R.animator.slide_in_card, R.animator.slide_out_card);
childFragTrans.commit();
Toast.makeText(getActivity().getBaseContext(), "yo", Toast.LENGTH_SHORT).show();
layoutReturn.setVisibility(View.INVISIBLE);
}
更改无效。父片段仍然可见,Toast 出现但 childFragment
仍然看不见。任何帮助将不胜感激!
主要Activity:
public class MainActivity extends AppCompatActivity {
private topRow_fragment fragmentTopRow;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
fragmentTopRow = new topRow_fragment();
}
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.add(R.id.top_row_fragment_container, fragmentTopRow);
fragmentTransaction.commit();
}}
parentFragment class:
public class topRow_fragment extends Fragment implements View.OnClickListener{
ImageButton btn;
View layoutReturn;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
layoutReturn = inflater.inflate(R.layout.firstrow_fragment, container, false);
btn = (ImageButton) layoutReturn.findViewById(R.id.imageLeft);
btn.setOnClickListener(this);
return layoutReturn;
}
@Override
public void onClick(View v) {
//layoutReturn.setVisibility(View.INVISIBLE);
FragmentManager childFrag = getFragmentManager();
FragmentTransaction childFragTrans = childFrag.beginTransaction();
addressCard_fragment addressCardChild = new addressCard_fragment();
childFragTrans.add(R.id.bottom_row_fragment_container, addressCardChild);
//childFragTrans.setCustomAnimations(R.animator.slide_in_card, R.animator.slide_out_card);
childFragTrans.commit();
Toast.makeText(getActivity().getBaseContext(), "yo", Toast.LENGTH_SHORT).show();
layoutReturn.setVisibility(View.INVISIBLE);
}}
childFragment class 就是典型的 onCreateView 方法。
Update01:当我使用 getChildFragmentManager 时,我收到以 09-14 22:54:34.933 20795-20795/com.example.bryanjordan.nestedfragmentstest E/FragmentManager: No view found for id 0x7f0c0060 (com.example.bryanjordan.nestedfragmentstest:id/bottom_row_fragment_container) for fragment addressCard_fragment{7d09836 #0 id=0x7f0c0060}
开头的大量错误
总结为:java.lang.IllegalArgumentException: No view found for id 0x7f0c0060 (com.example.bryanjordan.nestedfragmentstest:id/bottom_row_fragment_container) for fragment addressCard_fragment{7d09836 #0 id=0x7f0c0060}
XML 个文件:
主要Activity:
<?xml version="1.0" encoding="utf-8"?><RelativeLayout 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="wrap_content"
tools:context="com.example.bryanjordan.nestedfragmentstest.MainActivity">
<android.support.constraint.ConstraintLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<android.support.constraint.ConstraintLayout
android:layout_width="368dp"
android:layout_height="wrap_content"
tools:layout_editor_absoluteY="8dp"
android:id="@+id/constraintLayout"
android:layout_marginLeft="8dp"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginRight="8dp"
app:layout_constraintRight_toRightOf="parent">
<RelativeLayout
android:id="@+id/top_row_fragment_container"
android:layout_width="367dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
tools:layout_editor_absoluteY="8dp"
tools:layout_editor_absoluteX="1dp">
</RelativeLayout>
<RelativeLayout
android:id="@+id/bottom_row_fragment_container"
android:layout_width="367dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
tools:layout_editor_absoluteY="8dp"
tools:layout_editor_absoluteX="1dp">
</RelativeLayout>
</android.support.constraint.ConstraintLayout>
<android.support.constraint.ConstraintLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
app:layout_constraintTop_toBottomOf="@+id/constraintLayout"
android:layout_marginLeft="8dp"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginRight="8dp"
app:layout_constraintRight_toRightOf="parent">
<RelativeLayout
android:id="@+id/second_Row_container"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginLeft="6dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
tools:layout_editor_absoluteY="25dp">
</RelativeLayout>
</android.support.constraint.ConstraintLayout>
</android.support.constraint.ConstraintLayout>
firstrow_fragment
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/parent_row_container"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:context="com.example.bryanjordan.nestedfragmentstest.MainActivity">
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.CardView
android:id="@+id/cardTop"
android:layout_width="340dp"
android:layout_height="80dp"
android:layout_margin="5dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="3dp"
android:elevation="5dp"
app:cardCornerRadius="5dp"
app:contentPadding="5dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_goneMarginLeft="5dp"
app:layout_goneMarginRight="5dp"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp">
<android.support.constraint.ConstraintLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingLeft="5dp"
android:paddingRight="5dp">
<ImageButton
android:id="@+id/imageLeft"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_marginBottom="8dp"
android:layout_marginLeft="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:adjustViewBounds="true"
android:background="@drawable/square"
android:clickable="true"
android:scaleType="centerCrop"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="@+id/imageCentre"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_marginBottom="8dp"
android:layout_marginLeft="13dp"
android:layout_marginStart="13dp"
android:layout_marginTop="8dp"
android:background="@drawable/square"
android:clickable="true"
android:scaleType="centerCrop"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toRightOf="@+id/imageLeft"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="@+id/imageRight"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_marginBottom="8dp"
android:layout_marginLeft="13dp"
android:layout_marginStart="13dp"
android:layout_marginTop="8dp"
android:background="@drawable/square"
android:clickable="true"
android:scaleType="centerCrop"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toRightOf="@+id/imageCentre"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
</android.support.v7.widget.CardView>
</android.support.constraint.ConstraintLayout>
地址卡
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/address_card_container"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:context="com.example.bryanjordan.nestedfragmentstest.MainActivity">
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.CardView
android:id="@+id/years_of_cooking_xp_cardView"
android:layout_width="337dp"
android:layout_height="240dp"
android:layout_margin="5dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:elevation="50dp"
app:cardCornerRadius="5dp"
app:contentPadding="5dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_goneMarginLeft="5dp"
app:layout_goneMarginRight="5dp"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
tools:layout_editor_absoluteY="20dp">
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:paddingLeft="5dp"
android:paddingRight="5dp">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="8dp"
android:text="Test"
android:textAlignment="center"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageButton
android:id="@+id/experience_left_button"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_marginRight="40dp"
android:layout_marginTop="8dp"
android:adjustViewBounds="true"
android:background="@drawable/square"
android:clickable="true"
android:scaleType="centerCrop"
app:layout_constraintRight_toLeftOf="@+id/experience_centre_button"
app:layout_constraintTop_toBottomOf="@+id/textView" />
<ImageButton
android:id="@+id/experience_centre_button"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="8dp"
android:adjustViewBounds="true"
android:background="@drawable/square"
android:clickable="true"
android:scaleType="centerCrop"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView"
app:layout_constraintHorizontal_bias="0.498" />
<ImageButton
android:id="@+id/experience_right_button"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_marginLeft="40dp"
android:layout_marginTop="8dp"
android:adjustViewBounds="true"
android:background="@drawable/square"
android:clickable="true"
android:scaleType="centerCrop"
app:layout_constraintLeft_toRightOf="@+id/experience_centre_button"
app:layout_constraintTop_toBottomOf="@+id/textView" />
<TextView
android:id="@+id/zero_to_one_Year"
android:layout_width="40dp"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="8dp"
android:text="0 - 1 Year"
android:textAlignment="center"
app:layout_constraintBottom_toBottomOf="@+id/experience_left_button"
app:layout_constraintLeft_toLeftOf="@+id/experience_left_button"
app:layout_constraintRight_toRightOf="@+id/experience_left_button"
app:layout_constraintTop_toTopOf="@+id/experience_left_button" />
<TextView
android:id="@+id/one_to_three_years"
android:layout_width="40dp"
android:layout_height="wrap_content"
android:text="1 - 3 Years"
android:textAlignment="center"
android:layout_marginBottom="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toBottomOf="@+id/experience_centre_button"
app:layout_constraintLeft_toLeftOf="@+id/experience_centre_button"
app:layout_constraintRight_toRightOf="@+id/experience_centre_button"
app:layout_constraintTop_toTopOf="@+id/experience_centre_button"
/>
<TextView
android:id="@+id/three_plus_years"
android:layout_width="40dp"
android:layout_height="wrap_content"
android:text="3+ Years"
android:textAlignment="center"
android:layout_marginBottom="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toBottomOf="@+id/experience_right_button"
app:layout_constraintLeft_toLeftOf="@+id/experience_right_button"
app:layout_constraintRight_toRightOf="@+id/experience_right_button"
app:layout_constraintTop_toTopOf="@+id/experience_right_button"
/>
</android.support.constraint.ConstraintLayout>
</android.support.v7.widget.CardView>
</android.support.constraint.ConstraintLayout>
您的 top_row_fragment_container
和 bottom_row_fragment_container
容器都在 MainActivity 中。那么它可能不是嵌套片段。
嵌套片段意味着 片段 位于 片段.
内
您的容器高度为 wrap_content。
android:layout_height="wrap_content"
设置一些高度以使您的两个片段都可见。
从您的代码中删除 layoutReturn.setVisibility(View.INVISIBLE);
。
解决方案
首先感谢 Bhuvanesh Bs 和 JRamesh 的帮助。
对于那些面临类似问题的人,我建议您尽快考虑以下事项:
- 确保您在另一个片段中初始化您的子片段。虽然 Bhuvanesh 建议不要将 FrameLayout 移动到父片段中,但我发现这很有效。不幸的是,子片段现在正在父片段的 CardView 中初始化。
- 通过引用子片段的容器来解决切换可见性问题,该子片段是
FrameLayout
添加到第 1 点中所述的父片段。
- 关于切换
layoutReturn
的可见性,我认为这是因为我在返回 layoutReturn
时没有更新任何更改。这解释了为什么没有任何添加,activity 会变成空白。但是,一旦添加新片段,布局就会发生变化,因此更改布局的可见性现在已经过时了。应该注意的是,我对这个答案没有信心,所以请半信半疑。
干杯
我正在尝试解决与动画嵌套片段相关的更大问题,因此我创建了一个单独的项目并尝试在模块中解决它。
第一部分是在 childFragment
中切换可见性 - 我一直在尝试不同类型的代码,但不确定为什么要添加 layoutReturn.setVisibility(View.INVISIBLE);
父片段已隐藏。然而,当我在插入子片段后添加这一行时:
@Override
public void onClick(View v) {
//layoutReturn.setVisibility(View.INVISIBLE);
FragmentManager childFrag = getFragmentManager();
FragmentTransaction childFragTrans = childFrag.beginTransaction();
addressCard_fragment addressCardChild = new addressCard_fragment();
childFragTrans.add(R.id.bottom_row_fragment_container, addressCardChild);
//childFragTrans.setCustomAnimations(R.animator.slide_in_card, R.animator.slide_out_card);
childFragTrans.commit();
Toast.makeText(getActivity().getBaseContext(), "yo", Toast.LENGTH_SHORT).show();
layoutReturn.setVisibility(View.INVISIBLE);
}
更改无效。父片段仍然可见,Toast 出现但 childFragment
仍然看不见。任何帮助将不胜感激!
主要Activity:
public class MainActivity extends AppCompatActivity {
private topRow_fragment fragmentTopRow;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
fragmentTopRow = new topRow_fragment();
}
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.add(R.id.top_row_fragment_container, fragmentTopRow);
fragmentTransaction.commit();
}}
parentFragment class:
public class topRow_fragment extends Fragment implements View.OnClickListener{
ImageButton btn;
View layoutReturn;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
layoutReturn = inflater.inflate(R.layout.firstrow_fragment, container, false);
btn = (ImageButton) layoutReturn.findViewById(R.id.imageLeft);
btn.setOnClickListener(this);
return layoutReturn;
}
@Override
public void onClick(View v) {
//layoutReturn.setVisibility(View.INVISIBLE);
FragmentManager childFrag = getFragmentManager();
FragmentTransaction childFragTrans = childFrag.beginTransaction();
addressCard_fragment addressCardChild = new addressCard_fragment();
childFragTrans.add(R.id.bottom_row_fragment_container, addressCardChild);
//childFragTrans.setCustomAnimations(R.animator.slide_in_card, R.animator.slide_out_card);
childFragTrans.commit();
Toast.makeText(getActivity().getBaseContext(), "yo", Toast.LENGTH_SHORT).show();
layoutReturn.setVisibility(View.INVISIBLE);
}}
childFragment class 就是典型的 onCreateView 方法。
Update01:当我使用 getChildFragmentManager 时,我收到以 09-14 22:54:34.933 20795-20795/com.example.bryanjordan.nestedfragmentstest E/FragmentManager: No view found for id 0x7f0c0060 (com.example.bryanjordan.nestedfragmentstest:id/bottom_row_fragment_container) for fragment addressCard_fragment{7d09836 #0 id=0x7f0c0060}
总结为:java.lang.IllegalArgumentException: No view found for id 0x7f0c0060 (com.example.bryanjordan.nestedfragmentstest:id/bottom_row_fragment_container) for fragment addressCard_fragment{7d09836 #0 id=0x7f0c0060}
XML 个文件:
主要Activity:
<?xml version="1.0" encoding="utf-8"?><RelativeLayout 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="wrap_content"
tools:context="com.example.bryanjordan.nestedfragmentstest.MainActivity">
<android.support.constraint.ConstraintLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<android.support.constraint.ConstraintLayout
android:layout_width="368dp"
android:layout_height="wrap_content"
tools:layout_editor_absoluteY="8dp"
android:id="@+id/constraintLayout"
android:layout_marginLeft="8dp"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginRight="8dp"
app:layout_constraintRight_toRightOf="parent">
<RelativeLayout
android:id="@+id/top_row_fragment_container"
android:layout_width="367dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
tools:layout_editor_absoluteY="8dp"
tools:layout_editor_absoluteX="1dp">
</RelativeLayout>
<RelativeLayout
android:id="@+id/bottom_row_fragment_container"
android:layout_width="367dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
tools:layout_editor_absoluteY="8dp"
tools:layout_editor_absoluteX="1dp">
</RelativeLayout>
</android.support.constraint.ConstraintLayout>
<android.support.constraint.ConstraintLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
app:layout_constraintTop_toBottomOf="@+id/constraintLayout"
android:layout_marginLeft="8dp"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginRight="8dp"
app:layout_constraintRight_toRightOf="parent">
<RelativeLayout
android:id="@+id/second_Row_container"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginLeft="6dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
tools:layout_editor_absoluteY="25dp">
</RelativeLayout>
</android.support.constraint.ConstraintLayout>
</android.support.constraint.ConstraintLayout>
firstrow_fragment
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/parent_row_container"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:context="com.example.bryanjordan.nestedfragmentstest.MainActivity">
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.CardView
android:id="@+id/cardTop"
android:layout_width="340dp"
android:layout_height="80dp"
android:layout_margin="5dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="3dp"
android:elevation="5dp"
app:cardCornerRadius="5dp"
app:contentPadding="5dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_goneMarginLeft="5dp"
app:layout_goneMarginRight="5dp"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp">
<android.support.constraint.ConstraintLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingLeft="5dp"
android:paddingRight="5dp">
<ImageButton
android:id="@+id/imageLeft"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_marginBottom="8dp"
android:layout_marginLeft="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:adjustViewBounds="true"
android:background="@drawable/square"
android:clickable="true"
android:scaleType="centerCrop"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="@+id/imageCentre"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_marginBottom="8dp"
android:layout_marginLeft="13dp"
android:layout_marginStart="13dp"
android:layout_marginTop="8dp"
android:background="@drawable/square"
android:clickable="true"
android:scaleType="centerCrop"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toRightOf="@+id/imageLeft"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="@+id/imageRight"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_marginBottom="8dp"
android:layout_marginLeft="13dp"
android:layout_marginStart="13dp"
android:layout_marginTop="8dp"
android:background="@drawable/square"
android:clickable="true"
android:scaleType="centerCrop"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toRightOf="@+id/imageCentre"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
</android.support.v7.widget.CardView>
</android.support.constraint.ConstraintLayout>
地址卡
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/address_card_container"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:context="com.example.bryanjordan.nestedfragmentstest.MainActivity">
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.CardView
android:id="@+id/years_of_cooking_xp_cardView"
android:layout_width="337dp"
android:layout_height="240dp"
android:layout_margin="5dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:elevation="50dp"
app:cardCornerRadius="5dp"
app:contentPadding="5dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_goneMarginLeft="5dp"
app:layout_goneMarginRight="5dp"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
tools:layout_editor_absoluteY="20dp">
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:paddingLeft="5dp"
android:paddingRight="5dp">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="8dp"
android:text="Test"
android:textAlignment="center"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageButton
android:id="@+id/experience_left_button"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_marginRight="40dp"
android:layout_marginTop="8dp"
android:adjustViewBounds="true"
android:background="@drawable/square"
android:clickable="true"
android:scaleType="centerCrop"
app:layout_constraintRight_toLeftOf="@+id/experience_centre_button"
app:layout_constraintTop_toBottomOf="@+id/textView" />
<ImageButton
android:id="@+id/experience_centre_button"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="8dp"
android:adjustViewBounds="true"
android:background="@drawable/square"
android:clickable="true"
android:scaleType="centerCrop"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView"
app:layout_constraintHorizontal_bias="0.498" />
<ImageButton
android:id="@+id/experience_right_button"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_marginLeft="40dp"
android:layout_marginTop="8dp"
android:adjustViewBounds="true"
android:background="@drawable/square"
android:clickable="true"
android:scaleType="centerCrop"
app:layout_constraintLeft_toRightOf="@+id/experience_centre_button"
app:layout_constraintTop_toBottomOf="@+id/textView" />
<TextView
android:id="@+id/zero_to_one_Year"
android:layout_width="40dp"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="8dp"
android:text="0 - 1 Year"
android:textAlignment="center"
app:layout_constraintBottom_toBottomOf="@+id/experience_left_button"
app:layout_constraintLeft_toLeftOf="@+id/experience_left_button"
app:layout_constraintRight_toRightOf="@+id/experience_left_button"
app:layout_constraintTop_toTopOf="@+id/experience_left_button" />
<TextView
android:id="@+id/one_to_three_years"
android:layout_width="40dp"
android:layout_height="wrap_content"
android:text="1 - 3 Years"
android:textAlignment="center"
android:layout_marginBottom="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toBottomOf="@+id/experience_centre_button"
app:layout_constraintLeft_toLeftOf="@+id/experience_centre_button"
app:layout_constraintRight_toRightOf="@+id/experience_centre_button"
app:layout_constraintTop_toTopOf="@+id/experience_centre_button"
/>
<TextView
android:id="@+id/three_plus_years"
android:layout_width="40dp"
android:layout_height="wrap_content"
android:text="3+ Years"
android:textAlignment="center"
android:layout_marginBottom="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toBottomOf="@+id/experience_right_button"
app:layout_constraintLeft_toLeftOf="@+id/experience_right_button"
app:layout_constraintRight_toRightOf="@+id/experience_right_button"
app:layout_constraintTop_toTopOf="@+id/experience_right_button"
/>
</android.support.constraint.ConstraintLayout>
</android.support.v7.widget.CardView>
</android.support.constraint.ConstraintLayout>
您的 top_row_fragment_container
和 bottom_row_fragment_container
容器都在 MainActivity 中。那么它可能不是嵌套片段。
嵌套片段意味着 片段 位于 片段.
内您的容器高度为 wrap_content。
android:layout_height="wrap_content"
设置一些高度以使您的两个片段都可见。
从您的代码中删除 layoutReturn.setVisibility(View.INVISIBLE);
。
解决方案
首先感谢 Bhuvanesh Bs 和 JRamesh 的帮助。
对于那些面临类似问题的人,我建议您尽快考虑以下事项:
- 确保您在另一个片段中初始化您的子片段。虽然 Bhuvanesh 建议不要将 FrameLayout 移动到父片段中,但我发现这很有效。不幸的是,子片段现在正在父片段的 CardView 中初始化。
- 通过引用子片段的容器来解决切换可见性问题,该子片段是
FrameLayout
添加到第 1 点中所述的父片段。 - 关于切换
layoutReturn
的可见性,我认为这是因为我在返回layoutReturn
时没有更新任何更改。这解释了为什么没有任何添加,activity 会变成空白。但是,一旦添加新片段,布局就会发生变化,因此更改布局的可见性现在已经过时了。应该注意的是,我对这个答案没有信心,所以请半信半疑。
干杯