Android Activity in dynamic use Fragment

**

Android dynamically use Fragment

**
dynamically using Fragment Fragment layout is dynamically added to the container, replace, remove, hide, show Fragment.
Note: Be sure to choose the right layout container, if you choose a layout container can not Fragment, general editor like this does not complain, but on a virtual machine or test equipment is unable to show that we want interface.

Fragment Activity display (implement such micro-channel interface)

Fragment displayed in an Activity, When such micro-channel interface, we need a Activity, Fragment plurality, in this example, we used 4 Fragment.
First, create an Activity and 4 Fragment (first, second, third, forth ) , and create four XML file (first, second, third, forth ) in the layout folder, these four Fragment with a four XML files a link, the code is as follows:

package com.example.电脑用户名.项目名.main;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.电脑用户名.项目名.R;

public class first extends Fragment {
    public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState){
        View v=inflater.inflate(R.layout.first,container,false);
        return v;
    }
}

We can design the interface according to their own needs project requirements in the XML created, not repeat them here.

Fragment of switching Activity, removed, hidden

First, we need to write the interface files Activity corresponding XML file to display Fragment of RelativeLayout (id = "FragmentContainer"). Click Next to display different Fragment written columns. For reference code is as follows:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    xmlns:android="http://schemas.android.com/apk/res/android">
    <RelativeLayout
        android:id="@+id/FragmentContainer"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@id/buttoncontainer">
    </RelativeLayout>
    <LinearLayout
        android:id="@+id/buttoncontainer"
        android:layout_width="match_parent"
        android:layout_height="80dp"
        android:orientation="horizontal"
        android:layout_alignParentBottom="true"
        android:background="@color/white"
        android:gravity="bottom">
        <LinearLayout
            android:id="@+id/first1"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:orientation="vertical"
            style="@style/image">
            <ImageView
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:paddingTop="10dp"
                android:src="@drawable/shouye" />
            <TextView
                style="@style/text"
                android:layout_weight="1"
                android:text="@string/shouye"
                />
        </LinearLayout>
        <LinearLayout
            android:id="@+id/second2"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:orientation="vertical"
            style="@style/image">
            <ImageView
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:paddingTop="10dp"
                android:src="@drawable/chaxun" />
            <TextView
                style="@style/text"
                android:layout_weight="1"
                android:text="@string/chaxun"
                />
        </LinearLayout>
        <LinearLayout
            android:id="@+id/third3"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:orientation="vertical"
            style="@style/image">
            <ImageView
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:paddingTop="10dp"
                android:src="@drawable/shangchan" />
            <TextView
                style="@style/text"
                android:layout_weight="1"
                android:text="@string/shangchuan"
                />
        </LinearLayout>
        <LinearLayout
            android:id="@+id/forth4"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:orientation="vertical"
            style="@style/image">
            <ImageView
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:paddingTop="10dp"
                android:src="@drawable/wode" />
            <TextView
                style="@style/text"
                android:layout_weight="1"
                android:text="@string/wode"
                />
        </LinearLayout>

    </LinearLayout>
</RelativeLayout>

Written navigation bar shown below:
Here Insert Picture Description
Next, to write logic code in an Activity:
Activity.java

package com.example.a80496.newproject;

import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;

import com.example.电脑用户名.项目名.main.first;
import com.example.电脑用户名.项目名.main.second;
import com.example.电脑用户名.项目名.main.third;
import com.example.电脑用户名.项目名.main.forth;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private LinearLayout d1,d2,d3,d4;
    private FragmentTransaction fragmentTransaction;
    private first f1;
    private second f2;
    private third f3;
    private forth f4;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        d1=findViewById(R.id.first1);
        d2=findViewById(R.id.second2);
        d3=findViewById(R.id.third3);
        d4=findViewById(R.id.forth4);
        d1	.setOnClickListener(this);
        d2.setOnClickListener(this);
        d3.setOnClickListener(this);
        d4.setOnClickListener(this);
        setTab(4);
    }

    private void hidefragment(FragmentTransaction fragment){
        if(f1!=null)
        {
            fragment.hide(f1);
        }
        if(f2!=null)
        {
            fragment.hide(f2);
        }
        if(f3!=null)
        {
            fragment.hide(f3);
        }
        if(f4!=null)
        {
            fragment.hide(f4);
        }
    }

    private void setTab(int i){
        fragmentTransaction=getSupportFragmentManager().beginTransaction();
        hidefragment(fragmentTransaction);
        d1.setSelected(false);
        d2.setSelected(false);
        d3.setSelected(false);
        d4.setSelected(false);
        switch (i){
            case 1:
                d1.setSelected(true);
                if (f1==null){
                    f1=new first();
                    fragmentTransaction.add( R.id.FragmentContainer,f1);
                }else {
                    fragmentTransaction.show(f1);
                }
                break;
            case 2:
                second.setSelected(true);
                if (f2==null){
                    f2=new second();
                    fragmentTransaction.add( R.id.FragmentContainer,f2);
                }else {
                    fragmentTransaction.show(f2);
                }
                break;
            case 3:
                third.setSelected(true);
                if (f3==null){
                    f3=new third();
                    fragmentTransaction.add( R.id.FragmentContainer,f3);
                }else {
                    fragmentTransaction.show(f3);
                }
                break;
            case 4:
                fourth.setSelected(true);
                if (f4==null){
                    f4=new forth();
                    fragmentTransaction.add( R.id.FragmentContainer,f4);
                }else {
                    fragmentTransaction.show(f4);
                }
                break;
        }
        fragmentTransaction.commitAllowingStateLoss();

    }

    @Override
    public void onClick(View view) {
        switch(view.getId()){
            case R.id.first1:
                setTab(1);
                break;
            case R.id.second2:
                setTab(2);
                break;
            case R.id.third3:
                setTab(3);
                break;
            case R.id.forth4:
                setTab(4);
                break;
            default:
                break;
        }
    }
}

At this point, we want the type of micro-channel interface has been completed, to achieve the effect as follows:
Here Insert Picture Description

Released four original articles · won praise 11 · views 1264

Guess you like

Origin blog.csdn.net/weixin_45457983/article/details/104277537