0

我與片段事務,使用底Bar.In我的應用程序的默認片段顯示兩次,當選擇第二個片段其沒有隱瞞工作..問題與片段交易(默認片段顯示兩次)

public class MainActivity extends AppCompatActivity { 
private Fragment fragment; 
private FragmentManager fragmentManager; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    fragmentManager = getSupportFragmentManager(); 
    fragment = new FragmentOne(); 
    final FragmentTransaction transaction = fragmentManager.beginTransaction(); 
    transaction.add(R.id.output, fragment).commit(); 


    BottomBar bottomBar = (BottomBar) findViewById(R.id.bottomBar); 
    bottomBar.setOnTabSelectListener(new OnTabSelectListener() { 
     @Override 
     public void onTabSelected(@IdRes int tabId) { 
      switch (tabId){ 
       case R.id.tab_favorites: 
        Toast.makeText(MainActivity.this, "FAV", Toast.LENGTH_SHORT).show(); 
        fragment = new FragmentOne(); 
        break; 
       case R.id.tab_friends: 
        Toast.makeText(MainActivity.this, "FRIEND", Toast.LENGTH_SHORT).show(); 
        fragment = new FragmentTwo(); 
        break; 
       case R.id.tab_nearby: 
        Toast.makeText(MainActivity.this, "NEAR", Toast.LENGTH_SHORT).show(); 
        fragment = new FragmentOne(); 
        break; 
       case R.id.tab_test: 
        Toast.makeText(MainActivity.this, "TEST", Toast.LENGTH_SHORT).show(); 
        fragment = new FragmentTwo(); 
        break; 
      } 
      final FragmentTransaction transaction = fragmentManager.beginTransaction(); 
      transaction.replace(R.id.output, fragment).commit(); 
     } 
    }); 

    bottomBar.setOnTabReselectListener(new OnTabReselectListener() { 
     @Override 
     public void onTabReSelected(@IdRes int tabId) { 
     } 
    }); 
} 

enter image description here

請幫我解決這個問題 這裏是我的XML佈局

<?xml version="1.0" encoding="utf-8"?> 
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     xmlns:tools="http://schemas.android.com/tools" 
     xmlns:app="http://schemas.android.com/apk/res-auto" 
     android:id="@+id/activity_main" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     tools:context="com.syntax.bottomtabs.MainActivity"> 
<fragment 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_marginTop="10dp" 
     android:layout_marginBottom="10dp" 
     android:layout_marginLeft="10dp" 
     android:layout_marginRight="10dp" 
     android:name="com.syntax.bottomtabs.FragmentOne" 
     android:id="@+id/output"/> 

    <com.roughike.bottombar.BottomBar 
     android:id="@+id/bottomBar" 
     android:layout_width="match_parent" 
     android:layout_height="70dp" 
     android:layout_alignParentBottom="true" 
     app:bb_tabXmlResource="@xml/bottombar_tabs_three" /> 
</RelativeLayout> 
+0

我們提供您的XML代碼,以及 –

回答

0

只需更換XML升IKE下面通過更換室內用片段相對佈局.....

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:id="@+id/activity_main" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context="com.syntax.bottomtabs.MainActivity"> 
<RelativeLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_marginTop="10dp" 
    android:layout_marginBottom="10dp" 
    android:layout_marginLeft="10dp" 
    android:layout_marginRight="10dp" 
    android:id="@+id/output"> 
</RelativeLayout> 

<com.roughike.bottombar.BottomBar 
    android:id="@+id/bottomBar" 
    android:layout_width="match_parent" 
    android:layout_height="70dp" 
    android:layout_alignParentBottom="true" 
    app:bb_tabXmlResource="@xml/bottombar_tabs_three" /> 
</RelativeLayout> 

希望這將有助於.....

+0

還有它的問題.. – Suresh

+0

謝謝它的工作很好.. – Suresh

+0

如果th e更新的答案正在工作,那麼你可以將答案標記爲已接受..... – Ashiq

0

不要添加片段。添加一個容器,然後用一個片段替換它。在這裏,framelayout將作爲容器工作。

XML代碼應該是:

<?xml version="1.0" encoding="utf-8"?> 
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     xmlns:tools="http://schemas.android.com/tools" 
     xmlns:app="http://schemas.android.com/apk/res-auto" 
     android:id="@+id/activity_main" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     tools:context="com.syntax.bottomtabs.MainActivity"> 

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

    <com.roughike.bottombar.BottomBar 
     android:id="@+id/bottomBar" 
     android:layout_width="match_parent" 
     android:layout_height="70dp" 
     android:layout_alignParentBottom="true" 
     app:bb_tabXmlResource="@xml/bottombar_tabs_three" /> 
</RelativeLayout> 

在活動的onCreate():

FragmentManager fm = getFragmentManager(); 
     fm.beginTransaction().replace(R.id.content_frame, new FragmentOne()).commit(); 

而其餘代碼:

bottomBar.setOnTabSelectListener(new OnTabSelectListener() { 
     final FragmentManager fm = getFragmentManager(); 
     @Override 
     public void onTabSelected(@IdRes int tabId) { 
      switch (tabId){ 
       case R.id.tab_favorites: 
        Toast.makeText(MainActivity.this, "FAV", Toast.LENGTH_SHORT).show(); 
        fm.beginTransaction().replace(R.id.content_frame, new FragmentOne()).commit(); 
        break; 
       case R.id.tab_friends: 
        Toast.makeText(MainActivity.this, "FRIEND", Toast.LENGTH_SHORT).show(); 
        fm.beginTransaction().replace(R.id.content_frame, new FragmentTwo()).commit(); 
        break; 
       case R.id.tab_nearby: 
        Toast.makeText(MainActivity.this, "NEAR", Toast.LENGTH_SHORT).show(); 
        fm.beginTransaction().replace(R.id.content_frame, new FragmentOne()).commit(); 
        break; 
       case R.id.tab_test: 
        Toast.makeText(MainActivity.this, "TEST", Toast.LENGTH_SHORT).show(); 
        fm.beginTransaction().replace(R.id.content_frame, new FragmentTwo()).commit(); 
        break; 
      } 
     } 
    });