从导航抽屉打开新片段而不替换

时间:2016-10-25 12:49:54

标签: java android android-fragments fragment

我在内容主要布局上完成了设计,当我点击我的选项中的新片段时,另一个片段会覆盖主要内容,请帮我解决,我将如何解决它。这是我的代码。

主要活动

public class MainActivity extends AppCompatActivity
    implements NavigationView.OnNavigationItemSelectedListener {

@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();
        }
    });

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
            this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    drawer.setDrawerListener(toggle);
    toggle.syncState();

    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);
}

@Override
public void onBackPressed() {
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    if (drawer.isDrawerOpen(GravityCompat.START)) {
        drawer.closeDrawer(GravityCompat.START);
    } else {
        super.onBackPressed();
    }
}

@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();

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

    return super.onOptionsItemSelected(item);
}

@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
    // Handle navigation view item clicks here.
    int id = item.getItemId();
    FragmentManager fragmentManager = getFragmentManager();

    if (id == R.id.nav_trail) {
        fragmentManager.beginTransaction()
                .replace(R.id.content_fram
                        , new TrailBalance())
                .commit();
    } else if (id == R.id.nav_profit) {
        fragmentManager.beginTransaction()
                .replace(R.id.content_fram
                        , new ProfitAndLoss())
                .commit();
    } else if (id == R.id.nav_balance) {
        fragmentManager.beginTransaction()
                .replace(R.id.content_fram
                        , new BalanceSheet())
                .commit();
    }

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    drawer.closeDrawer(GravityCompat.START);
    return true;
}}

追踪活动

public class TrailBalance extends Fragment {

ConnectionClass connectionClass;
Button btnSubmit;
EditText edtFrom, edtTo;
View myView;
ProgressBar pbbar;
static Date FromDate = null;
static Date ToDate = null;


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    connectionClass = new ConnectionClass();
    myView = inflater.inflate(R.layout.trail_balance, container, false);
    return myView;
}

内容Main.xml

<?xml version="1.0" encoding="utf-8"?>

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

但我的结果就是这样 This is Main Content Fields This is Tail fragment 在此Trail片段类中覆盖Content类。

1 个答案:

答案 0 :(得分:0)

尝试在MainActivity中加载片段FristTime,就像那样

  if (savedInstanceState == null) {
        Fragment newFragment = new YourFragment();
        FragmentTransaction ft = getFragmentManager().beginTransaction();
        ft.add(R.id.fragment_frame, newFragment);
        ft.addToBackStack(null);
        ft.commit();
    }

并用于替换片段或任何地方的片段

使用此方法从另一个

替换片段
public static void launchFragment(Fragment fragment, Context context){
    FragmentManager fragmentManager = ((AppCompatActivity)context).getFragmentManager();
    FragmentTransaction ft = fragmentManager.beginTransaction();
    ft.addToBackStack(null);

    ft.replace(R.id.main_content, fragment);
    ft.commit();
}

希望这会对你有所帮助。如果有帮助

,请不要忘记 accept 回答