Android之Fragment(动态使用)

版权声明:《==study hard and make progress every day==》 https://blog.csdn.net/qq_38225558/article/details/79853682

效果:

代码:

activity_info主布局:

<?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:name  连接java TitleFragment里面的代码
                      java里面的代码连接fragment_title页面-->

    <!-- 把Fragment当作普通控件使用,这里没啥作用,就是作为一个标题栏,不用Fragment,直接写布局一样的 -->
    <fragment
        android:id="@+id/fragment_title"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:name="com.ied.fragmentproject.TitleFragment"/>
    <!-- 用一个帧布局来占一个位置,目的是给fragment用 -->
    <FrameLayout
        android:id="@+id/fl_content"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1">
    </FrameLayout>
    <!-- 底部三个按钮,我们用RadioGroup来实现 -->
    <include
        android:id="@+id/id_bottombar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        layout="@layout/fragment_bottombar"></include>



</LinearLayout>

MainActivity:

package com.ied.fragmentproject;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.RadioButton;
import android.widget.RadioGroup;

public class MainActivity extends FragmentActivity implements OnClickListener {
    private RadioButton rb1;
    private RadioButton rb2;
    private RadioButton rb3;

    private RadioGroup radioGroup;

    private Fragment f1,f2,f3;
    private FragmentManager manager;
    private FragmentTransaction transaction;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_info);
        /**
         * 拿到事务管理器并开启事务
         */
        manager = getSupportFragmentManager();//获取FragmentManage的方式
        transaction = manager.beginTransaction();//开启一个事务

        // 初始化控件
        rb1 = (RadioButton) findViewById(R.id.rb_1);
        rb2 = (RadioButton) findViewById(R.id.rb_2);
        rb3 = (RadioButton) findViewById(R.id.rb_3);
        radioGroup = (RadioGroup) findViewById(R.id.rg_bar);

        //为三个按钮添加监听
        rb1.setOnClickListener(this);
        rb2.setOnClickListener(this);
        rb3.setOnClickListener(this);

        //启动默认选中第一个
        radioGroup.check(R.id.rb_1);
        f1 = new FirstFragment();
        transaction.replace(R.id.fl_content, f1);
        transaction.commit();
    }

    @Override
    public void onClick(View v) {
        manager = getSupportFragmentManager();
        transaction = manager.beginTransaction();

        switch (v.getId()) {
            case R.id.rb_1 :
                /**
                 * 为了防止重叠,需要点击之前先移除其他Fragment
                 */
                hideFragment(transaction);
                f1 = new FirstFragment();
                transaction.replace(R.id.fl_content, f1);
                transaction.commit();
                break;
            case R.id.rb_2 :
                hideFragment(transaction);
                f2 = new SecondFragment();
                transaction.replace(R.id.fl_content, f2);
                transaction.commit();
                break;
            case R.id.rb_3 :
                hideFragment(transaction);
                f3 = new ThirdFragment();
                transaction.replace(R.id.fl_content, f3);
                transaction.commit();
                break;
            default :
                break;
        }
    }
    /*
    * 去除(隐藏)所有的Fragment
    * */
    private void hideFragment(FragmentTransaction transaction) {
        if (f1 != null) {
            //transaction.hide(f1);隐藏方法也可以实现同样的效果,不过我一般使用去除
            transaction.remove(f1);
        }
        if (f2 != null) {
            //transaction.hide(f2);
            transaction.remove(f2);
        }
        if (f3 != null) {
            //transaction.hide(f3);
            transaction.remove(f3);
        }

    }

}
然后--->
fragment_title布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#00cef7">
    <ImageView
        android:id="@+id/iv_back"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:src="@mipmap/back"/>
    <TextView
        android:id="@+id/tv_title"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:text="标题"
        android:textSize="20sp"
        android:textStyle="bold"/>

</LinearLayout>

TitleFragment:

package com.ied.fragmentproject;

import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.Toast;

/**
 * Created by DELL on 2018/4/6.
 */

public class TitleFragment extends Fragment {
    private ImageView ivBack;

    //当创建fragment视图时调用
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        //加载片段的视图
        /**
         * 参数说明:
         * 参数1:layout资源的ID
         * 参数2:存放fragment的layout的ViewGroup
         * 参数3:布尔型数据,表示是否在创建fragment的layout期间,把layout附加到container上(因为系统已把layout插入到
         * container中了,所以值为false,如果为true会导致在最终的layout中创建多余的viewgroup).
         */
        View view = inflater.inflate(R.layout.fragment_title,container,false);
        //对视图中的控件进行初始化
        ivBack = view.findViewById(R.id.iv_back);
        ivBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //通过getActivity()获取上下文环境对象
                Toast.makeText(getActivity(),"您点击了fragment_title里的ImageView按钮!!",Toast.LENGTH_SHORT).show();
            }
        });
        return view;
    }


}

三个底部按钮布局fragment_bottombar:

<?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:paddingLeft="0dp"加这个是为了让内容能够剧中 -->
    <RadioGroup
        android:id="@+id/rg_bar"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal">
        <RadioButton
            android:id="@+id/rb_1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@drawable/change_bar"
            android:button="@null"
            android:drawableTop="@android:drawable/alert_light_frame"
            android:gravity="center"
            android:paddingLeft="0dp"
            android:text="first" />

        <RadioButton
            android:id="@+id/rb_2"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@drawable/change_bar"
            android:button="@null"
            android:drawableTop="@android:drawable/alert_light_frame"
            android:gravity="center"
            android:paddingLeft="0dp"
            android:text="second" />

        <RadioButton
            android:id="@+id/rb_3"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@drawable/change_bar"
            android:button="@null"
            android:drawableTop="@android:drawable/alert_light_frame"
            android:gravity="center"
            android:paddingLeft="0dp"
            android:text="third" />
    </RadioGroup>

</LinearLayout>

第一个页面first_fragment.xml:

<?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">
    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:text="第一个fragment"
        android:textSize="20sp"/>

</LinearLayout>

FirstFragment.java:

package com.ied.fragmentproject;


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

/**
 * Created by DELL on 2018/4/8.
 */

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

同理第二个页面,第三个页面如上......

源代码下载

 密码:qev3

猜你喜欢

转载自blog.csdn.net/qq_38225558/article/details/79853682