【问题标题】:Issue with Fragment Transaction (default fragment displaying twice)片段事务问题(默认片段显示两次)
【发布时间】:2017-02-11 07:28:56
【问题描述】:

我正在使用底部栏处理片段事务。在我的应用程序默认片段中显示两次,并且在选择第二个片段时它不会隐藏..

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) {
        }
    });
}

请帮我解决这个问题 这是我的 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>

【问题讨论】:

  • 把你的xml代码也给我们

标签: android android-fragments fragment android-fragmentactivity fragmentmanager


【解决方案1】:

只需用相对布局替换片段来替换下面的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">
<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>

希望对你有所帮助.....

【讨论】:

    【解决方案2】:

    不要添加片段。添加一个容器,然后用一个片段替换它。在这里,框架布局将用作容器。

    您的 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>
    

    在 Activity 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;
                }
            }
        });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-07-31
      • 2014-06-11
      • 1970-01-01
      • 2021-11-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多