Android之点击按钮切换不同的Fragment

截图:

主要代码:

1.布局文件:

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

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="108dp"
        android:background="@drawable/bg_message_title">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="30dp">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerHorizontal="true"
                android:fitsSystemWindows="true"
                android:text="日程"
                android:textColor="@color/white"
                android:textSize="16sp" />

            <ImageView
                android:id="@+id/iv_menu"
                android:layout_width="18dp"
                android:layout_height="20dp"
                android:layout_alignParentRight="true"
                android:layout_marginRight="10dp"
                android:src="@drawable/iv_menu" />
        </RelativeLayout>

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_marginBottom="0.1dp">

            <RelativeLayout
                android:id="@+id/rl_richeng"
                android:layout_width="85dp"
                android:layout_height="38dp"
                android:layout_marginLeft="45dp"
                android:background="@drawable/shape_btn_bg">

                <TextView
                    android:layout_width="85dp"
                    android:layout_height="38dp"
                    android:gravity="center"
                    android:text="日程"
                    android:textColor="@color/white"
                    android:textSize="14sp" />

                <TextView
                    android:id="@+id/tv_tip_richeng"
                    android:layout_width="match_parent"
                    android:layout_height="6dp"
                    android:layout_alignParentBottom="true"
                    android:background="@color/white" />
            </RelativeLayout>

            <RelativeLayout
                android:id="@+id/rl_renwu"
                android:layout_width="85dp"
                android:layout_height="38dp"
                android:layout_centerInParent="true"
                android:layout_marginLeft="45dp"
                android:background="@drawable/shape_btn_bg">

                <TextView
                    android:layout_width="85dp"
                    android:layout_height="38dp"
                    android:gravity="center"
                    android:text="任务"
                    android:textColor="@color/white"
                    android:textSize="14sp" />

                <TextView
                    android:id="@+id/tv_tip_renwu"
                    android:layout_width="match_parent"
                    android:layout_height="6dp"
                    android:layout_alignParentBottom="true"
                    android:background="@color/white" />
            </RelativeLayout>

            <RelativeLayout
                android:id="@+id/rl_huiyi"
                android:layout_width="85dp"
                android:layout_height="38dp"
                android:layout_alignParentRight="true"
                android:layout_marginLeft="45dp"
                android:layout_marginRight="45dp"
                android:background="@drawable/shape_btn_bg">

                <TextView
                    android:layout_width="85dp"
                    android:layout_height="38dp"
                    android:gravity="center"
                    android:text="会议"
                    android:textColor="@color/white"
                    android:textSize="14sp" />

                <TextView
                    android:id="@+id/tv_tip_huiyi"
                    android:layout_width="match_parent"
                    android:layout_height="6dp"
                    android:layout_alignParentBottom="true"
                    android:background="@color/white" />
            </RelativeLayout>
        </RelativeLayout>
    </RelativeLayout>

    <FrameLayout
        android:id="@+id/fl"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>

2.java代码:

package com.fb.panoa.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.fb.panoa.R;
import com.fb.panoa.view.RCPopWindow;

/**
 * 日程
 */
public class MainTwoFragment extends Fragment implements View.OnClickListener {

