1.   阿里云服务器1888元代金券领取
  2.   本网站所有源码包解压密码:www.youhutong.com
登录  帮助问答

Android的主底部菜单栏控件fragment页面显示隐藏、fragment页面点击按钮切换到下一个fragment页面

小川 技术文章
郑重声明:
  本站发布的内容仅限用于学习和研究目的.请勿用于商业或非法用途,否则后果请用户自负,下载后请24小时内删除。 本站所有内容均不能保证其完整性,不提供任何技术支持,不能接受请勿购买或下载,如需完整程序,请去其官方购买正版使用。
下载源码  
【温馨提示】源码包解压密码: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"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#cccccc">
    
    <!-- 内容(显示各种fragment页面) -->
    <RelativeLayout
        android:id="@+id/main_content"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    </RelativeLayout>

    <!-- 底部按钮 -->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="35dp"
        android:background="#ffffff"
        android:gravity="center"
        android:layout_alignParentBottom="true"
        android:orientation="horizontal">
        <TextView
            android:id="@+id/main"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="35dp"
            android:gravity="center"
            android:text="首页"
            android:textColor="#000fff"/>
        <TextView
            android:id="@+id/shop"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="35dp"
            android:gravity="center"
            android:text="购物"
            android:textColor="#605e5e"/>
        <TextView
            android:id="@+id/user"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="35dp"
            android:gravity="center"
            android:text="会员"
            android:textColor="#605e5e"/>
    </LinearLayout>
</RelativeLayout>


主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"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center"
        android:text="这是首页"
        android:textSize="30sp"/>
    <TextView
        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"/>

</LinearLayout>


然后就是还有三个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"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center"
        android:text="这是购物页"
        android:textSize="30sp"/>

</LinearLayout>


源码:(android studio    model模块)





 浏览器启用弹出窗口过滤功能,将无法跳转到下载页。在浏览器地址栏右边符号提示处点击允许就可以了!
下载源码  
【温馨提示】源码包解压密码:www.youhutong.com

转载请注明来源地址:小川编程 » https://www.youhutong.com/index.php/article/index/177.html


  1、本站发布的内容仅限用于学习和研究目的.请勿用于商业或非法用途,下载后请24小时内删除。
  2、本站所有内容均不能保证其完整性,不能接受请勿购买或下载,如需完整程序,请去其官方购买正版使用
  3、本站联系方式Email:admin@youhutong.com ,收到邮件会第一时间处理。
  4、如侵犯到任何版权问题,请立即告知本站(立即在线告知),本站将及时删除并致以最深的歉意
( 0 )个小伙伴在吐槽
    登录帐号  如果已经登录请刷新! 发表我的评论
    表情