我需要从另一个fragment
开始fragment
。
我有 MainActivity
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
mNavigationView = (NavigationView) findViewById(R.id.shitstuff) ;
mFragmentManager = getSupportFragmentManager();
mFragmentTransaction = mFragmentManager.beginTransaction();
mFragmentTransaction.replace(R.id.containerView,new TabFragment()).commit();
mNavigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
mDrawerLayout.closeDrawers();
//Selecao dos menus
if (menuItem.getItemId() == R.id.nav_reservas) {
FragmentTransaction fragmentTransaction = mFragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.containerView, new Reservas()).commit();
} else if (menuItem.getItemId() == R.id.nav_hotel) {
FragmentTransaction xfragmentTransaction = mFragmentManager.beginTransaction();
xfragmentTransaction.replace(R.id.containerView, new TabFragment()).commit();
} else if (menuItem.getItemId() == R.id.nav_check) {
FragmentTransaction xfragmentTransaction = mFragmentManager.beginTransaction();
xfragmentTransaction.replace(R.id.containerView, new CheckIn()).commit();
} else if (menuItem.getItemId() == R.id.nav_loc) {
FragmentTransaction xfragmentTransaction = mFragmentManager.beginTransaction();
xfragmentTransaction.replace(R.id.containerView, new Endereco()).commit();
} else if (menuItem.getItemId() == R.id.nav_avalie) {
FragmentTransaction xfragmentTransaction = mFragmentManager.beginTransaction();
xfragmentTransaction.replace(R.id.containerView, new Avalie()).commit();
} else if (menuItem.getItemId() == R.id.nav_info) {
FragmentTransaction xfragmentTransaction = mFragmentManager.beginTransaction();
xfragmentTransaction.replace(R.id.containerView, new Informacoes()).commit();
} else if (menuItem.getItemId() == R.id.nav_guia) {
FragmentTransaction xfragmentTransaction = mFragmentManager.beginTransaction();
xfragmentTransaction.replace(R.id.containerView, new GuiaDaCidade()).commit();
}
return false;
}
}
);
我开始使用GuiaDaCidade扩展片段。
public class GuiaDaCidade extends Fragment {
View rootView;
@Nullable
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.guia_da_cidade, null);
View botaoPasseios = (View) rootView.findViewById(R.id.btpasseios);
View botaoAgenda = (View) rootView.findViewById(R.id.btagenda);
botaoPasseios.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
}
});
我需要在单击按钮时启动Passeios扩展Fragment
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.guia_passeios, null);
expListView = (ExpandableListView) rootView.findViewById(R.id.listaexp);
prepareListData();
listAdapter = new ExpandableListAdapter(getActivity(), listDataHeader, listDataChild);
expListView.setAdapter(listAdapter);
expandeAll();
expListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
mudaTela(groupPosition, childPosition);
return false;
}
});
return rootView;
}
我已经尝试了一切,但没办法。
答案 0 :(得分:0)
在启动另一个片段之前,如果要将当前片段保留在backstack中以便在用户按下按钮时可用,请使用片段管理器将其添加到后台堆栈。然后只使用相同的行:fragmentTransaction.replace(containerView,new Fragment(),TAG).commit();
答案 1 :(得分:0)
<强> GuiaDaCidade 强>
botaoPasseios.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Passeios fragment = new Passeios();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.your_guiadacidade_layoutID, fragment);
transaction.addToBackStack(null);
transaction.commit();
}
});