2017-03-16 61 views
3

我在另一個片段(片段A - >片段B)上添加一個片段,但是當我這樣做時,第一個片段的按鈕顯示出來。我不明白爲什麼在另一個片段上添加一個片段會顯示第一個片段的內容。當添加新片段時顯示的按鈕

這是第一個活動。它保持第一片段(片段A):

public class MainActivity extends AppCompatActivity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
     setSupportActionBar(toolbar); 

     FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); 
     fab.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG) 
         .setAction("Action", null).show(); 
      } 
     }); 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.menu_main, menu); 
     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     // Handle action bar item clicks here. The action bar will 
     // automatically handle clicks on the Home/Up button, so long 
     // as you specify a parent activity in AndroidManifest.xml. 
     int id = item.getItemId(); 

     //noinspection SimplifiableIfStatement 
     if (id == R.id.action_settings) { 
      return true; 
     } 

     return super.onOptionsItemSelected(item); 
    } 
} 

這裏是片段A的碼,即調用第二個片段(片段B):

public class MainActivityFragment extends Fragment { 

     public MainActivityFragment() { 
     } 

     @Override 
     public View onCreateView(LayoutInflater inflater, ViewGroup container, 
           Bundle savedInstanceState) { 
      return inflater.inflate(R.layout.fragment_main, container, false); 
     } 

     @Override 
     public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { 
      super.onViewCreated(view, savedInstanceState); 

      Button startButton = (Button) view.findViewById(R.id.start_button); 
      startButton.setOnClickListener(new View.OnClickListener() { 
       @Override 
       public void onClick(View v) { 
        GameSetupFragment newGame = new GameSetupFragment(); 
        getFragmentManager().beginTransaction() 
          .addToBackStack("MainActivity") 
          .replace(R.id.fragment, newGame) 
          .commit(); 
       } 
      }); 
     } 

    } 

這裏是片段B類:

public class GameSetupFragment extends Fragment { 

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

     return inflater.inflate(R.layout.game_board_fragment, container, false); 
    } 
} 

下面是每個XML文件:

activity_main.xml中(主要活動):

<android.support.design.widget.CoordinatorLayout 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" 
     tools:context="com.blaine.tictactoe.MainActivity"> 

     <android.support.design.widget.AppBarLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:theme="@style/AppTheme.AppBarOverlay"> 

      <android.support.v7.widget.Toolbar 
       android:id="@+id/toolbar" 
       android:layout_width="match_parent" 
       android:layout_height="?attr/actionBarSize" 
       android:background="?attr/colorPrimary" 
       app:popupTheme="@style/AppTheme.PopupOverlay" /> 

     </android.support.design.widget.AppBarLayout> 

     <include layout="@layout/content_main" /> 

     <android.support.design.widget.FloatingActionButton 
      android:id="@+id/fab" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="bottom|end" 
      android:layout_margin="@dimen/fab_margin" 
      app:srcCompat="@android:drawable/ic_dialog_email" /> 

    </android.support.design.widget.CoordinatorLayout> 

下面是片段B(fragment_main.xml)XML

 <TextView 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:text="@string/welcome" 
      android:textAppearance="@android:style/TextAppearance.Large" 
      android:id="@+id/textView" 
      tools:layout_constraintTop_creator="1" 
      tools:layout_constraintRight_creator="1" 
      android:layout_marginStart="69dp" 
      android:layout_marginEnd="69dp" 
      app:layout_constraintRight_toRightOf="parent" 
      android:layout_marginTop="74dp" 
      tools:layout_constraintLeft_creator="1" 
      app:layout_constraintLeft_toLeftOf="parent" 
      app:layout_constraintTop_toTopOf="parent" /> 

     <Button 
      android:id="@+id/start_button" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:text="@string/start" 
      tools:layout_constraintRight_creator="1" 
      tools:layout_constraintBottom_creator="1" 
      android:layout_marginStart="8dp" 
      app:layout_constraintBottom_toBottomOf="parent" 
      android:layout_marginEnd="8dp" 
      app:layout_constraintRight_toRightOf="parent" 
      tools:layout_constraintLeft_creator="1" 
      android:layout_marginBottom="117dp" 
      app:layout_constraintLeft_toLeftOf="parent" /> 

     <android.support.constraint.Guideline 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:id="@+id/guideline" 
      android:orientation="vertical" 
      app:layout_constraintGuide_begin="367dp" 
      tools:layout_editor_absoluteY="0dp" 
      tools:layout_editor_absoluteX="367dp" /> 

     <android.support.constraint.Guideline 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:id="@+id/guideline2" 
      app:layout_constraintGuide_begin="20dp" 
      android:orientation="vertical" 
      tools:layout_editor_absoluteY="0dp" 
      tools:layout_editor_absoluteX="20dp" /> 

     <android.support.constraint.Guideline 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:id="@+id/guideline3" 
      app:layout_constraintGuide_begin="74dp" 
      android:orientation="horizontal" 
      tools:layout_editor_absoluteY="74dp" 
      tools:layout_editor_absoluteX="0dp" /> 



</android.support.constraint.ConstraintLayout> 

Content_main.xml:

<fragment 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:id="@+id/fragment" 
    android:name="com.blaine.tictactoe.fragments.MainActivityFragment" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    app:layout_behavior="@string/appbar_scrolling_view_behavior" 
    tools:layout="@layout/fragment_main" /> 

片段B代碼(game_board_fragment.xml):

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:background="@color/colorAccent"> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="This is just a test"/> 
</RelativeLayout> 

這裏是應用程序的外觀在運行時,如:

First screen

Second screen

+0

與移除工具試試:佈局= 「@佈局/ fragment_main」 從Content_main佈局。你必須從主要活動onCreate方法添加這個片段使用frgament tansaction – Nas

回答

1

在你Content_main.xml採取的FrameLayout,並用它來試圖打開您的片段。例如

<FrameLayout 
     android:id="@+id/main_fragment" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" /> 

,並在您MainActivity的onCreate,使用下面的代碼來打開你的第一個片段是這樣的:

String fragmentName = targetFragment.getClass().getName(); 
FragmentManager manager = getSupportFragmentManager(); 
Fragment targetFragment = new MainActivityFragment(); 
manager.popBackStack(); 
manager.beginTransaction() 
       .replace(R.id.main_fragment, targetFragment, fragmentName) 
       .addToBackStack(fragmentName) 
       .commit(); 
+0

我只是想出了這一點。但這是正確的答案。感謝你的回答。 – BlackHatSamurai

相關問題