使用Activity动态加载Fragment实现主界面框架

使用Activity动态加载Fragment实现主界面框架

在做项目中,需要建立一个主界面框架,尝试过使用ViewPager,后来又换成了使用Activity动态加载Fragment实现选项卡的效果。总结一下方便以后回顾。

先给出总体效果:

 使用Activity动态加载Fragment实现主界面框架

要实现上述效果,首先来大体上阐述步骤:

步骤一:

    创建一个界面框架布局文件activity_frame.xml ,一个垂直现行布局包含:从上到下第一个帧布局FrameLayout用于动态加载Fragment,命其idcontent;第二个布局是一个相对布局,用于放置三个标签按钮,其中三个线性布局嵌套分别一个ImageView,为了能均分,权重都为1;图标自己制作。同时创建一个FrameActivity.java文件,来显示主界面框架。

activity_frame.xml 代码:

 

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

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

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="45dp"
        android:background="#c0c0c0">

        <RelativeLayout
            android:id="@+id/main_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/main_image"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_horizontal"
                    android:src="@drawable/img_main" />
            </LinearLayout>
        </RelativeLayout>

        <RelativeLayout
            android:id="@+id/shop_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/shop_image"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_horizontal"
                    android:src="@drawable/img_shop" />
            </LinearLayout>
        </RelativeLayout>

        <RelativeLayout
            android:id="@+id/shop_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/shop_image"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_horizontal"
                    android:src="@drawable/img_shop" />
            </LinearLayout>
        </RelativeLayout>


        <RelativeLayout
            android:id="@+id/my_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/my_image"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_horizontal"
                    android:src="@drawable/img_my" />
            </LinearLayout>
        </RelativeLayout>
    </LinearLayout>

</LinearLayout>

 

步骤二:

   分别创建四个布局文件:

其中布局内容根据自己想要的布局设置;

步骤三:
    然后再为这四个布局创建对应的Fragment

其中每个Fragment加载布局的代码都是差不多,都是在onCreateView中获得一个View对象,其它代码根据业务需要而编写,如图:

步骤四:

    四Fragment创建好了之后,此时需要在FrameActivity中,通过界面底边的三个标签注册单击事件监听器来动态地加载Fragment

业务代码如下:


setTabSelection中包含一个switch函数,根据判断id来动态加载fragment(动态加载fragent的步骤这里不给出,csdn有相应的文章):
 FrameActivity的完整代码如下:

 

