Fragment之静态加载

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

主要知识点:

1.Fragment可以作为Activity界面的一部分组成出现
2.可以在一个Activity中同时出现多个Fragment,并且一个Fragment也可以在多个Activity中使用
3.在Activity运行过程中,可以添加、移除或替换Fragment
4.Fragment可以响应自己的输入事件,并且有自己的生命周期,他的生命周期会受宿主Activity的生命周期的影响

5.Fragment相对于Acitivty来说,Activity就是房子,Fragment就是房子里面的房间。

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"

   >

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

以上的底部导航栏,使用了选择器,点击之后有灰色背景

android:background="@drawable/radio_pressed"
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" android:state_pressed="true"></item>
    <item android:drawable="@color/white"></item>
</selector>

-------------------------------华丽分割线------------------------------------------

PS:一些选中状态:

android:state_pressed=["true" | "false"]//是否触摸

android:state_focused=["true" | "false"]//是否获取到焦点

android:state_hovered=["true" | "false"]//光标是否经过

android:state_selected=["true" | "false"]//是否选中

android:state_checkable=["true" | "false"]//是否可勾选

android:state_checked=["true" | "false"]//是否勾选

android:state_enabled=["true" | "false"]//是否可用

android:state_activated=["true" | "false"]//是否激活

android:state_window_focused=["true" | "false"] />//所在窗口是否获取焦点

-------------------------------华丽分割线------------------------------------------

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

MainAcitivity.java

package com.rui.fragmentdemo;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.RadioGroup;

public class MainActivity extends Activity {
    private RadioGroup radiogroup;//声明RadioGroup
    @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:
                        break;
                    case R.id.three:
                        break;
                    case R.id.four:
                        break;
                }
            }
        });
    }
}

然后创建一个布局文件,给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.TextView;

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

public class MyFragment  extends Fragment{

    @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 tv1 = view.findViewById(R.id.tv1);
        tv1.setText("静态加载Fargment");
        return view;
    }
}

然后创建一个布局xml,添加Fragment,其中android:name="com.rui.fragmentdemo.MyFragment"

加载了刚刚继承了Fragment的类

activity_main2.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">

    <fragment
        android:id="@+id/myfragment"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:name="com.rui.fragmentdemo.MyFragment"/>

</LinearLayout>

 创建一个类文件继承Activity,加载布局(有fragment),通过按按钮,改变tv1的文本,说明了Activity中的Fragment里面的组件是共享的!

Mainactivity2.java

package com.rui.fragmentdemo;

import android.app.Activity;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;


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

public class MainActivity2 extends Activity{
    private  TextView tv1;//声明TextView
    private  Button bt1;//生命Button

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        //实例化tv1,bt1
        tv1 = (TextView)findViewById(R.id.tv1);
        bt1 = (Button)findViewById(R.id.bt1);
        //按钮监听事件,并覆盖监听方法
        bt1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //改变Fragment的tv1的值
                tv1.setText("改变了!");
            }
        });
    }
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值