JAVA 点击菜单栏后跳到界面_Android的主底部菜单栏控件fragment页面显示隐藏、fragment页面点击按钮切换到下一个fragment页面...

本文介绍了在Android开发中如何使用Fragment实现底部菜单栏点击切换页面,避免频繁的Activity跳转,提高应用性能。通过创建Fragment类、绑定布局、管理Fragment实例以及监听按钮事件来详细阐述切换过程。
摘要由CSDN通过智能技术生成

【温馨提示】源码包解压密码:www.youhutong.com

场景:

android开发中,经常看到好几个UI界面底部都有一个相同的菜单。

点击对应菜单项时,会显示对应的页面,点击某个按钮时又会显示对应页面。

分析:

对于初学者来说可能会以为是Activity跳转了,当然你要这样做也可以实现了(不推荐这样做哦)。

为什么不能用Activity跳转呢?

如果这样用,每个Acitity相同代码量会很多,而且跳转都是要刷新UI界面的。体验速度就不用说了,可想而知!

那么该用什么呢?

我们这时就要用Fragment(Fragment的简介?可以去看另一篇文章)。

用它就是为了不重复的刷新(也就是不重新实例化),要刷新时才从新刷新(重新实例化)。

实现:

先说一下大概步骤:

准备工作: 先创建一个主Acitity和对应的布局文件(写好底部菜单UI)

第一步:创建4个继承自android.support.v4.app.Fragment的java文件,并一起创建对应的布局文件/*

1):在mainActivity.java文件同级目录新建名为fragment的目录    (用来放4个java文件)

2):新建文件时,一定要继承自Fragment(是android.support.v4包下的)

3):创建对应的布局文件。

4):为fragment文件绑定布局文件,通过重写方法onCreateView方法

*/第二步:添加所有Fragment到管理类去。(添加前先实列化好)第三步:要能过点击按钮显示不同的Fragment当然得先监听按钮事件

第四步:创建一个继承自FragmentActivity的类,并定义一个方法来实现Fragment页面间的切换第五步:在当前Activity中创建一个方法,调用内部类的页面切换方法。并保存当前Tag

第六步:fragment中控制页面切换(去对应Java文件中设置监听事件,调用switchFragment方法)

主Activity的布局文件:<?xml  version="1.0" encoding="utf-8"?>

android:layout_width="match_parent"

android:layout_height="match_parent"

android:background="#cccccc">

android:id="@+id/main_content"

android:layout_width="match_parent"

android:layout_height="wrap_content">

android:layout_width="match_parent"

android:layout_height="35dp"

android:background="#ffffff"

android:gravity="center"

android:layout_alignParentBottom="true"

android:orientation="horizontal">

android:id="@+id/main"

android:layout_width="0dp"

android:layout_weight="1"

android:layout_height="35dp"

android:gravity="center"

android:text="首页"

android:textColor="#000fff"/>

android:id="@+id/shop"

android:layout_width="0dp"

android:layout_weight="1"

android:layout_height="35dp"

android:gravity="center"

android:text="购物"

android:textColor="#605e5e"/>

android:id="@+id/user"

android:layout_width="0dp"

android:layout_weight="1"

android:layout_height="35dp"

android:gravity="center"

android:text="会员"

android:textColor="#605e5e"/>

主Activity的代码:package com.youhutong.fragment;

import android.os.Bundle;

import android.support.v4.app.Fragment;

import android.support.v4.app.FragmentActivity;

import android.support.v4.app.FragmentManager;

import android.support.v7.app.AppCompatActivity;

import android.view.View;

import android.widget.TextView;

import com.youhutong.fragment.fragment.CateFragment;

import com.youhutong.fragment.fragment.MainFragment;

import com.youhutong.fragment.fragment.ShopFragment;

import com.youhutong.fragment.fragment.UserFragment;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

private String fragmentTag;

private TextView main,shop,user;

private Fragment mainFragment = new MainFragment(),

shopFragment = new ShopFragment(),

userFragment = new UserFragment(),

cateFragment = new CateFragment();

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

//  第一步:创建4个继承自android.support.v4.app.Fragment的java文件,并一起创建对应的布局文件

/*

1):在mainActivity.java文件同级目录新建名为fragment的目录    (用来放4个java文件)

2):新建文件时,一定要继承自Fragment(是android.support.v4包下的)

3):创建对应的布局文件。

4):为fragment文件绑定布局文件,通过重写方法onCreateView方法

*/

//  第二步:添加所有Fragment到管理类去。(添加前先实列化好)

this.getSupportFragmentManager()                                      // 获取管理类

.beginTransaction()                                           // 开启事物

.add(R.id.main_content, mainFragment, "mainFragment") // 添加fragment

.add(R.id.main_content, shopFragment, "shopFragment") // 添加fragment

.add(R.id.main_content, userFragment, "userFragment") // 添加fragment

.add(R.id.main_content, cateFragment, "cateFragment") // 添加fragment

