尝试在主活动文件中加载两个框架布局

时间:2016-03-31 12:31:13

标签: java android

我正在尝试为不同的活动加载两个框架布局。问题是同时显示数据的帧。我在主java文件中使用了setVisibilty方法。我想,当一帧显示数据时,另一帧自动隐藏。谁能告诉我java代码。

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

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

private void setFrameVisibility (boolean frameOneVisible){
    if (frameOneVisible){
        findViewById(R.id.content_frame).setVisibility(View.VISIBLE);
        findViewById(R.id.content_frametwo).setVisibility(View.GONE);
    } else {
        findViewById(R.id.content_frame).setVisibility(View.GONE);
        findViewById(R.id.content_frametwo).setVisibility(View.VISIBLE);
    }
}

@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.homepage) {
        Intent homepage = new Intent (MainActivity.this, MainActivity.class);
        startActivity(homepage);
                        // Handle the camera action
    } else if (id == R.id.foodpage) {
        //handle the food page here
        fragmentManager.beginTransaction()
                .replace(R.id.content_frame
                        , new FirstFragment())
                .commit();

    } else if (id == R.id.schedulepage) {
        fragmentManager.beginTransaction()
                .replace(R.id.content_frame
                , new ScheduleFragment())
                .commit();

    } else if (id == R.id.emotionspage) {
        fragmentManager.beginTransaction()
                .replace(R.id.content_frame
                , new EmotionsFragment())
                .commit();

    } else if (id == R.id.basicneedspage) {
        fragmentManager.beginTransaction()
                .replace(R.id.content_frametwo
                , new BasicneedsFragment())
                .commit();

    } else if (id == R.id.exit) {
        askBeforeExit();

    }

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

public void onBackPressed() {

    askBeforeExit();
}

private void askBeforeExit(){
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setCancelable(false);
    builder.setTitle("Confirm Exit");
    builder.setMessage("Are you sure you want to quit?");
    builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            finish();
        }
    });
    builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            dialog.cancel();
        }
    });
    AlertDialog alert=builder.create();
    alert.show();
}

}

1 个答案:

答案 0 :(得分:0)

setFrameVisibility中的onCreate添加方法调用以及要在两个布局之间切换的其他方法:

    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);
    setFrameVisibility(true); //or false
}