活动被片段隐藏,不可用

时间:2016-02-16 13:52:27

标签: java android android-fragments fragment android-5.0-lollipop

我已经建立了一个适用于Android的应用程序,其api高于21,问题是我的代码的这条指令:

        mFragmentTransaction.replace(R.id.content_frame, new TabFragment()).commit();

在不到21岁的API上以不同的方式工作。

在Api少于21时,新片段隐藏了previus活动,因此我无法点击我的浮动操作按钮。 这里有两张图片可以解释我的问题。

API HIGER> 21

enter image description here

API不到21

enter image description here

所以我的问题是:我怎样才能在API中获得相同的结果21,那时我在API Higer然后21?

以下是主要活动代码的受影响部分:

public class MainActivity extends AppCompatActivity {
public static AppDataBase appDataBase;
public static UserDataBase userDataBase;
static FragmentManager mFragmentManager;
static FragmentTransaction mFragmentTransaction;
private DrawerLayout myDrawerLayout;
final String TXT_MAINACTVT_USER_HAVE_NOT_ADDED_CONSOLE = "Add a console!";
TextView currentConsole;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    appDataBase = new AppDataBase(this);
    userDataBase = new UserDataBase(this);
    myDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);

    currentConsole = (TextView) findViewById(R.id.txt_Mainactvt_currentConsole);
    currentConsole.setText(TXT_MAINACTVT_USER_HAVE_NOT_ADDED_CONSOLE);

    tabLayoutManagement();
    floatingActionButtonManagement();
    leftDrawerMenuManagement();
    rigthDrawerMenuManagement();
    populateMyConsole();
}

void tabLayoutManagement() {
    mFragmentManager = getSupportFragmentManager();
    mFragmentTransaction = mFragmentManager.beginTransaction();
    mFragmentTransaction.replace(R.id.content_frame, new TabFragment()).commit();
}

// Floating Action Button
private void floatingActionButtonManagement() {
    FloatingActionButton fab_addGame = (FloatingActionButton)findViewById(R.id.fab_AddGame);
    fab_addGame.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            /*
            The currentConsole TextView is used to show at the user wich console is selected.
            We use it to have a strng that conteins the selected console.
            Call the method that manage the click event of the FloatingActionButton. We pass the console name.
             */
            String currentConsoleName = currentConsole.getText().toString();

            floatingActionButtonClickEvent(currentConsoleName);
        }
    });
}

private void floatingActionButtonClickEvent(String currentConsoleName) {
    /*
    Check if user have added a console. If he did start a menu for adding games, else start an
    error message
     */
    if (!currentConsoleName.equals(TXT_MAINACTVT_USER_HAVE_NOT_ADDED_CONSOLE)) {
        popUpViewAddGameBuild(currentConsoleName);
    }
    else
        mySimpleAlertDialogMethod("Attention!", "Before you enter game, you must enter a console.", true, true);
}

private void popUpViewAddGameBuild(String currentConsoleName) {
    /*
    Build the view that show the menu for adding games.
     */
    LayoutInflater inflater = this.getLayoutInflater();
    View popupView = inflater.inflate(R.layout.popupview_addgame, null);
    PopupWindow popupWindow = new PopupWindow(
            popupView,
            WindowManager.LayoutParams.MATCH_PARENT,
            WindowManager.LayoutParams.MATCH_PARENT);
    popupWindow.setInputMethodMode(PopupWindow.INPUT_METHOD_NEEDED);
    popupWindow.setFocusable(true);
    popupWindow.showAtLocation(popupView, 0, 0, 0);
  }

这是TabLayout类:

public class TabFragment extends Fragment {
public static TabLayout tabLayout;
public static ViewPager viewPager;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View x =  inflater.inflate(R.layout.tab_layout, null);
    tabLayout = (TabLayout) x.findViewById(R.id.tabs);
    viewPager = (ViewPager) x.findViewById(R.id.viewpager);

    viewPager.setAdapter(new MyAdapter(getChildFragmentManager()));



    tabLayout.post(new Runnable() {
        @Override
        public void run() {
            tabLayout.setupWithViewPager(viewPager);
        }
    });
    return x;
}

