Android 从一个片段移动到另一个片段时,片段UI不可见

Android 从一个片段移动到另一个片段时,片段UI不可见,android,xml,android-layout,android-fragments,Android,Xml,Android Layout,Android Fragments,我正在尝试从现有片段移动到另一个片段。事务已成功完成,但没有可见的ui元素 <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" xmlns:tools="http://schemas.android.com/tools" android:orientation="vertical" android:id="@+id/mainLinearLayout" xmlns:and

我正在尝试从现有片段移动到另一个片段。事务已成功完成,但没有可见的ui元素

<LinearLayout android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:id="@+id/mainLinearLayout"
xmlns:android="http://schemas.android.com/apk/res/android"
tools:context=".Main.Main"></LinearLayout>
第二段-

public class Electronicss extends Fragment{

private RecyclerView twentyFourHoursDealsRecyclerView,justLaunchedRecyclerView,bestOfElectronicsRecyclerView;
private String featuredCategoryKey="featured";

public Electronicss() {
    // Required empty public constructor
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.fragment_electronicss, container, false);

    // Inflate the layout for this fragment
    return view;
}}
第二个片段ui仅包含一个按钮,但仍然不可见:-

<android.support.constraint.ConstraintLayout 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="match_parent"
android:scrollbars="vertical"
tools:context=".Categories.Electronics.Electronicss">

<Button
    android:id="@+id/button11"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Button"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.5"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

除了添加片段使用替换,还可以将LinearLayout更改为FrameLayout,该布局充当要替换的片段的容器

注意:如果不希望片段存储在堆栈中,只需删除addToBackStack行

FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        transaction.replace(R.id.fragment_container, yourfragment)
                .addToBackStack(null)
                .commit();

您可以尝试替换片段,而不是添加片段

getActivity().getSupportFragmentManager().beginTransaction().replace(R.id.mainLinearLayout,fragmentElectronics).addToBackStack(null).commit();

正如@jitenbasnet所说,你还应该考虑使用FrAMLAYOUT而不是线性布局作为你的片段的容器。

< P>我终于得到了答案。我需要使用框架布局作为片段的容器,并且需要在第二个片段

中设置背景和可聚焦的n个可点击的真值,而不是添加片段,你可以尝试替换它。getActivity.getSupportFragmentManager.beginTransaction.replaceR.id.mainLinearLayout,fragmentElectronics.addToBackStacknull.commit;我明白了。但是这样做之后没有事务发生。请附上主机活动的全部源代码。是的,我用“替换”替换了“添加”,但这样做没有任何效果,而是现在没有事务发生,已经完成了,但我的新片段与前一个重叠。
getActivity().getSupportFragmentManager().beginTransaction().replace(R.id.mainLinearLayout,fragmentElectronics).addToBackStack(null).commit();