仿微信 Fragment 动态加载

1.先看效果
这里写图片描述

2,关于底部菜单可以到这里找
仿微信 RadioButton 自定义点击效果

3.项目结构图
这里写图片描述

4.下面开始贴出代码
MainActivity.java

public class MainActivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener {

    private RadioGroup radioGroup;
    private Address address;
    private Find find;
    private Setting setting;
    private Talk talk;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);

        radioGroup = (RadioGroup) findViewById(R.id.radiogroup);
        radioGroup.setOnCheckedChangeListener(MainActivity.this);
    }
    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {

        FragmentManager fm = getFragmentManager();
        // 开启Fragment事务
        FragmentTransaction transaction = fm.beginTransaction();

        switch (checkedId) {
            case R.id.first:
                if (talk == null) {
                    talk = new Talk();
                }
                // 使用当前Fragment的布局替代id.fragment的控件
                transaction.replace(R.id.fragment, talk);
                break;
            case R.id.two:
                if (address == null) {
                    address = new Address();
                }      
                transaction.replace(R.id.fragment, address);
                break;
            case R.id.three:
                if (find == null) {
                    find = new Find();
                }         
                transaction.replace(R.id.fragment, find);
                break;
            case R.id.four:
                if (setting == null) {
                    setting = new Setting();
                }         
                transaction.replace(R.id.fragment, setting);
                break;
        }
        // 事务提交
        transaction.commit();
    }

}

activity_main.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"
    android:orientation="vertical">

    <FrameLayout
        android:id="@+id/fragment"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        >

    </FrameLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:background="@drawable/bottom_bar">

        <RadioGroup
            android:id="@+id/radiogroup"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center_horizontal"
            android:orientation="horizontal">

            <RadioButton
                android:id="@+id/first"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:button="@null"
                android:drawableTop="@drawable/radio_image_talk"
                android:gravity="center_horizontal"
                android:text="聊天"
                android:textColor="#eee" />

            <RadioButton
                android:id="@+id/two"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:button="@null"
                android:drawableTop="@drawable/radio_image_address"
                android:gravity="center_horizontal"
                android:text="联系人"
                android:textColor="#eee" />

            <RadioButton
                android:id="@+id/three"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:button="@null"
                android:drawableTop="@drawable/radio_image_find"
                android:gravity="center_horizontal"
                android:text="朋友圈"
                android:textColor="#eee" />

            <RadioButton
                android:id="@+id/four"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:button="@null"
                android:drawableTop="@drawable/radio_image_setting"
                android:gravity="center_horizontal"
                android:text="设置"
                android:textColor="#eee" />
        </RadioGroup>


    </LinearLayout>
</LinearLayout>

Talk.java

public class Talk extends Fragment{

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {


        return inflater.inflate(R.layout.activity_talk,container,false);
    }
}

activity_talk.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"
    >
    <TextView
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true"
        android:textSize="30sp"
        android:id="@+id/address"
        android:text="对话"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</RelativeLayout>

Addres.java

public class Address extends Fragment{

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {


        return inflater.inflate(R.layout.activity_address,container,false);
    }
}

activity_address.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"
    >
    <TextView
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true"
        android:textSize="30sp"
        android:id="@+id/address"
        android:text="联系人"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</RelativeLayout>

其他的Find,Setting修改一下就可以了就不贴了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值