public class MyAdapter extends FragmentPagerAdapter {
    public int position;

    public MyAdapter(FragmentManager fm) {
        super(fm);
    }
    @Override
    public Fragment getItem(int position) {
        switch (position){
            case 0 :
                return new DesireFragment();
            case 1 :
                return new BuyedFragment();
            case 2 :
                return new StartedFragment();
            case 3 :
                return new FinishedFragment();
            case 4 :
                return new AllTrophiesFragment();
        }
        return null;
    }

    @Override
    public int getCount() {
        return 5;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        switch (position){
            case 0 :
                return "Desire";
            case 1 :
                return "Buyed";
            case 2 :
                return "Started";
            case 3 :
                return "Finished";
            case 4 :
                return "AllTrophies";
        }
        return null;
    }
}
}

MainActivity的布局:

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:layout_gravity="end"
android:orientation="vertical">

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">


    <android.support.v7.widget.Toolbar
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/colorPrimary"
        android:id="@+id/toolbar"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        app:navigationIcon="@drawable/ic_menu_white_24dp"
        app:title="MyGames">

        <Button
            android:id="@+id/btnOpenRigthDrawer"
            android:background="@drawable/ic_filter_list_white_24dp"
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:layout_marginRight="10dp"
            android:layout_gravity="right" />

        <Button
            android:id="@+id/btnOpenOptions"
            android:background="@drawable/ic_settings_white_24dp"
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:layout_marginRight="17dp"
            android:layout_gravity="right" />

    </android.support.v7.widget.Toolbar>

</RelativeLayout>

<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:scrollIndicators="bottom">

    <FrameLayout
        android:id="@+id/content_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:backgroundTint="@color/colorPrimary"
        app:borderWidth="0dp">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="30dp"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:id="@+id/txt_Mainactvt_currentConsole"
            android:layout_gravity="center_horizontal|top"
            android:layout_marginTop="50dp"/>

        <android.support.design.widget.FloatingActionButton
            android:id="@+id/fab_AddGame"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="17dp"
            android:layout_marginRight="17dp"
            android:src="@drawable/ic_mode_edit_white_24dp"
            android:layout_gravity="bottom|right"
            android:background="@color/colorPrimary" />

    </FrameLayout>

    <include
        layout="@layout/drawer_left"
        android:id="@+id/layLeft"
        android:layout_gravity="start"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scrollbars="vertical"/>

    <include
        layout="@layout/drawer_rigth"
        android:id="@+id/layRigth"
        android:layout_gravity="end"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#ffffff"
        android:scrollbars="vertical"
        />
</android.support.v4.widget.DrawerLayout>

这是TabLayout的布局代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="wrap_content">


<android.support.design.widget.TabLayout
    android:id="@+id/tabs"
    app:tabGravity="fill"
    app:tabMode="scrollable"
    android:background="@color/colorPrimary"
    app:tabIndicatorColor="@android:color/holo_orange_dark"
    app:tabSelectedTextColor="@android:color/holo_orange_dark"
    app:tabTextColor="@android:color/white"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    >
</android.support.design.widget.TabLayout>

<android.support.v4.view.ViewPager
    android:id="@+id/viewpager"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
</android.support.v4.view.ViewPager>

2 个答案:

答案 0 :(得分:1)

使用片段替换FrameLayout的内容。这会导致你的奇怪结果。 将一个布局添加到Framelayout中,并将其用作FragmentContainer:

<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:backgroundTint="@color/colorPrimary"
    app:borderWidth="0dp">

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

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="30dp"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:id="@+id/txt_Mainactvt_currentConsole"
        android:layout_gravity="center_horizontal|top"
        android:layout_marginTop="50dp"/>

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab_AddGame"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="17dp"
        android:layout_marginRight="17dp"
        android:src="@drawable/ic_mode_edit_white_24dp"
        android:layout_gravity="bottom|right"
        android:background="@color/colorPrimary" />

</FrameLayout>

答案 1 :(得分:0)

尝试将带有此按钮的FrameLayout放置到布局的末尾