.hide(cateFragment)

.hide(shopFragment)

.hide(userFragment)

.commit();                                                    // 提交

this.fragmentTag = "mainFragment";                                    // 保存当前显示的Tag

//  第三步:要能过点击按钮显示不同的Fragment当然得先监听按钮事件

initView();

//  第四步:创建一个继承自FragmentActivity的类,并定义一个方法来实现Fragment页面间的切换

//  第五步:在当前Activity中创建一个方法,调用内部类的页面切换方法。并保存当前Tag

//  第六步:fragment中控制页面切换(去对应Java文件中设置监听事件,调用switchFragment方法)

}

/**

* 主Activity切换Fragment页面方法入口

* @param toTag  要显示的Fragment的Tag

*/

public void switchFragment(String toTag){

MyFragmentActivity mf = new MyFragmentActivity();

mf.switchFragment(getSupportFragmentManager(),toTag, this.fragmentTag);

this.fragmentTag = toTag;

}

/**

*  创建一个继承自FragmentActivity的类,并定义一个方法来实现Fragment页面间的切换

*/

class MyFragmentActivity extends FragmentActivity{

// 定义一个方法:实现Fragment页面间的切换

public void switchFragment(FragmentManager fm, String toTag, String foTag) {

Fragment fo = fm.findFragmentByTag(foTag);

Fragment to = fm.findFragmentByTag(toTag);

if (fo != to) {

fm.beginTransaction().hide(fo).show(to).commit();

}

}

}

/**

* 初始化3个按钮,并绑定监听器

*/

private void initView() {

main = (TextView) findViewById(R.id.main);

shop = (TextView) findViewById(R.id.shop);

user = (TextView) findViewById(R.id.user);

main.setOnClickListener(this);

shop.setOnClickListener(this);

user.setOnClickListener(this);

}

/**

* 监听器对应点击方法

*/

public void onClick(View v) {

switch (v.getId()){

case R.id.main:

this.switchFragment("mainFragment");

break;

case R.id.shop:

this.switchFragment("shopFragment");

break;

case R.id.user:

this.switchFragment("userFragment");

break;

}

}

}

MainFragment.java的代码:(这里带按钮点击切换Fragment的实现)package com.youhutong.fragment.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 android.widget.TextView;

import com.youhutong.fragment.MainActivity;

import com.youhutong.fragment.R;

/**

*    首页

*/

public class MainFragment extends Fragment{

private MainActivity mainActivity;

private TextView btnss;

@Nullable

@Override

// 创建该Fragment的视图

public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,

@Nullable Bundle savedInstanceState) {

this.mainActivity = (MainActivity) getActivity();

return inflater.inflate(R.layout.fragment_main, container, false);

}

@Override

// 当Activity的onCreate方法返回时调用

public void onActivityCreated(@Nullable Bundle savedInstanceState) {

super.onActivityCreated(savedInstanceState);

btnss = (TextView) getView().findViewById(R.id.main_btn1);

btnss.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

mainActivity.switchFragment("cateFragment");

}

});

}

}

MainFragment.java的布局文件:<?xml  version="1.0" encoding="utf-8"?>

android:orientation="vertical"

android:layout_width="match_parent"

android:layout_height="match_parent">

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_weight="1"

android:gravity="center"

android:text="这是首页"

android:textSize="30sp"/>

android:id="@+id/main_btn1"

android:layout_width="match_parent"

android:layout_height="40dp"

android:layout_margin="100dp"

android:background="#f91f39"

android:textColor="#ffffff"

android:gravity="center"

android:text="跳到分类页"

android:textSize="16sp"/>

然后就是还有三个Fragment和对应的布局文件,都是差不多(只是名字不同,显示文字不同而于)

这里就只列出一个Fragment和对应的布局文件:package com.youhutong.fragment.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.youhutong.fragment.R;

/**

*    分类页

*/

public class ShopFragment extends Fragment {

@Nullable

@Override

public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,

@Nullable Bundle savedInstanceState) {

return inflater.inflate(R.layout.fragment_shop, container, false);

}

}<?xml  version="1.0" encoding="utf-8"?>

android:orientation="vertical"

android:layout_width="match_parent"

android:layout_height="match_parent">

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_weight="1"

android:gravity="center"

android:text="这是购物页"

android:textSize="30sp"/>

源码:(android studio    model模块)

浏览器启用弹出窗口过滤功能,将无法跳转到下载页。在浏览器地址栏右边符号提示处点击允许就可以了!

【温馨提示】源码包解压密码:www.youhutong.com

郑重声明:

1、本站源码仅供个人学习研究和交流使用,请于下载后二十四小时内删除

2、本站大多资源来源于互联网、用户分享,仅供学习交流使用,本站不提供任何技术支持

3、本站联系方式Email:admin@youhutong.com ,收到邮件会第一时间处理。

4、如侵犯到任何版权问题,请立即告知本站(立即在线告知),本站将及时删除并致以最深的歉意

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值