Replace one fragment with an another fragment

前端 未结 8 1545
再見小時候
再見小時候 2020-12-16 19:37

I want to replace an old Fragment with a new Fragment, but i still get the buttons of the old Fragment that is still visible in the ne

相关标签:
8条回答
  • 2020-12-16 20:22

    I Have worked on fragments before and hope this would help you out and give you a better understanding of the flow. Firstly, your MainActivity.xml file will look like this :

    <?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"
       app:layout_behavior="@string/appbar_scrolling_view_behavior"
       tools:context="com.example.activity.HomeActivity">
    
    
        //This frameLayout will contain all your fragments view.
       <FrameLayout
          android:id="@+id/container_view"
          android:layout_width="match_parent"
          android:layout_height="match_parent">
        </FrameLayout>
    
     </RelativeLayout>
    

    Next, you create two fragments and their XML is mentioned below : fragment1.xml

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:clickable="true" // important to have this
        tools:context=".fragments.frament1">
    
         <Button
            android:id="@+id/btn"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom"/>
    
     </RelativeLayout>
    

    The next fragment would look exactly the same as mentioned above. Here is the Fragment1.class:

    public class Fragment1 extends Fragment implements View.OnClickListener {
    Button btn;
    
       public Fragment1() {
        // Required empty public constructor
    }
    
         @Override
         public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
            View view = inflater.inflate(R.layout.fragment1, container, false);
            view.setBackgroundColor(Color.WHITE);
    
           //Perform required conditions and return view 
           button = (Button) view.findViewById(R.id.btn);
           button.setOnClickListener(this);
    
           return view;
          }
    
            public void onClick(View v) {
    
                 switch(v.getId())
                 {
                    case R.id.btn: 
                     //replace current fragment on button click
    
                     Fragment fragment2= new Fragment2();
    
                     getFragmentManager().beginTransaction().
                     replace(R.id.container_view, fragment2).
                     addToBackStack("frags").commit();
    
                   break;
                 }
            }
     }
    

    And the Fragment2 would be as follows :

     public class Fragment2 extends Fragment{
     String TAG = "Fragment2";
    
    
         public Fragment2() {
          // Required empty public constructor
       }
    
         @Override
         public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
    
              View view = inflater.inflate(R.layout.fragment2,container,false);
              view.setBackgroundColor(Color.WHITE);
           return view;
        }
    }
    

    As I mentioned earlier, the xml file would be the same as fragment1.xml. What is more important here is that the main activity will contain a layout which will take the fragments view when ever the user switches fragments. Hence we use the replace method which would just replace the previous view with the fragments view specified by us.

    0 讨论(0)
  • 2020-12-16 20:23

    try this once, 1. if you are passing any value upon button click In Activity

    Category category=new Category();
                Bundle bundle=new Bundle();
                bundle.putString("heading",heading);
                bundle.putInt("position",position1+1);
                bundle.putString("url",url);
                bundle.putString("sku",sku);
                bundle.putBoolean("flag",flag);
                category.setArguments(bundle);
    
                FragmentManager fragmentManager = getSupportFragmentManager();
                final FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
                fragmentTransaction.replace(R.id.fragmentCategories,category);
                fragmentTransaction.commit();
    

    In fragment

    Bundle bundle=getArguments();
        if(getArguments()!=null) {
            position = bundle.getInt("position");
            heading = bundle.getString("heading");
            url = bundle.getString("url");
            sku=bundle.getString("sku");
            flag=bundle.getBoolean("flag");
    
            tvHeading.setText(heading);
    
            video_chapter = handler.getContent_Aspects(position);
            adapter = new Chapter_content_Adapter(getActivity(), video_chapter, url, heading, position);
            gvChapter.setAdapter(adapter);
        }
    

    2.if simply calling fragment

    FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentCategories=fragmentManager.findFragmentById(R.id.fragmentCategories);
        fragmentTransaction.commit();
    
    0 讨论(0)
提交回复
热议问题