Android fragment for static loading

Keywords: Android Fragment Java xml

The interface after static loading is as follows. The two fragments are located on the left and right of an activity respectively:

 

 

There is a Fragment on the left and a Fragment on the right, which just fills the whole activity. There can be multiple fragments in an activity. The meaning of fragments is that the interface can be divided into several interfaces under the same UI interface, and the status can be updated separately. If there are no fragments, it is impossible for you to "jump" the activity in a certain area by yourself. Therefore, we can introduce fragments, so that we can The region performs the jump of fragments separately. Fragments are needed when using the title bar at the bottom to switch the UI of the home page. Therefore, fragments are widely used in Android development. This blog will show you how to achieve static loading of fragments. In addition to static loading of fragments, it also has a way of dynamic loading of fragments. The two different ways of understanding and referencing can bring the power of fragments into play Maximum. The following is the code. The first is the code in the main activity. The main activity must inherit the Fragment class to implement fragments:

I. MainActivity.java

import androidx.fragment.app.FragmentActivity;
import android.os.Bundle;
import android.util.Log;

public class MainActivity extends FragmentActivity {

    public MainActivity() {
        Log.e("TAG", "MainActivity()..");
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        Log.e("TAG", "MainActivity onCreate()..");
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}

Then we create fragments. There are two fragments in the above UI interface. So we create two fragments in succession:

2. MyFragment.java

In this fragment, we use Java to directly introduce TextView control. Of course, it can also be used in the xml file corresponding to this fragment, which is equivalent to each other and relatively simple.

import android.graphics.Color;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class MyFragment extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        //Load layout to get View Object and return

        //Create a view object, Set data and return
        TextView  textView = new TextView(getActivity());
        textView.setText("This is the first piece");
        textView.setBackgroundColor(Color.RED);

        return textView;
    }
}

3. MyFragment2.java

import android.graphics.Color;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;



public class MyFragment2 extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        //Load layout to get View Object and return

        //Create a view object, Set data and return
        TextView  textView = new TextView(getActivity());
        textView.setText("This is the second piece");
        textView.setBackgroundColor(Color.RED);

        return textView;
    }
}

Later, in the UI interface of our main activity, the code will be modified as follows:

IV. activity main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:orientation="horizontal"
    tools:context=".MainActivity">
    <fragment
        android:name="com.example.fragment.MyFragment"
        android:id="@+id/myfragment_1"
        android:layout_weight="1"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        />
    <fragment
        android:name="com.example.fragment.MyFragment2"
        android:id="@+id/myfragment_2"
        android:layout_weight="1"
        android:layout_width="0dp"
        android:layout_height="match_parent"

        />
</LinearLayout>

In this way, we can introduce fragment to our main activity, run Android project and achieve success!!

Posted by iyia12co on Thu, 28 Nov 2019 04:01:54 -0800