Android development fragment Fragment layout example (switch fragment layout with buttons)

Implementation ideas:

1. Write a parent class layout, which writes a button and a frame layout (for subsequent replacement of Fragment layout )

2. Write 3 sub-layouts, and write 3 classes to inherit the Fragment layout

3. Write a method to replace the fragment layout in the class of MainActivity

(Includes: FragmentManger (fragment manager), getSupportFragmentManager (supported fragment manager), FragmenTransaction (fragment swap), beginTransaction (start fragment swap), replace (replace), commit (delivery))


1. Write a parent class layout, which writes a button and a frame layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >
   <Button
       android:id="@+id/Button1"
       android:layout_width="match_parent"
       android:layout_height="0dp"
       android:layout_weight="1"
       android:text="Change the fragment below"/>
    <FrameLayout
        android:id="@+id/FrameLayout1"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="8">
    </FrameLayout>


</LinearLayout>

2. Write 3 sub-layouts, and write 3 classes to inherit the Fragment layout

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="10dp">
    <ImageView
        android:id="@+id/fragment_1_ImageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:src="@drawable/ace"/>
    <TextView
        android:id="@+id/fragment_1_TextView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="@string/ace"
        android:textColor="@color/colorBlack"
        android:layout_marginTop="20dp"/>
</LinearLayout>

The same layout is writing 2

Write a class that inherits fragment to instantiate the fragment layout:

package com.example.lenovo.myfragmentdemo2.fragment;
// Note that Fragment's dependency library is recommended to use the support-v4 library uniformly
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.lenovo.myfragmentdemo2.R;


/**
 * Created by lenovo on 2018/5/7.
 */

public class Fragment1 extends Fragment {
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_1,container,false);
        return view;
    }
}

3. Write a method to replace the fragment layout in the class of MainActivity

package com.example.lenovo.myfragmentdemo2;
// Note Fragment's dependency library
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import com.example.lenovo.myfragmentdemo2.fragment.Fragment1;
import com.example.lenovo.myfragmentdemo2.fragment.Fragment2;
import com.example.lenovo.myfragmentdemo2.fragment.Fragment3;

public class MainActivity extends AppCompatActivity {
    private Button button;
    private int i = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate (savedInstanceState);
        setContentView(R.layout.activity_main);
        button = (Button)findViewById(R.id.Button1);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switch (i){
                    case 1:
                        replaceFragment(new Fragment1());
                        i++;
                        break;
                    case 2:
                        replaceFragment(new Fragment2());
                        i++;
                        break;
                    case 3:
                        replaceFragment(new Fragment3());
                        i++;
                        break;
                    default:
                        replaceFragment(new Fragment1());
                        i=1;
                        break;
                }

            }
        });
    }

    public void  replaceFragment(Fragment fragment){
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        transaction.replace(R.id.FrameLayout1,fragment);
        transaction.commit();

    }
}


running result:

After clicking:

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325733553&siteId=291194637