当前位置: 首页 > 知识库问答 >
问题:

从导航抽屉中打开新片段时,将content_main替换为

单于奕
2023-03-14

我已经搜索了这个,还没有找到答案,我使用Android导航抽屉模板,当从导航菜单点击时,我有一些片段相互替换,当应用程序第一次打开时,它在Content_Main布局上打开,当我点击我的一个片段时,它打开了,但Content_Main的内容仍然显示,我需要隐藏,而我使用其他片段,也做了一个主页按钮,让我从菜单上回到这个内容,有什么帮助吗?

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

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

    }



}
<?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" />

共有1个答案

皇甫逸清
2023-03-14

appcompatactivity应该使用通过getsupportfragmentManager()检索的fragmentManager

 类似资料:
  • 我正在使用developer.android.com指南来构建一个应用程序。我在Android Studio做新项目时选择了“导航:导航抽屉”。我已经在互联网上搜索我的问题的答案,但我没有找到任何有效的。抱歉,我是编程新手。 在导航抽屉中单击时,如何使应用程序在主视图中打开一个新的片段? 在导航抽屉中单击时,是否可以使用选项卡打开多个可滑动的片段? 如何使“标题”可展开/可折叠? http://d

  • 我希望能够使用导航抽屉,并根据导航中的选择在不同片段之间进行更改。 我正在使用Android Studio,基本上我所做的是这样的: 使用内置模板“导航抽屉活动”创建了一个新项目 创建了一个空白片段 然后我更改了onNavigationDrawerItemSelected方法中的一些代码。 我的程序崩溃了,给了我很多我不理解的错误。我做错了什么?

  • 我已经创建了默认的Android Studio导航抽屉项目。默认情况下,我有一个活动和三个主要片段。(家庭,画廊,幻灯片),我只是尝试与家庭片段。我已经将 从content_main XML替换为 并将MainActivity修改为

  • 所以我有一个活动,其中有一个片段,片段有一个导航抽屉和页面的内容。当我打开抽屉并单击一个项目时,片段被替换为一个新片段。当我按下后退按钮时,我在片段管理器上调用popBackStack,它返回到第一个片段,但导航抽屉是打开的。 有几件事要注意:当按下抽屉中的一个项目时,我在抽屉布局上调用关闭抽屉,当片段被替换时抽屉关闭。如果我按下操作栏中的UP按钮,我可以用新的主片段替换片段容器,但我更喜欢能够将

  • 我是一名iOS开发者,我开始尝试将我的一个应用程序移植到Android。一切都很顺利,直到我遇到了一个问题,这让我在过去几天里步履维艰。 我的应用程序使用的是导航抽屉模板,工作正常。通过点击所需的导航项,我可以很容易地在片段之间切换。然而,问题是当我试图从现有片段中切换内容时。换句话说,我的一个片段上有一个按钮,单击时,我希望它用另一个片段替换内容。我已经使用以下代码实现了这一点: 当此代码切换到

  • 我目前正在为我的Android应用程序使用导航抽屉。在我的第一个片段中,有一个片段使用Facebook的Graph API加载数据。因此,当我的应用程序第一次加载时,它首先进入第一个片段。 然后,我使用导航抽屉单击另一个片段并查看它。 最后,我重用导航抽屉返回第一个片段并查看它。 我面临的问题是,我如何继续利用已经创建过一次的片段,而不是在选择导航抽屉项时重新创建它。我的片段切换代码如下所示。 我