    private ImageView iv_menu;
    private RelativeLayout rl_richeng, rl_renwu, rl_huiyi;
    FragmentManager fm;
    private RiChengFragment riChengFragment;
    private RenWuFragment renWuFragment;
    private HuiYiFragment huiYiFragment;
    private TextView tv_tip_richeng, tv_tip_renwu, tv_tip_huiyi;

    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_two, container, false);
        iv_menu = view.findViewById(R.id.iv_menu);//右上角菜单
        rl_richeng = view.findViewById(R.id.rl_richeng);//日程
        rl_renwu = view.findViewById(R.id.rl_renwu);//任务
        rl_huiyi = view.findViewById(R.id.rl_huiyi);//会议
        tv_tip_richeng = view.findViewById(R.id.tv_tip_richeng);//日程导航条
        tv_tip_renwu = view.findViewById(R.id.tv_tip_renwu);//任务导航条
        tv_tip_huiyi = view.findViewById(R.id.tv_tip_huiyi);//会议导航条
        iv_menu.setOnClickListener(this);
        rl_richeng.setOnClickListener(this);
        rl_renwu.setOnClickListener(this);
        rl_huiyi.setOnClickListener(this);
        fm = getChildFragmentManager();
        setTabSelection(0);//进入页面加载第一个日程页
        rl_renwu.setBackgroundResource(R.color.green);//隐藏任务背景
        tv_tip_renwu.setVisibility(View.GONE);//隐藏任务导航条
        rl_huiyi.setBackgroundResource(R.color.green);//隐藏会议背景
        tv_tip_huiyi.setVisibility(View.GONE);//隐藏会议导航条
        return view;
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_menu://点击加号弹出菜单栏
                new RCPopWindow(getActivity()).showAtBottom(iv_menu);
                break;
            case R.id.rl_richeng:
                setTabSelection(0);
                rl_richeng.setBackgroundResource(R.drawable.shape_btn_bg);//显示日程背景
                tv_tip_richeng.setVisibility(View.VISIBLE);//显示日程导航条
                rl_renwu.setBackgroundResource(R.color.green);//隐藏任务背景
                tv_tip_renwu.setVisibility(View.GONE);//隐藏任务导航条
                rl_huiyi.setBackgroundResource(R.color.green);//隐藏会议背景
                tv_tip_huiyi.setVisibility(View.GONE);//影藏会议导航条
                break;
            case R.id.rl_renwu:
                setTabSelection(1);
                rl_richeng.setBackgroundResource(R.color.green);//隐藏日程背景
                tv_tip_richeng.setVisibility(View.GONE);//隐藏日程导航条
                rl_renwu.setBackgroundResource(R.drawable.shape_btn_bg);//显示任务背景
                tv_tip_renwu.setVisibility(View.VISIBLE);//显示任务导航条
                rl_huiyi.setBackgroundResource(R.color.green);//隐藏会议背景
                tv_tip_huiyi.setVisibility(View.GONE);//隐藏会议导航条
                break;
            case R.id.rl_huiyi:
                setTabSelection(2);
                rl_richeng.setBackgroundResource(R.color.green);//隐藏日程背景
                tv_tip_richeng.setVisibility(View.GONE);//隐藏日程导航条
                rl_renwu.setBackgroundResource(R.color.green);//隐藏任务背景
                tv_tip_renwu.setVisibility(View.GONE);//隐藏任务导航条
                rl_huiyi.setBackgroundResource(R.drawable.shape_btn_bg);//显示会议背景
                tv_tip_huiyi.setVisibility(View.VISIBLE);//显示任务导航条
                break;
            default:
                break;
        }
    }

    private void setTabSelection(int index) {
        FragmentTransaction ft = fm.beginTransaction();
        hideFragment(ft);
        switch (index) {
            case 0:
                if (riChengFragment == null) {
                    riChengFragment = new RiChengFragment();
                    ft.add(R.id.fl, riChengFragment);
                } else {
                    ft.show(riChengFragment);
                }
                break;
            case 1:
                if (renWuFragment == null) {
                    renWuFragment = new RenWuFragment();
                    ft.add(R.id.fl, renWuFragment);
                }
                ft.show(renWuFragment);
                break;
            case 2:
                if (huiYiFragment == null) {
                    huiYiFragment = new HuiYiFragment();
                    ft.add(R.id.fl, huiYiFragment);
                }
                ft.show(huiYiFragment);
                break;
        }
        ft.commit();
    }

    //用于隐藏fragment
    private void hideFragment(FragmentTransaction ft) {
        if (riChengFragment != null) {
            ft.hide(riChengFragment);
        }
        if (renWuFragment != null) {
            ft.hide(renWuFragment);
        }
        if (huiYiFragment != null) {
            ft.hide(huiYiFragment);
        }
    }

}

3.各个fragment布局和代码,这里给出一个,其他两个类似

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/white"
    android:gravity="center"
    android:orientation="vertical">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:text="日程" />
</LinearLayout>
package com.fb.panoa.fragment;

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;

import com.fb.panoa.R;

/**
 *日程
 */

public class RiChengFragment extends Fragment{

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


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

举儿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值