Fragment学习3--底部tab布局

这里写图片描述

我的博客
Fragment学习2–简单的添加Fragment
https://blog.youkuaiyun.com/qq_26296197/article/details/81146022

MessageFragment

public class MessageFragment extends Fragment {

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View messageLayout = inflater.inflate(R.layout.message_layout,
                container, false);
        return messageLayout;
    }

}

ContactsFragment

public class ContactsFragment extends Fragment {

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

}

其他fragment…

Activity

/**
 * 项目的主Activity,所有的Fragment都嵌入在这里。
 *
 * 用到 add 和hide
 *
 * @author zq
 *
 *
 */
public class TabFragmentActivityTest extends BaseActivity implements OnClickListener {

    /**
     * 用于展示消息的Fragment
     */
    private MessageFragment messageFragment;

    /**
     * 用于展示联系人的Fragment
     */
    private ContactsFragment contactsFragment;

    /**
     * 用于展示动态的Fragment
     */
    private NewsFragment newsFragment;

    /**
     * 用于展示设置的Fragment
     */
    private SettingFragment settingFragment;

    /**
     * 消息界面布局
     */
    private View messageLayout;

    /**
     * 联系人界面布局
     */
    private View contactsLayout;

    /**
     * 动态界面布局
     */
    private View newsLayout;

    /**
     * 设置界面布局
     */
    private View settingLayout;

    /**
     * 在Tab布局上显示消息图标的控件
     */
    private ImageView messageImage;

    /**
     * 在Tab布局上显示联系人图标的控件
     */
    private ImageView contactsImage;

    /**
     * 在Tab布局上显示动态图标的控件
     */
    private ImageView newsImage;

    /**
     * 在Tab布局上显示设置图标的控件
     */
    private ImageView settingImage;

    /**
     * 在Tab布局上显示消息标题的控件
     */
    private TextView messageText;

    /**
     * 在Tab布局上显示联系人标题的控件
     */
    private TextView contactsText;

    /**
     * 在Tab布局上显示动态标题的控件
     */
    private TextView newsText;

    /**
     * 在Tab布局上显示设置标题的控件
     */
    private TextView settingText;

