调用replace方法后片段仍然可见

时间:2014-10-07 17:33:08

标签: android android-fragments fragmenttransaction android-actionbaractivity

我知道这有其他答案但我已经环顾四周并尝试了其他解决方案;所有这些都不起作用。

我有一个包含可点击项目的菜单;点击后,他们会将主屏幕上的Fragment替换为另一个Fragment

这是我的代码:

 package com.nanospark.cnc;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.ActionBarActivity;
//import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

public class MainActivity extends ActionBarActivity {
    GridLayout_Fragment profileGridFragment = new GridLayout_Fragment();
    EventList_Fragment eventListFragment = new EventList_Fragment();
    ContactList_Fragment contactListFragment = new ContactList_Fragment();
    FragmentManager transactionManager = getSupportFragmentManager();
    FragmentTransaction transaction;
    //CustomIOIO customioio;

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

        if(savedInstanceState == null){
            transaction = transactionManager.beginTransaction();
            transaction.replace(R.id.fragment_frame, profileGridFragment);
            transaction.commit();
        }
        //insert the initial fragment for when the app boots.


/*       customioio = (CustomIOIO) getApplicationContext();
         customioio.create();
         customioio.start();*/

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.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();
        switch (id) {
        case R.id.action_settings:
            openSettings();
            return true;
        case R.id.action_home:
            openHome();
            return true;
        case R.id.action_events:
            openEvents();
            return true;
        case R.id.action_help:
            openHelp();
            return true;
        case R.id.action_contacts:
            openContacts();
            return true;

        }
        return super.onOptionsItemSelected(item);
    }

    private void openContacts() {

        transaction = transactionManager.beginTransaction();
        transaction.replace(R.id.fragment_frame, contactListFragment);

        transaction.commit();
    }

    private void openHome() {

        transaction = transactionManager.beginTransaction();
        transaction.replace(R.id.fragment_frame, profileGridFragment);

        transaction.commit();
    }

    private void openHelp() {

    }

    private void openEvents() {
        transaction = transactionManager.beginTransaction();
        transaction.replace(R.id.fragment_frame, eventListFragment);

        transaction.commit();
    }

    private void openSettings() {

    }
}

我不确定为什么以前的Fragment仍然可见?

编辑 - 添加我的activiy_main.xml布局文件:

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.nanospark.cnc.MainActivity" >

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:orientation="vertical" >

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

        </FrameLayout>

    </LinearLayout>

</RelativeLayout>

0 个答案:

没有答案