从导航抽屉打开新片段时替换content_main

时间:2016-03-10 00:40:06

标签: java android android-fragments android-studio fragment

我已经搜索了这个并且还没有找到答案,我正在使用Android导航抽屉模板,我从导航菜单中点击时有一些相互替换的片段,当应用首次打开时,它会在Content_Main布局上打开当我点击我的一个片段时它会打开,但是content_main中的内容仍然显示,我需要隐藏,同时我使用其他片段,还有一个主页按钮,允许我从菜单返回到这个内容,任何帮助?

问题是当我打开应用程序时,Content_main布局出现,当我滑动导航抽屉并在菜单中选择nav_gallery音乐片段打开但内容主要没有被替换。

这是我的MainActivity

package com.justmikey.justmik;

 import android.media.MediaPlayer;
 import android.os.Bundle;
 import android.support.design.widget.FloatingActionButton;
 import android.support.design.widget.Snackbar;
 import android.support.v4.app.FragmentManager;
 import android.support.v7.internal.widget.ButtonBarLayout;
 import android.text.method.ScrollingMovementMethod;
 import android.view.View;
 import android.support.design.widget.NavigationView;
 import android.support.v4.view.GravityCompat;
 import android.support.v4.widget.DrawerLayout;
 import android.support.v7.app.ActionBarDrawerToggle;
 import android.support.v7.app.AppCompatActivity;
 import android.support.v7.widget.Toolbar;
 import android.view.Menu;
 import android.view.MenuItem;
 import android.widget.Button;
 import android.widget.TextView;

 import JustMikey.Fragments.MainFragment;
 import JustMikey.Fragments.MusicFragment;

 public class MainActivity extends AppCompatActivity
    implements NavigationView.OnNavigationItemSelectedListener {

//create a media player object called mp
MediaPlayer mp;
//declare my buttons play, pause and stop
Button play, pause,stop;

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

    //Set the textView scrollMain to scrollable
    TextView tv = (TextView) findViewById(R.id.scrollMain);
    tv.setMovementMethod(new ScrollingMovementMethod());

    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 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) {
    android.app.FragmentManager fm = getFragmentManager();

    int id = item.getItemId();

    if (id == R.id.nav_camara) {

   fm.beginTransaction().replace(R.id.content_frame, new MainFragment()).commit();


    } else if (id == R.id.nav_gallery) {

        fm.beginTransaction().replace(R.id.content_frame, new MusicFragment()).commit();


    } else if (id == R.id.nav_slideshow) {

    } else if (id == R.id.nav_manage) {

    } else if (id == R.id.nav_share) {

    } else if (id == R.id.nav_send) {

    }

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

这就是content_main

<FrameLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/content_frame">

    <ImageView
        android:layout_width="355dp"
        android:layout_height="wrap_content"
        android:src="@drawable/jmmain"
        android:id="@+id/imageView2"
        android:layout_gravity="center_horizontal|top"
        android:layout_alignRight="@+id/content_frame"
        android:layout_alignEnd="@+id/content_frame" />

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="274dp"
        android:text="hello hello hello hello hello hello hello hello hello  hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello "
        android:id="@+id/scrollMain"
        android:layout_gravity="center_horizontal|bottom"
        android:layout_alignBottom="@+id/scrollView"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginBottom="28dp"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:background="#ffffff"
        android:padding="10dp" />

    <ScrollView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@+id/scrollView"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_gravity="center"></ScrollView>


</FrameLayout>

</RelativeLayout>

这是我的MusicFragmant

package JustMikey.Fragments;

import android.app.Fragment;
import android.graphics.LightingColorFilter;
import android.graphics.PorterDuff;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

import com.justmikey.justmik.R;

 /**
  *     Created by James on 24/02/2016.
  */
public class MusicFragment extends Fragment implements View.OnClickListener {

//create a media player object called mp
MediaPlayer mp;
//declare my buttons play, pause and stop
Button play, pause,stop;




@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle  savedInstanceState) {

    View rootView = inflater.inflate(R.layout.fragmentmusic, container,  false);


    //call my button views in by using the ID tag
    play = (Button) rootView.findViewById(R.id.play);
    pause = (Button) rootView.findViewById(R.id.pause);
    stop = (Button) rootView.findViewById(R.id.stop);

    //set an onClickListener to make my buttons clickable
    play.setOnClickListener(this);
    pause.setOnClickListener(this);
    stop.setOnClickListener(this);

    //Change the colour of the pause button for a nice design effect
    pause.getBackground().setColorFilter(new LightingColorFilter(0x00000000,  0xFFAA0000));
    //return the view
    return rootView;
}
// onClick Method for the play, pause and stop buttons
public void onClick(View v) {
    switch(v.getId()) {
        case R.id.play : if(mp == null) {
            mp = MediaPlayer.create(getActivity(), R.raw.holdmytong);
        }
            mp.start();
            break;
        case R.id.pause : mp.pause();break;

        case R.id.stop : mp.stop();mp = null;break;
    }

    }



}

fragment_music布局

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

<ImageView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:adjustViewBounds="true"
    android:scaleType="fitCenter"
    android:id="@+id/imageView3"
    android:src="@drawable/jmholdmy"
    android:layout_gravity="center_horizontal|top" />

<Button
    style="?android:attr/buttonStyleSmall"
    android:layout_width="100dp"
    android:layout_height="74dp"
    android:id="@+id/pause"
    android:layout_alignTop="@+id/play"
    android:layout_toRightOf="@+id/play"
    android:layout_toEndOf="@+id/play"
    android:layout_gravity="center_horizontal|bottom"
    android:background="@drawable/pausebtnimg" />

<Button
    style="?android:attr/buttonStyleSmall"
    android:layout_width="113dp"
    android:layout_height="72dp"
    android:id="@+id/stop"
    android:layout_alignTop="@+id/play"
    android:layout_toRightOf="@+id/pause"
    android:layout_toEndOf="@+id/pause"
    android:layout_gravity="right|bottom"
    android:background="@drawable/stopbtnimg" />

<Button
    style="?android:attr/buttonStyleSmall"
    android:layout_width="114dp"
    android:layout_height="72dp"
    android:id="@+id/play"
    android:layout_marginTop="170dp"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_gravity="left|bottom"
    android:background="@drawable/playbtnimg" />

<TextView
    android:layout_width="match_parent"
    android:layout_height="258dp"
    android:id="@+id/textView4"
    android:layout_gravity="left|bottom"
    android:text="Taking from our debut EP Hold my tongue was our first released song, it won play at the Picnic competition which brought Just Mikey to the stage at the largest festivil in Europe Electric Picnic, This is the first of many to come so enjoy.  " />

<TextView
    android:layout_width="314dp"
    android:layout_height="156dp"
    android:text="Buy Track: https://www.facebook.com/search/top/?q=just%20mikey%20google%20play"
    android:id="@+id/textView5"
    android:layout_gravity="center_horizontal|bottom"
    android:backgroundTint="#000000"
    android:backgroundTintMode="screen" />

2 个答案:

答案 0 :(得分:0)

AppCompatActivity应该使用通过FragmentManager检索到的getSupportFragmentManager()

答案 1 :(得分:0)

回答为时已晚,但我会回答以防万一其他人偶然发现这个问题。

要显示fragment_music布局而不显示content_main,您需要将Relative Layout布局fragment_music的背景设置为white(或你想要的任何东西)content_main将会隐藏,fragment_layout将是你想要看到的所有......