Fragment之动态加载

此文,仅做为个人学习Android,记录成长以及方便复习!

1.配置一下界面,使用RadioGroup实现底部导航栏,其中的4个按钮,本次是只使用动态加载一个按钮!

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"

   >

    <LinearLayout
        android:id="@+id/linearlayout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"></LinearLayout>

    <RadioGroup
        android:id="@+id/radiogrop"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:orientation="horizontal">
        
            <RadioButton
                android:id="@+id/one"
                android:background="@drawable/radio_pressed"
                android:drawableTop="@drawable/ic_launcher_foreground"
                android:gravity="center_horizontal"
                android:text="静态加载"
                android:button="@null"
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="wrap_content"/>
            <RadioButton
                android:id="@+id/two"
                android:background="@drawable/radio_pressed"
                android:drawableTop="@drawable/ic_launcher_foreground"
                android:gravity="center_horizontal"
                android:text="动态加载"
                android:button="@null"
                android:layout_width="0dp"
                android:layout_weight="1"
                    android:layout_height="wrap_content"/>
            <RadioButton
                android:id="@+id/three"
                android:background="@drawable/radio_pressed"
                android:drawableTop="@drawable/ic_launcher_foreground"
                android:gravity="center_horizontal"
                android:text="生命周期"
                android:button="@null"
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="wrap_content"/>
            <RadioButton
                android:id="@+id/four"
                android:background="@drawable/radio_pressed"
                android:drawableTop="@drawable/ic_launcher_foreground"
                android:gravity="center_horizontal"
                android:text="传值通讯"
                android:button="@null"
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="wrap_content"/>
    </RadioGroup>
</RelativeLayout>

接下来是底部导航栏的选择器配置文件

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

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@color/garay" android:state_checked="true"></item>

    <item android:drawable="@color/white"></item>
</selector>

然后创建一个布局文件,给Fragment加载使用的

fragment.xml

<?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">
    <TextView
        android:id="@+id/tv1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <Button
        android:id="@+id/bt1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="改变" />
</LinearLayout>

创建今天的主角文件,先创建一个类继承Fragment并实现其方法OncreateView,通过inflater.inflate将布局转换成View

通过View.findViewById.xxxxx就可以获取到Fragment的组件

MyFragment.java

package com.rui.fragmentdemo;

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.Button;
import android.widget.TextView;

/**
 * Created by qqazl001 on 2018/3/19.
 */

public class MyFragment  extends Fragment{
    private Button bt1;//声明按钮
    private TextView tv1;//声明TextView
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        //布局文件转成View
        //参数1:Fragment需要加载的布局文件
        //参数2:加载layout的父ViewGroup
        //参数1:是否返回父ViewGroup对象,false为不
        View view =  inflater.inflate(R.layout.fragment,container,false);
        //实例化TextView 设置 TextView
        tv1 = view.findViewById(R.id.tv1);
        tv1.setText("动态加载Fargment");
        //实例化获取按钮
        bt1 = view.findViewById(R.id.bt1);
        //设置监听事件,覆盖监听方法
        bt1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                tv1.setText("其实就是试试按钮!");
            }
        });
        return view;
    }
}

接下来是Activity文件,实现RadioGroup的按钮监听时间,监听来着two按钮的事件

MainAcitivity.java

package com.rui.fragmentdemo;

import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {
    private RadioGroup radiogroup;//声明RadioGroup
    private TextView t1;//声明TextView
    private Button b1;//声明Buttion
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //实例化RadioGroup
        radiogroup = (RadioGroup)findViewById(R.id.radiogrop);
        //使用RadioGroup监听事件,使用监听方法OnCheckedChangeListener
        radiogroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                switch (i){
                    case R.id.one:
                        //跳转到MainAcitvity2
                        //Intent  intent = new Intent(MainActivity.this,MainActivity2.class);
                        //startActivity(intent);
                        break;
                    case R.id.two:
                        //每一个事务都是同时执行一套变化,可以在一个事务中设置所有想执行的变化,
                        // 包括add()、remove()、replace(),然后提交给Activity,提交必须调用commit()方法。

                        //实例化fragment
                        MyFragment fragment = new MyFragment();
                        //获取事物管理者
                        FragmentManager fragmentManager = getFragmentManager();
                        //通过事务管理者,开启一个事物
                        FragmentTransaction fragmentTransaction=  fragmentManager.beginTransaction();
                        //添加frament
                        // 参数1 是添加到哪个布局
                        //参数2 继承了Fragment的类
                        fragmentTransaction.add(R.id.linearlayout,fragment);
                        //返回上个Fragment.或者指定的Fragment
                        fragmentTransaction.addToBackStack(null);
                        //提交事务
                        fragmentTransaction.commit();
                        break;
                    case R.id.three:
                        break;
                    case R.id.four:
                        break;
                }
            }
        });
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值