how to move from one fragment to another fragment on button click

前端 未结 6 1901
失恋的感觉
失恋的感觉 2021-01-16 11:43

I am using the SherlockFragments library for the sliding menu.I have list of items as menu when I click on item fragment get opened as an activity but it is a fragment.Now

相关标签:
6条回答
  • 2021-01-16 11:49

    This is the code I use to switch fragments inside a view:

    FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
    fragmentTransaction.replace([viewId], fragment, tag);
    fragmentTransaction.addToBackStack(tag);
    fragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
    fragmentTransaction.commit();
    
    0 讨论(0)
  • 2021-01-16 11:55

    You can try this also:-

    public void ButtonClick(View view) {
      Fragment mFragment = new YourNextFragment(); 
     getSupportFragmentManager().beginTransaction()
                .replace(R.id.content_frame, mFragment ).commit();
     }
    
    0 讨论(0)
  • 2021-01-16 11:55
    FragmentManager fragmentManager = getFragmentManager();
                    fragmentManager
                            .beginTransaction()
                            .replace(R.id.frame_container,
                                    new TeacherFragment()).commit();
    
    0 讨论(0)
  • 2021-01-16 12:03
     FragmentTransaction fragmenttransaction = getSupportFragmentManager().beginTransaction();
     FirstFragment regcomplainfragment = new FirstFragment();
     fragmenttransaction.replace(R.id.content_frame,   regcomplainfragment).addToBackStack("tag");
     fragmenttransaction.commit();
    
    0 讨论(0)
  • 2021-01-16 12:11
     //Below is the example 
    
     //In first Fragment 
    
    Fragment fragment = new YourFragmentClassName();
    FragmentManager fragmentManager = getActivity().getSupportFragmentManager();                       android.support.v4.app.FragmentTransaction ft= fragmentManager.beginTransaction();
    ft.replace(R.id.flContent, fragment);
    Bundle args = new Bundle();
    // Pass the values what you want to send to next fragment 
    args.putInt("Year", rYear);
    args.putString("Month", rMonth);
    args.putInt("Industry", rIndustry);
    fragment.setArguments(args);
    ft.commit();
    
    //In Second Fragment
    
    //In onCreateView Method get the values
    
    
    
    int strYear= getArguments().getInt("Year");
    String strMonth = getArguments().getString("Month");
    strIndustry= getArguments().getInt("Industry");
    
    //That's it very simple
    
    0 讨论(0)
  • 2021-01-16 12:13

    It's simple Only three line code...

    Fragment fragment = new SalesFragment();
    
    FragmentManager fragmentManager = getFragmentManager();
    
    fragmentManager.beginTransaction().replace(R.id.frame_container, fragment).commit();
    
    0 讨论(0)
提交回复
热议问题