package com.android.activity;import com.android.client.R;import com.android.fragment.MainFragment;import com.android.fragment.MyFragment;import com.android.fragment.ShopFragment;import android.app.Activity;import android.app.AlertDialog;import android.app.FragmentManager;import android.app.FragmentTransaction;import android.app.AlertDialog.Builder;import android.content.DialogInterface;import android.os.Bundle;import android.view.KeyEvent;import android.view.View;import android.view.View.OnClickListener;import android.view.Window;import android.widget.ImageView;public class FrameActivity extends Activity implements OnClickListener {    private MainFragment mainFragment;    private ShopFragment shopFragment;    private MyFragment myFragment;    private View mainLayout;    private View shopLayout;    private View myLayoutView;    private ImageView mainImg;    private ImageView shopImg;    private ImageView myImg;    private FragmentManager fragmentManager;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        requestWindowFeature(Window.FEATURE_NO_TITLE);        setContentView(R.layout.activity_frame);        initViews();        fragmentManager = getFragmentManager();        setTabSelection(0);    }    private void setTabSelection(int i) {        clearSelection();        FragmentTransaction transaction = fragmentManager.beginTransaction();        hideFragments(transaction);        switch (i) {            case 0:                mainImg.setImageResource(R.drawable.img_main_pressed);                if (mainFragment == null) {                    mainFragment = new MainFragment();                    transaction.add(R.id.content, mainFragment);                } else {                    transaction.show(mainFragment);                }                break;            case 1:                shopImg.setImageResource(R.drawable.img_shop_pressed);                if (shopFragment == null) {                    shopFragment = new ShopFragment();                    transaction.add(R.id.content, shopFragment);                } else {                    transaction.show(shopFragment);                }                break;            case 2:            default:                myImg.setImageResource(R.drawable.img_my_pressed);                if (myFragment == null) {                    myFragment = new MyFragment();                    transaction.add(R.id.content, myFragment);                } else {                    transaction.show(myFragment);                }                break;        }        transaction.commit();    }    private void hideFragments(FragmentTransaction transaction) {        if (mainFragment != null) {            transaction.hide(mainFragment);        }        if (shopFragment != null) {            transaction.hide(shopFragment);        }        if (myFragment != null) {            transaction.hide(myFragment);        }    }    private void clearSelection() {        // TODO Auto-generated method stub  mainImg.setImageResource(R.drawable.img_main);        shopImg.setImageResource(R.drawable.img_shop);        myImg.setImageResource(R.drawable.img_my);    }    private void initViews() {        // TODO Auto-generated method stub  mainLayout = findViewById(R.id.main_layout);        shopLayout = findViewById(R.id.shop_layout);        myLayoutView = findViewById(R.id.my_layout);        mainImg = (ImageView) findViewById(R.id.main_image);        shopImg = (ImageView) findViewById(R.id.shop_image);        myImg = (ImageView) findViewById(R.id.my_image);        mainLayout.setOnClickListener(this);        shopLayout.setOnClickListener(this);        myLayoutView.setOnClickListener(this);    }    @Override    public void onClick(View arg0) {        switch (arg0.getId()) {            case R.id.main_layout:                setTabSelection(0);                break;            case R.id.shop_layout:                setTabSelection(1);                break;            case R.id.my_layout:                setTabSelection(2);                break;            default:                break;        }        // TODO Auto-generated method stub   }    //框架中的退出提示代码,常规代码重用率很高可以抽象出来    @Override    public boolean onKeyDown(int keyCode, KeyEvent event) {        // TODO Auto-generated method stub  if (keyCode == KeyEvent.KEYCODE_BACK) {            if (keyCode == KeyEvent.KEYCODE_BACK) {                Builder builder = new Builder(FrameActivity.this);                builder.setTitle("提示");                builder.setMessage("你确定要退出吗?");                builder.setIcon(R.drawable.ic_launcher);                DialogInterface.OnClickListener dialog = new DialogInterface.OnClickListener() {                    @Override                    public void onClick(DialogInterface arg0, int arg1) {                        // TODO Auto-generated method stub  if (arg1 == DialogInterface.BUTTON_POSITIVE) {                            arg0.cancel();                        } else if (arg1 == DialogInterface.BUTTON_NEGATIVE) {                            FrameActivity.this.finish();                        }                    }                };                builder.setPositiveButton("取消", dialog);                builder.setNegativeButton("确定", dialog);                AlertDialog alertDialog = builder.create();                alertDialog.show();            }        }        return false;    }}

 

到此,界面框架已经建好,接下来就根据自己的业务需要实现代码。

犯了错,总之要改,改之后呢,最好还是记录。因为这些难题在后来的日子还是会遇到。不妨到时候回过头看看自己当时记录的错误。毕竟是自己的错,资以改正。


纸上得来终觉浅,书到用时方恨少。希望对你有所帮助。

 

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/115080.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • PyCharm安装配置谷歌浏览器驱动[通俗易懂]

    PyCharm安装配置谷歌浏览器驱动[通俗易懂]PyCharm配置安装Chorme驱动下载驱动查看Chorme的版本这是我的谷歌浏览器版本去网站下载对应的驱动软件下载后进入下载的目录,解压然后把这个文件放入Python的Scripts文件夹(首先你的Python环境变量已经配置过)驱动下载地址:http://chromedriver.storage.proxy.ustclug.org/index.html安装驱动进入设置,按照步骤点击安装selenium包然后会出现安装进程,添加完成之后开始测试驱动测试驱动创建一个Pyth

    2022年5月4日
    599
  • 关于SoftMax函数的一些介绍[通俗易懂]

    前言SoftMax函数是在机器学习中经常出现的,时常出现在输出层中。对于这个函数,大部分blog作者对于它介绍已经很完善了,包括如何玄学设计,如何使用等等,这里只是从数学来源上讨论下这个函数名字的来历,或者说数学的来源,为什么叫做SoftMax(有没有HardMax)等等。1.SoftMax的形式SoftMax函数,全名SoftMaximum函数。其形式为σ(z)j=ezjΣk=…

    2022年4月14日
    91
  • 软件版本号命名规则参考标准_怎么修改app版本号名称

    软件版本号命名规则参考标准_怎么修改app版本号名称为了在软件产品生命周期中更好的沟通和标记,我们应该对APP、软件的版本号命名的规范和原则有一定的了解。1、APP、软件的版本阶段Alpha版:也叫α版,此版本主要是以实现软件功能为主,通常只在软件开发者内部交流,一般而言,该版本软件的Bug较多,需要继续修改; Beta版:此版本相对于α版已经有了很大的改进,消除了严重的错误,但还是存在着一些缺陷,需要经过多次测试来进一…

    2022年9月10日
    0
  • 建立友好城市有什么用_中国国际友好城市联合会

    建立友好城市有什么用_中国国际友好城市联合会原题连接Palmia国有一条横贯东西的大河,河有笔直的南北两岸,岸上各有位置各不相同的N个城市。北岸的每个城市有且仅有一个友好城市在南岸,而且不同城市的友好城市不相同。每对友好城市都向政府申请在河上开辟一条直线航道连接两个城市,但是由于河上雾太大,政府决定避免任意两条航道交叉,以避免事故。编程帮助政府做出一些批准和拒绝申请的决定,使得在保证任意两条航线不相交的情况下,被批准的申请尽量多。输入格式第1行,一个整数N,表示城市数。第2行到第n+1行,每行两个整数,中间用1个空格隔开,分别表示南岸和

    2022年8月8日
    1
  • Visual Studio 2008 序列号 激活 vs2008[通俗易懂]

    Visual Studio 2008 序列号 激活 vs2008[通俗易懂]
    VisualStudio2008简体中文试用版(90天)变永久正式版的两种方法:
    一、先安装试用版,然后在“添加或删除程序”里找到VS2008,点“更改/删除”就会看到一个输入序列号的地方,把下面这个序列号输进去即可,TeamSuite和Professional通用。
    二、把Setupsetup.sdb文件中的[ProductKey]项中对应的序列号即可。
    因为九十天试用版本已经是rtm版本。所以改变序列号以后的升级或者安装,就会变成正式版,不再

    2022年8月10日
    36
  • c++sscanf函数_c语言字符串常用函数

    c++sscanf函数_c语言字符串常用函数在处理字符串的程序当中,经常会分析字符串,从一大长串的字符串截取我们需要的数据,这如果通过自己手写函数来分析,虽然可以,但当你知道sscanf的功能,那就自己写函数分析就显得多此一举。这些函数的使用都很简单,总结一下,等下次使用一目了然。俗话说:好记性不如烂笔头,记录下来就是效率。以下源代码是本人测试的源文件,附带讲解注释。/************************

    2025年6月11日
    0

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号