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

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

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

先给出总体效果:

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

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

步骤一:

    创建一个界面框架布局文件activity_frame.xml ,一个垂直现行布局包含:从上到下第一个帧布局FrameLayout用于动态加载Fragment,命其id为content;第二个布局是一个相对布局,用于放置三个标签按钮,其中三个线性布局嵌套分别一个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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • MyBatis中Like语句使用方式

    Mybatis中Like的使用,防止Sql注入

    2022年2月26日
  • 美军打掉德国炮兵阵地的电影_英雄连2怎么找炮兵阵地

    美军打掉德国炮兵阵地的电影_英雄连2怎么找炮兵阵地司令部的将军们打算在 N×M 的网格地图上部署他们的炮兵部队。一个 N×M 的地图由 N 行 M 列组成,地图的每一格可能是山地(用 H 表示),也可能是平原(用 P 表示),如下图。在每一格平原地形上最多可以布置一支炮兵部队(山地上不能够部署炮兵部队);一支炮兵部队在地图上的攻击范围如图中黑色区域所示:如果在地图中的灰色所标识的平原上部署一支炮兵部队,则图中的黑色的网格表示它能够攻击到的区域:沿横向左右各两格,沿纵向上下各两格。图上其它白色网格均攻击不到。从图上可见炮兵的攻击范围不受地形的影响

    2022年8月9日
  • 堆叠降噪自动编码器 Stacked Denoising Auto Encoder(SDAE)

    堆叠降噪自动编码器 Stacked Denoising Auto Encoder(SDAE)自动编码器(Auto-Encoder,AE)自编码器(autoencoder)是神经网络的一种,经过训练后能尝试将输入复制到输出。自编码器内部有一个隐藏层h,可以产生编码(code)表示输入。该网络可以看作由两部分组成:一个由函数h=f(x)表示的编码器和一个生成重构的解码器r=g(h)。我们不应该将自编码器设计成输入到输出完全相等。这通常需要向自编码器强加一些约束,使它只能近…

    2022年5月22日
  • 深信服(SCSA)认证学习—基础知识点二

    深信服(SCSA)认证学习—基础知识点二综述经过一周的实践教学学习,总体来讲深信服的scsa认证还是不难(理论基本上上都是基础知识),对于实验来讲呢,技术做的越多,熟练度也就越好,也就能更快的做出题目来。这次我主要还是讲讲深信服的理论知识。五元组:源IP地址,源端口,目的IP地址,目的端口和传输层协议mail.qq.comfqdnfulldomainname//顶级域名qq.comdomainname邮件传输协议:SMTP(25端口)、POP3(110端口)、IMAP4http:通一资源标志符(UniformRe

    2022年6月20日
  • linux upx脱壳工具,最新UPX加脱壳工具简单修改版

    linux upx脱壳工具,最新UPX加脱壳工具简单修改版SECURITY_ATTRIBUTES.nLength=12SECURITY_ATTRIBUTES.lpSecurityDescriptor=0SECURITY_ATTRIBUTES.bInheritHandle=真CreatePipe(hRead,hWrite,SECURITY_ATTRIBUTES,0)STARTUPINFO.cb=68STARTUPINFO.dwFla…

    2022年7月19日
  • JAVA 找不到符号

    JAVA 找不到符号写了一个新接口后莫名其妙报找不到符号,但是代码又看出任何问题,上网查了半天也没找到解决方法,然后把这个项目关了重开……顺利运行

    2022年7月9日

发表回复

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

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