简体   繁体   中英

How to Call Navigation Drawer Fragment From A Fragment

So I have a Google Map integrated in one of my Fragment components and I also have a navigation drawer. The google maps is in one of my drawer fragments. My question is how do I change fragments when I click a place marker info window.

public class AboutFragment extends Fragment {
   private GoogleMap mMap;

...

mMap.setOnInfoWindowClickListener(new GoogleMap.OnInfoWindowClickListener() {
        @Override
        public void onInfoWindowClick(Marker marker) {
            Toast.makeText(getActivity(), "Clicked on Info view", Toast.LENGTH_SHORT).show();
            // This is where I want to call my ProductsFragment class.


        }
});

...

}

This is what my ProductsFragment class looks like below.

public class ProductsFragment extends Fragment {


public ProductsFragment() {
    // Required empty public constructor
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    return inflater.inflate(R.layout.fragment_products, container, false);
}

}

I tried using FragmentManager but I got errors. Maybe I wasnt doing it right but can you guys help me?

Thanks

I got it to work

Fragment fragment = fragmentProducts;
FragmentTransaction ft = getFragmentManager().beginTransaction();
Fragment aboutFragment = MainActivity.setFragment();
ft.remove(aboutFragment);
ft.add(R.id.container_body, fragment).commit();

But it doesnt change the ActionBar title?

Any clues to how to go changing that in this class?

"But it doesnt change the ActionBar title? Any clues to how to go changing that in this class?"

yeah just call:

getActionBar().setTtitle(fragmentTitle);

where ?

** you can add listener :) to drawer

DrawerLayout.setDrawerListener(DrawerLayout.DrawerListener)

// example for open/close/changeState etc
@Override
public void onDrawerOpened(View drawerView) {
            // set title or subtitle
            getActionBar().setSubtitle(title);
}

**or in onBackPressed() if u get back from prev fragment

**or after ItemSelected from fragment adapter

**or after statement as in yr code:

ft.add(R.id.container_body, fragment).commit(); 

**or in fragment

@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
    // set title or subtitle
    getActionBar().setSubtitle(fragmentTitle);
}

as you see it depends where & when u wanna do it

I get the error "cannot resolve method getActionBar()". Do I need something else with that line of code?

ActionBar

AppCompatActivity

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM