Android学习笔记 Fragment

—-作者为菜鸟,请多多指教。—

效果图:这里写图片描述

工程结构:这里写图片描述

  • activity界面(activity_main.xml)

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:toolbar="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.david.test.MainActivity">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="@color/slateblue"
        toolbar:navigationIcon="@mipmap/icon"
        toolbar:title="test APP"/>

    <LinearLayout
        android:id="@+id/button_group"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_alignParentBottom="true"
        android:background="@color/slateblue"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin">

        <Button
            android:id="@+id/button_message"
            android:layout_width="100dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="消息" />

        <Button
            android:id="@+id/button_lxr"
            android:layout_width="100dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="联系人" />

        <Button
            android:id="@+id/button_dt"
            android:layout_width="100dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="动态" />
    </LinearLayout>

    <FrameLayout
        android:id="@+id/frame_content"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@id/button_group"
        android:layout_below="@id/toolbar"></FrameLayout>
</RelativeLayout>
  • fragment子界面(fragment_dt.xml) 其他界面相似
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="layout.DTFragment">

    <!-- TODO: Update blank fragment layout -->
    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="这是动态界面" />

</FrameLayout>
  • Mainactivity.java
package com.david.test;

import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.widget.Toast;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import layout.DTFragment;
import layout.LXRFragment;
import layout.MessageFragment;

public class MainActivity extends Activity {

    @Bind(R.id.toolbar)
    Toolbar toolbar;
    private FragmentManager fragmentManager;
    MessageFragment messageFragment;
    LXRFragment lxrFragment;
    DTFragment dtFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        //toolbar.setNavigationIcon(R.mipmap.icon);//设置导航栏图标
        //toolbar.setLogo(R.mipmap.icon);//设置app logo
        //toolbar.setTitle("text APP");//设置主标题
        //toolbar.setSubtitle("Subtitle");//设置子标题
        toolbar.inflateMenu(R.menu.menu_main);//设置右上角的填充菜单

        toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {//菜单的监听事件
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                int menuItemId = item.getItemId();
                if (menuItemId == R.id.action_login) {
                    Toast.makeText(MainActivity.this, "登陆", Toast.LENGTH_SHORT).show();
                } else if (menuItemId == R.id.action_logout) {
                    Toast.makeText(MainActivity.this, "注销", Toast.LENGTH_SHORT).show();
                }
                return true;
            }
        });

        fragmentManager = getFragmentManager();
        setTabSelection(0);
    }

    @OnClick({R.id.button_message, R.id.button_lxr, R.id.button_dt})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.button_message:
                setTabSelection(0);
                break;
            case R.id.button_lxr:
                setTabSelection(1);
                break;
            case R.id.button_dt:
                setTabSelection(2);
                break;
        }
    }

    /**
     * 选中某个子页面
     * 两种方式
     *  1、transaction.replace替换fragment
     *  2、transaction.hide和show通过隐藏和显示
     * @param index
     */
    public void setTabSelection(int index) {
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        //hideFragments(transaction);
        switch (index) {
            case 0:
                if (messageFragment == null) {
                    messageFragment = new MessageFragment();
                    //transaction.add(R.id.frame_content, messageFragment.newInstance("aa", "bb"));
                    transaction.replace(R.id.frame_content, messageFragment.newInstance("aa", "bb"));
                } else {
                    transaction.replace(R.id.frame_content, messageFragment.newInstance("aa", "bb"));
                    //transaction.show(messageFragment);
                }
                break;
            case 1:
                if (lxrFragment == null) {
                    lxrFragment = new LXRFragment();
                    //transaction.add(R.id.frame_content, lxrFragment);
                    transaction.replace(R.id.frame_content, lxrFragment);
                } else {
                    transaction.replace(R.id.frame_content, lxrFragment);
                    //transaction.show(lxrFragment);
                }
                break;
            case 2:
                if (dtFragment == null) {
                    dtFragment = new DTFragment();
                    //transaction.add(R.id.frame_content, dtFragment);
                    transaction.replace(R.id.frame_content, dtFragment);
                } else {
                    transaction.replace(R.id.frame_content, dtFragment);
                    //transaction.show(dtFragment);
                }
                break;
            default:

                break;
        }
        transaction.commit();
    }

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


import android.app.Fragment;
import android.os.Bundle;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.david.test.R;

/**
 * A simple {@link Fragment} subclass.
 */
public class DTFragment extends Fragment {


    public DTFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_dt, container, false);
    }

}
  • menu_main.xml
<menu 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"
    tools:context="com.david.test.MainActivity">
    <item
        android:id="@+id/action_login"
        android:orderInCategory="100"
        android:title="登陆"
        app:showAsAction="never" />
    <item
        android:id="@+id/action_logout"
        android:orderInCategory="100"
        android:title="注销"
        app:showAsAction="never" />
</menu>

个人记录知识点:Toolbar(标题栏)、FrameLayout(Frame布局)menu(菜单)

demo下载

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值