Android serial 8-dynamic add fragment

Keywords: Android Fragment xml encoding

1, Add fragment dynamically

 

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:background="#ffff00"

    android:orientation="vertical" >

   

    <TextView

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_gravity="center_horizontal"

        android:textSize="20sp"

        android:text="This is another right franment"

        /></LinearLayout>

 

Let's modify a layout page first, and then add this page to the main page, as shown in the following XML

<LinearLayout 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" ><fragment

      android:id="@+id/left_fragment"

      android:name="com.example.fragmenttest.LeftFragment"

      android:layout_width="0dp"

      android:layout_height="match_parent"

      android:layout_weight="1" />

 

  <FrameLayout

      android:id="@+id/right_layout"

      android:layout_width="0dp"

      android:layout_height="match_parent"

      android:layout_weight="1" >

     

    <fragment

        android:id="@+id/right_fragment"

        android:name="com.example.fragmenttest.RightFragment"

        android:layout_width="match_parent"

        android:layout_height="match_parent" />

 

  </FrameLayout>

 

</LinearLayout>

 

After we make the layout, we start to edit the main activity

 

package com.example.fragmenttest;

​

import android.app.Activity;

import android.app.FragmentManager;

import android.app.FragmentTransaction;

import android.os.Bundle;

import android.view.Menu;

import android.view.MenuItem;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

​

public class MainActivity extends Activity implements OnClickListener{

​

  @Override

  protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);

    Button button = (Button) findViewById(R.id.button);

    button.setOnClickListener(this);

  }

​

  @Override

  public void onClick(View v) {

    switch(v.getId()) {

    case R.id.button:

      AnotherRightFragment fragment = new AnotherRightFragment();

      FragmentManager fragmentManager = getFragmentManager();

      FragmentTransaction transaction = fragmentManager.beginTransaction();

      transaction.replace(R.id.right_layout,fragment);

      transaction.commit();

      break;

    default:

      break;

    }

  }

}

 

Summary: (1) create the fragment instance to be added; (2) get the fragment manager and call the getFragmentManager() method directly in the activity; (3) open a transaction by calling the beginTransaction() method; (4) add the fragment to the container, which is generally implemented by using the replace() method and needs to pass in the id of the container and the fragment to be added Slice instance; (5) commit the transaction and call the commit() method to complete.

2, Source code:

1. Project address

https://github.com/ruigege66/Android/tree/master/UIBestPractice

2.CSDN: https://blog.csdn.net/weixin_44630050

3. Blog Park: https://www.cnblogs.com/ruige0000/

4. welcome to WeChat official account: Fourier transform, official account number, only for learning communication, background reply, "gift package", get big data learning materials.

 

Posted by nikifi on Wed, 29 Apr 2020 22:23:08 -0700