我正在尝试使用片段更改content_navegacao.xml的内容,但提交似乎无效。
我创建了一个带有空活动布局的项目,之后我创建了一个导航抽屉活动,并且我试图在导航抽屉布局中更改我的片段的内容。我的事务在Navigation_Drawer活动中进行管理。我想,也许在这种情况下使用的进口可能是 对此负责,因为导航抽屉布局扩展了AppCompactAcivity类。
我做了类似的事情:
在content_navegacao中创建片段:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="receitas.cleiton.com.br.receitas.Navegacao_Activity"
tools:showIn="@layout/app_bar_navegacao_">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/container_conteudo">
</LinearLayout>
</RelativeLayout>
创建片段布局 - &gt; fragment_home.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ff0000">
</LinearLayout>
创建片段类
package receitas.cleiton.com.br.receitas;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class Home_Fragment extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_home,null);
return view;
}
}
最后,我在NavigationDrawerAcitvity类上进行了最后的实现:
活动变得像:
package receitas.cleiton.com.br.receitas;
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.v4.app.FragmentTransaction;
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.Toast;
public class Navegacao_Activity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
FragmentManager fm = getSupportFragmentManager();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_navegacao_);
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);
if(savedInstanceState == null){
Home_Fragment home_fragment = new Home_Fragment();
FragmentTransaction fragmentTransaction = fm.beginTransaction();
fragmentTransaction.add(R.id.container_conteudo,home_fragment,"home");
fragmentTransaction.addToBackStack("stack");
fragmentTransaction.commit();
}
}
但......当活动加载时,没有任何反应。
任何想法?
答案 0 :(得分:1)
在没有局部变量的if(savedInstanceState == null){
之后尝试。
getSupportFragmentManager().beginTransaction().replace(fragment, R.id.container, "home").addToBackStack("tag").commit();
P.S.I不建议在公告后立即初始化变量。在onCreate()方法中执行;