zoukankan      html  css  js  c++  java
  • Fragment生命周期及实现点击导航图片切换fragment,Demo

    PS:Fragment简介

        Fragment是Android3.0后引入的一个新的API,他出现的初衷是为了适应大屏幕的平板电脑, 当然现在他仍然是平板APP UI设计的宠儿,而且我们普通手机开发也会加入这个Fragment, 我们可以把他看成一个小型的Activity,又称Activity片段!想想,如果一个很大的界面,我们 就一个布局,写起界面来会有多麻烦,而且如果组件多的话是管理起来也很麻烦!而使用Fragment 我们可以把屏幕划分成几块,然后进行分组,进行一个模块化的管理!从而可以更加方便的在 运行过程中动态地更新Activity的用户界面!另外Fragment并不能单独使用,他需要嵌套在Activity 中使用,尽管他拥有自己的生命周期,但是还是会受到宿主Activity的生命周期的影响,比如Activity 被destory销毁了,他也会跟着销毁!

    下面是效果图,

    生命周期图:

    加载fragment1--->点击导航点fragment2(扳子)--->点击home键--->回到程序--->点击返回键(退出)。

    1:创建Fragment1,及xml文件。因为和Fragment2一样,这里就写一个了

    package day1.cn.frag;
    
    import android.app.Fragment;
    import android.content.Context;
    import android.os.Bundle;
    import android.util.AttributeSet;
    import android.util.Log;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    
    import day1.cn.ceshi001.R;
    
    
    public class Fragment1 extends Fragment {
        @Override
        public void onAttach(Context context) {
            super.onAttach(context);
            Log.e("Fragment", "onAttach: 11111");
        }
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            Log.e("Fragment", "onCreate: 11111");
        }
    
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            View view = inflater.inflate(R.layout.fragment1,container,false);
            Log.e("Fragment", "onCreateView: 11111");
            return view;
        }
    
    
          @Override
        public void onStart() {
            super.onStart();
            Log.e("Fragment", "onStart: 11111");
        }
    
        @Override
        public void onResume() {
            super.onResume();
            Log.e("Fragment", "onResume: 11111");
        }
    
        @Override
        public void onPause() {
            super.onPause();
            Log.e("Fragment", "onPause: 11111");
        }
    
        @Override
        public void onActivityCreated(Bundle savedInstanceState) {
            super.onActivityCreated(savedInstanceState);
            Log.e("Fragment", "onActivityCreated: 11111");
        }
    
        @Override
        public void onStop() {
            super.onStop();
            Log.e("Fragment", "onStop: 11111");
        }
    
        @Override
        public void onDestroyView() {
            super.onDestroyView();
            Log.e("Fragment", "onDestroyView: 11111");
        }
    
        @Override
        public void onDestroy() {
            super.onDestroy();
            Log.e("Fragment", "onDestroy: 11111");
        }
    
        @Override
        public void onDetach() {
            super.onDetach();
            Log.e("Fragment", "onDetach: 11111");
        }
    
        @Override
        public void onInflate(Context context, AttributeSet attrs, Bundle savedInstanceState) {
            super.onInflate(context, attrs, savedInstanceState);
            Log.e("Fragment", "onInflate: 11111");
    
        } }
    


    fragment.xml文件

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
    
        android:layout_height="match_parent">
        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:textSize="40dp"
            android:text="1111111111"
            android:gravity="center"/>
    
    </LinearLayout>
    

    2:MainActivity.java 及xml文件

    (1)fragment开启事物,用的都是import android.app.Fragment;
    import android.app.FragmentTransaction;并不是v4包下的。

    这里用的是add和hide,,没用replace。

     public void hideFrag(Fragment f1){
            FragmentTransaction ft=getFragmentManager().beginTransaction();
            if(f1!=null&& f1.isAdded()){
                ft.hide(f1);
    
            }
            ft.commit();
        }
    
        public void addFrag(Fragment f1) {
            FragmentTransaction ft = getFragmentManager().beginTransaction();
            if (f1 != null && !f1.isAdded()) {
                ft.add(R.id.id_contentlayout, f1);
    
            }
            ft.commit();
            getFragmentManager().beginTransaction().show(f1).commit();
        }
        public void hideAllFrag(){
            hideFrag(f1);
            hideFrag(f2);
        }
    


    (2)总代码:

    package day1.cn.ceshi001;
    
    import android.app.Fragment;
    import android.app.FragmentTransaction;
    import android.graphics.Color;
    import android.os.Bundle;
    import android.support.v7.app.AppCompatActivity;
    import android.view.View;
    import android.widget.LinearLayout;
    
    import day1.cn.frag.Fragment1;
    import day1.cn.frag.Fragment2;
    
    
    public class FragmentActivity1 extends AppCompatActivity implements View.OnClickListener {
    
        private Fragment1 f1;
        private Fragment2 f2;
        private LinearLayout l1;
        private LinearLayout l2;
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.fragment_layout);
    
            l1 = (LinearLayout) findViewById(R.id.id_L1);
            l2 = (LinearLayout) findViewById(R.id.id_L2);
    
            l1.setOnClickListener(this);
            l2.setOnClickListener(this);
            init();
    
    
    
        }
    
        private void init() {
            hideAllFrag();//首先隐藏全部fragment
            if(f1==null){
                f1 = new Fragment1();
            }
            addFrag(f1);
        }
    
        public void hideFrag(Fragment f1){
            //开启事务
            FragmentTransaction ft=getFragmentManager().beginTransaction();
            //如果不为null 并且已经添加过了,就隐藏掉。
            if(f1!=null&& f1.isAdded()){
                ft.hide(f1);
    
            }
            //提交
            ft.commit();
        }
    
        public void addFrag(Fragment f1) {
            FragmentTransaction ft = getFragmentManager().beginTransaction();
            if (f1 != null && !f1.isAdded()) {
                //添加到指定布局
                ft.add(R.id.id_contentlayout, f1);
    
            }
            ft.commit();
            getFragmentManager().beginTransaction().show(f1).commit();//显示所添加的fragment
        }
        public void hideAllFrag(){
            hideFrag(f1);
            hideFrag(f2);
        }
    
        @Override
        public void onClick(View v) {
            hideAllFrag();
            switch (v.getId()){
                case R.id.id_L1:
                    if(f1==null){
                        f1 = new Fragment1();
                    }
                    addFrag(f1);
    
                    //更改点击后的导航布局背景颜色。
                    l1.setBackgroundColor(Color.rgb(228,228,228));
                    l2.setBackgroundColor(Color.WHITE);
                    break;
                case R.id.id_L2:
                    if (f2 == null) {
                        f2 = new Fragment2();
                    }
                    addFrag(f2);
                    l2.setBackgroundColor(Color.rgb(228,228,228));
                    l1.setBackgroundColor(Color.WHITE);
                    break;
            }
        }
    }
    

    fragment_layout.xml

     
    
    <?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">
        <LinearLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/id_contentlayout"
            android:layout_above="@+id/include">
    
        </LinearLayout>
       <include android:id="@+id/include"
           android:layout_alignParentBottom="true"
           android:layout_width="match_parent"
           android:layout_height="60dp"
    layout="@layout/bottom"
           ></include>
    
    </RelativeLayout>
    


    底部导航:bottom.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:orientation="horizontal"
        android:layout_height="match_parent">
        <LinearLayout
            android:id="@+id/id_L1"
            android:layout_width="0dp"
            android:layout_height="55dp"
            android:layout_weight="1"
            android:background="#e4e4e4"
            android:orientation="vertical">
            <ImageView
                android:layout_width="match_parent"
                android:layout_height="30dp"
                android:src="@drawable/img2"/>
            <TextView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:gravity="center"
                android:text="首页"/>
        </LinearLayout>
        <LinearLayout
            android:id="@+id/id_L2"
            android:layout_width="0dp"
            android:layout_height="55dp"
            android:layout_weight="1"
    
            android:orientation="vertical">
            <ImageView
                android:layout_width="match_parent"
                android:layout_height="30dp"
                android:src="@drawable/img1"/>
            <TextView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:gravity="center"
                android:text="扳子"/>
        </LinearLayout>
    </LinearLayout>
    


    完。

  • 相关阅读:
    Java method Exception throw with return instance.
    SQL Profiler Practice.
    FTP 文件 循环 Copy
    【SQL遗补】之 SET NOCOUNT (TransactSQL)
    【WinForm窗体控件开发】之三 窗体控件设计时属性Attribute
    【WinForm窗体控件开发】之三续 窗体控件设计时的事件属性
    【WinForm窗体控件开发】之二 简单的窗体控件
    解决删除DataGridView中数据引发的“DataGridView Default Error Dialog 错误”
    .NET开发WinCE程序之使用软键盘(System.WindowsCE.Forms命名空间)兼容WinCE和桌面操作系统之解决方案
    【C#遗补】之Char.IsDigit和Char.IsNumber的区别
  • 原文地址:https://www.cnblogs.com/cmusketeer/p/8016545.html
Copyright © 2011-2022 走看看