    /**
     * 用于对Fragment进行管理
     */
    private FragmentManager fragmentManager;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_tab_fragment);
        // 初始化布局元素
        initViews();
        fragmentManager = getFragmentManager();
        // 第一次启动时选中第0个tab
        setTabSelection(0);
    }

    /**
     * 在这里获取到每个需要用到的控件的实例,并给它们设置好必要的点击事件。
     */
    private void initViews() {
        messageLayout = findViewById(R.id.message_layout);
        contactsLayout = findViewById(R.id.contacts_layout);
        newsLayout = findViewById(R.id.news_layout);
        settingLayout = findViewById(R.id.setting_layout);
        messageImage = (ImageView) findViewById(R.id.message_image);
        contactsImage = (ImageView) findViewById(R.id.contacts_image);
        newsImage = (ImageView) findViewById(R.id.news_image);
        settingImage = (ImageView) findViewById(R.id.setting_image);
        messageText = (TextView) findViewById(R.id.message_text);
        contactsText = (TextView) findViewById(R.id.contacts_text);
        newsText = (TextView) findViewById(R.id.news_text);
        settingText = (TextView) findViewById(R.id.setting_text);
        messageLayout.setOnClickListener(this);
        contactsLayout.setOnClickListener(this);
        newsLayout.setOnClickListener(this);
        settingLayout.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.message_layout:
            // 当点击了消息tab时,选中第1个tab
            setTabSelection(0);
            break;
        case R.id.contacts_layout:
            // 当点击了联系人tab时,选中第2个tab
            setTabSelection(1);
            break;
        case R.id.news_layout:
            // 当点击了动态tab时,选中第3个tab
            setTabSelection(2);
            break;
        case R.id.setting_layout:
            // 当点击了设置tab时,选中第4个tab
            setTabSelection(3);
            break;
        default:
            break;
        }
    }

    /**
     * 根据传入的index参数来设置选中的tab页。
     * 
     * @param index
     *  每个tab页对应的下标。0表示消息,1表示联系人,2表示动态,3表示设置。
     */
    private void setTabSelection(int index) {
        // 每次选中之前先清楚掉上次的选中状态
        clearSelection();
        // 开启一个Fragment事务
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        // 先隐藏掉所有的Fragment,以防止有多个Fragment显示在界面上的情况
        hideFragments(transaction);
        switch (index) {
        case 0:
            // 当点击了消息tab时,改变控件的图片和文字颜色
            messageImage.setImageResource(R.drawable.message_selected);
            messageText.setTextColor(Color.WHITE);
            if (messageFragment == null) {
                // 如果MessageFragment为空,则创建一个并添加到界面上
                messageFragment = new MessageFragment();
                transaction.add(R.id.content, messageFragment);
            } else {
                // 如果MessageFragment不为空,则直接将它显示出来
                transaction.show(messageFragment);
            }
            break;
        case 1:
            // 当点击了联系人tab时,改变控件的图片和文字颜色
            contactsImage.setImageResource(R.drawable.contacts_selected);
            contactsText.setTextColor(Color.WHITE);
            if (contactsFragment == null) {
                // 如果ContactsFragment为空,则创建一个并添加到界面上
                contactsFragment = new ContactsFragment();
                transaction.add(R.id.content, contactsFragment);
            } else {
                // 如果ContactsFragment不为空,则直接将它显示出来
                transaction.show(contactsFragment);
            }
            break;
        case 2:
            // 当点击了动态tab时,改变控件的图片和文字颜色
            newsImage.setImageResource(R.drawable.news_selected);
            newsText.setTextColor(Color.WHITE);
            if (newsFragment == null) {
                // 如果NewsFragment为空,则创建一个并添加到界面上
                newsFragment = new NewsFragment();
                transaction.add(R.id.content, newsFragment);
            } else {
                // 如果NewsFragment不为空,则直接将它显示出来
                transaction.show(newsFragment);
            }
            break;
        case 3:
        default:
            // 当点击了设置tab时,改变控件的图片和文字颜色
            settingImage.setImageResource(R.drawable.setting_selected);
            settingText.setTextColor(Color.WHITE);
            if (settingFragment == null) {
                // 如果SettingFragment为空,则创建一个并添加到界面上
                settingFragment = new SettingFragment();
                transaction.add(R.id.content, settingFragment);
            } else {
                // 如果SettingFragment不为空,则直接将它显示出来
                transaction.show(settingFragment);
            }
            break;
        }
        transaction.commit();
    }

    /**
     * 清除掉所有的选中状态。
     */
    private void clearSelection() {
        messageImage.setImageResource(R.drawable.message_unselected);
        messageText.setTextColor(Color.parseColor("#82858b"));
        contactsImage.setImageResource(R.drawable.contacts_unselected);
        contactsText.setTextColor(Color.parseColor("#82858b"));
        newsImage.setImageResource(R.drawable.news_unselected);
        newsText.setTextColor(Color.parseColor("#82858b"));
        settingImage.setImageResource(R.drawable.setting_unselected);
        settingText.setTextColor(Color.parseColor("#82858b"));
    }

    /**
     * 将所有的Fragment都置为隐藏状态。
     * 
     * @param transaction
     *            用于对Fragment执行操作的事务
     */
    private void hideFragments(FragmentTransaction transaction) {
        if (messageFragment != null) {
            transaction.hide(messageFragment);
        }
        if (contactsFragment != null) {
            transaction.hide(contactsFragment);
        }
        if (newsFragment != null) {
            transaction.hide(newsFragment);
        }
        if (settingFragment != null) {
            transaction.hide(settingFragment);
        }
    }
}

需要强调的知识点

1.

或者通过编程方式将片段添加到某个现有 ViewGroup
您可以在 Activity 运行期间随时将片段添加到 Activity 布局中。您只需指定要将片段放入哪个 ViewGroup。

要想在您的 Activity 中执行片段事务(如添加、移除或替换片段),您必须使用 FragmentTransaction 中的 API。您可以像下面这样从 Activity 获取一个 FragmentTransaction 实例:

FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
然后,您可以使用 add() 方法添加一个片段,指定要添加的片段以及将其插入哪个视图。例如:

ExampleFragment fragment = new ExampleFragment();
fragmentTransaction.add(R.id.fragment_container, fragment);
fragmentTransaction.commit();
传递到 add() 的第一个参数是 ViewGroup,即应该放置片段的位置,由资源 ID 指定,第二个参数是要添加的片段。

