【问题标题】:Replace content_main with when opening a new fragment from navigation drawer从导航抽屉打开新片段时替换 content_main
【发布时间】:2016-03-10 00:40:06
【问题描述】:

我已经用谷歌搜索了这个但还没有找到答案,我使用的是 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" />

【问题讨论】:

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


    【解决方案1】:

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

    【讨论】:

    • 用于 main_activity 吗?我的其他片段运行良好我只需要在我调用 ID 时隐藏主要内容,如何做到这一点?
    • 我不知道main_activity 或content_main 是什么。您的代码中没有提及它。我认为它是一个单独的片段,只需在片段转换中使用 replace 即可替换它。
    • 我已经更新了我的问题,如果它更容易理解。
    • 首先,更改为FragmentManager 和Fragment 的支持库实现,看看是否能解决您的问题。如果没有,您应该创建一个MCVE,以便以易于重现的方式清楚地显示您的问题。
    【解决方案2】:

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

    要显示fragment_music 布局后面没有content_main,您需要将fragment_music 布局的Relative Layout 的背景设置为white(或任何您想要的),就像content_main将隐藏,您的fragment_layout 将是您想看到的所有内容..

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-16
      • 2015-04-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多