2017-05-07 155 views
0

我遇到了我的片段问题,但不知道为什么。我在主要活动中有两个片段。一边(右边)在片段上有一个微调器,在那个片段中,我替换了微调器显示的片段。左侧显示了使用阵列适配器显示数据的列表视图。替换片段删除另一片段

问题是,无论何时我替换左侧的片段,右侧的片段消失,我不知道为什么会这样做。

这是我的微调更换左侧片段代码:

personSelection.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { 

     @Override 
     public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { 

      if (position == 0) { 

       personType = "Student"; 

       StudentFragment studentFrag = StudentFragment.newInstance(); 
       getFragmentManager().beginTransaction() 
         .replace(R.id.form, studentFrag) 
         .commit(); 

      } else if (position == 1) { 

       personType = "Teacher"; 

       TeacherFragment teacherFrag = TeacherFragment.newInstance(); 
       getFragmentManager().beginTransaction() 
         .replace(R.id.form, teacherFrag) 
         .commit(); 

      } else if (position == 2) { 

       personType = "Administrator"; 

       AdminFragment adminFrag = AdminFragment.newInstance(); 
       getFragmentManager().beginTransaction() 
         .replace(R.id.form, adminFrag) 
         .commit(); 

      } // end if/else 

     } // end personSelection 

     @Override 
     public void onNothingSelected(AdapterView<?> parent) {} 

    }); // end selection 

右侧片段被用微调器旁边显示新添加的数据到数组列表按钮刷新,这就是只有该片段被使用的时间。

一个视觉展现我的意思。

  • 应用程序启动时工作正常:

enter image description here

  • 选择微调后:(刷新按钮并不要么在此之后的工作)

enter image description here

这是主要活动在xml:

<LinearLayout 
    android:layout_width="wrap_content" 
    android:layout_height="match_parent" 
    android:orientation="vertical"> 

    <LinearLayout 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal"> 

    <Spinner 
     android:layout_height="40dp" 
     android:id="@+id/person_spinner" 
     android:entries="@array/child" 
     android:layout_width="170dp" /> 

    <Button 
     android:text="Button" 
     android:id="@+id/refresh_button" 
     android:drawableStart="@android:drawable/stat_notify_sync" 
     android:drawableTint="#000000" 
     android:layout_width="45dp" 
     android:layout_height="50dp" /> 
    </LinearLayout> 

    <fragment 
    android:name="com....StudentFragment" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_weight=".4" 
    android:id="@+id/form" 
     tools:layout="@layout/student_fragment" /> 

</LinearLayout> 

<fragment 
    android:name="com....DetailFragment" 
    android:layout_width="wrap_content" 
    android:layout_height="match_parent" 
    android:layout_weight=".7" 
    android:id="@+id/frame_detail" 
    tools:layout="@layout/fragment_detail"/> 

回答

0

也许尝试动态更换两个(左和右片段)。不要在xml布局中定义任何片段。相反,使用2个FrameLayouts作为碎片的容器,并根据需要仅在代码中动态添加所需的碎片。

0

我修好了。我不应该试图替换一个片段,这会将其移除并与主要活动的整个布局混淆。所以我做的是,在主体中一起创建所有片段,并在线性布局内的相对布局中添加它们。在onCreate方法,我只是改变了他们的知名度,以显示我想要的和隐藏其他人取决于微调选择。现在整个应用程序没有任何问题。