一旦您通过 FragmentTransaction 做出了更改,就必须调用 commit() 以使更改生效。

fragmentTransaction 必须要声明为局部变量,不能是全局变量,因为只能commit一次,fragmentTransaction.commit();
上面的代码也是巧妙的用到了这一点

// 开启一个Fragment事务
        FragmentTransaction transaction = fragmentManager.beginTransaction();

2.

底部布局这样写更简单

   <RadioGroup
            android:id="@+id/rg_title"
            android:layout_width="match_parent"
            android:layout_height="42dp"
            android:gravity="center"
            android:orientation="horizontal">

            <RadioButton
                android:id="@+id/rb_news_list"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:background="@drawable/bg_login_type_left"
                android:button="@null"
                android:checked="true"
                android:gravity="center"
                android:text="@string/news_list"
                android:textColor="@color/bg_color_login_type" />

            <RadioButton
                android:id="@+id/rb_announcements"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:background="@drawable/bg_login_type_right"
                android:button="@null"
                android:gravity="center"
                android:text="@string/notices_management"
                android:textColor="@color/bg_color_login_type" />
...
        </RadioGroup>
  /**
     *  用replace ,fragment被移除替换的时候,fragment中对象messageInfos为空,会导致这里的对象也为空
     *  用add替换
     */

    private RadioGroup.OnCheckedChangeListener onCheckedListener = new RadioGroup.OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(RadioGroup radioGroup, int i) {

            switch (i) {
                case R.id.rb_news_list:

                    setTabSelection(0);
                    break;

                case R.id.rb_announcements:

                    setTabSelection(1);
                    break;

            }


        }
    };

原来的底部布局

<android.support.design.widget.CoordinatorLayout 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.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

        <include layout="@layout/include_toolbar" />

    </android.support.design.widget.AppBarLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

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

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="60dp"
            android:background="@drawable/tab_bg">

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

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_centerVertical="true"
                    android:orientation="vertical">

                    <ImageView
                        android:id="@+id/message_image"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center_horizontal"
                        android:src="@drawable/message_unselected" />

                    <TextView
                        android:id="@+id/message_text"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center_horizontal"
                        android:text="消息"
                        android:textColor="#82858b" />
                </LinearLayout>
            </RelativeLayout>

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

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_centerVertical="true"
                    android:orientation="vertical">

                    <ImageView
                        android:id="@+id/contacts_image"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center_horizontal"
                        android:src="@drawable/contacts_unselected" />

                    <TextView
                        android:id="@+id/contacts_text"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center_horizontal"
                        android:text="联系人"
                        android:textColor="#82858b" />
                </LinearLayout>
            </RelativeLayout>

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

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_centerVertical="true"
                    android:orientation="vertical">

                    <ImageView
                        android:id="@+id/news_image"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center_horizontal"
                        android:src="@drawable/news_unselected" />

                    <TextView
                        android:id="@+id/news_text"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center_horizontal"
                        android:text="动态"
                        android:textColor="#82858b" />
                </LinearLayout>
            </RelativeLayout>

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

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_centerVertical="true"
                    android:orientation="vertical">

                    <ImageView
                        android:id="@+id/setting_image"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center_horizontal"
                        android:src="@drawable/setting_unselected" />

                    <TextView
                        android:id="@+id/setting_text"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center_horizontal"
                        android:text="设置"
                        android:textColor="#82858b" />
                </LinearLayout>
            </RelativeLayout>
        </LinearLayout>

    </LinearLayout>
</android.support.design.widget.CoordinatorLayout>

状态

背景和字体颜色选择的是:

android:state_checked

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item  android:state_checked="true">
        <shape android:shape="rectangle">

            <solid android:color="#4db6f4"/>
            <corners android:bottomLeftRadius="@dimen/_5dp" android:topLeftRadius="@dimen/_5dp"/>

        </shape>

    </item>

    <item  android:state_checked="false">
        <shape android:shape="rectangle">

            <solid android:color="#cfcfcf"/>
            <corners android:bottomLeftRadius="@dimen/_5dp" android:topLeftRadius="@dimen/_5dp"/>
        </shape>

    </item>

</selector>

参考
https://blog.youkuaiyun.com/guolin_blog/article/details/13171191?locationNum=2

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值