Android中Activity动态使用Fragment

**

Android中动态使用Fragment

**
动态使用Fragment就是向Fragment布局容器中动态添加、替换、移除、隐藏、显示Fragment。
注意:布局容器一定要选择合适,如果选择的布局容器不能包含Fragment,这样子一般编辑器不会报错,但是虚拟机或者测试设备上是无法显示出我们想要的界面。

Activity中显示Fragment(比如实现微信界面)

在Activity中显示Fragment,实现微信这样的界面时,我们需要一个Activity,多个Fragment,在本例中,我们使用4个Fragment。
首先创建一个Activity和4个Fragment(first、second、third、forth),并在layout文件夹中创建4个XML文件(first、second、third、forth),将这4个Fragment与4个XML文件一一链接,代码如下:

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;
    }
}

我们可以在创建的XML中根据项目需求设计自己需要的界面,此处不再赘述。

Fragment在Activity中的切换、移除、隐藏

首先我们需要在Activity对应的XML文件中写好界面文件,用来显示Fragment的为RelativeLayout(id=“FragmentContainer”)。接下来写好用以点击显示不同Fragment的栏目。供参考代码如下:

<?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>

写好的导航栏显示如下图所示:
在这里插入图片描述
接下来写好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;
        }
    }
}

至此,我们想要的微信类型界面已经完成,实现效果如下所示:
在这里插入图片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章