• Android Fragemnt的基本使用


    Fragment的优点

    (1)复用性强:任何activity都可以使用同一个fragment。

    (2)解耦性强:有属于自己的完整的生命周期,可以做到与activity互不打扰。

    (3)适配性强:可以根据硬件的不同尺寸、屏幕方向,能够方便的实现布局,用户体验效果更好。

    Fragment&Activity

    fragment用activity中方法

    第一种:getActivity()Activity的类名叫 ParentActivity,有一个test()方法
    在Fragment中调用其实很简单,代码如下:
    ParentActivity parentActivity = (ParentActivity ) getActivity();
    parentActivity.test();
    
    
    第二种,接口回调
    Fragment中定议:
    public interface notification{
        void  sentNotification(final String toastStr);
    }Activity实现它
        
    然后在Fragment中在要调用Activity的方法这么写:
    if(getActivity()instanceof notification){
        ((notification)getActivity()).sentNotification(toastStr);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    activity 用fragment中方法

    Fragment FragmentManager fragmentManager = getFragmentManager();
    // 布局中fragment对应的id
    Fragment fragment = fragmentManager.findFragmentById(R.id.--); 
    // 或者通过tag标记查找
    Fragment fragment2 = fragmentManager.findFragmentByTag(tag);
    
    注意:在获取到fragment之后,调用getView方法是要明确改fragment是否已构建完布局,否则将会报空指针异常。
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    RadioGroup+Fragment切换

    布局

    设置字体切换颜色:android:textColor=“@color/fun_button_textcolor”
    设置无按钮颜色样式:android:button=“@null”
    设置默认选中:android:checked=“true”

    <RadioGroup
                android:id="@+id/fun_menu"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal" >
    
                <RadioButton
                    android:id="@+id/fun_document"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:gravity="center"
                    android:textColor="@color/fun_button_textcolor"
                    android:textSize="@dimen/fun_title_size"
                    android:text="@string/fun_document"
                    android:singleLine="true"
                    android:button="@null"
                    android:tag="fun_document"/>
    
                <RadioButton
                    android:id="@+id/fun_translate"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:gravity="center"
                    android:textColor="@color/fun_button_textcolor"
                    android:textSize="@dimen/fun_title_size"
                    android:text="@string/fun_translate"
                    android:singleLine="true"
                    android:button="@null"
                    android:tag="fun_translate"/>
    
                <RadioButton
                    android:id="@+id/fun_scan"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:gravity="center"
                    android:textColor="@color/fun_button_textcolor"
                    android:textSize="@dimen/fun_title_size"
                    android:text="@string/fun_scan"
                    android:singleLine="true"
                    android:button="@null"
                    android:checked="true"
                    android:tag="fun_scan"/>
    RadioGroup>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46

    切换颜色

    
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:color="@color/textColorWhite" android:state_checked="false" />
        <item android:color="@color/textColorYellow" android:state_checked="true" />
    selector>
    
    • 1
    • 2
    • 3
    • 4
    • 5

    切换:通过 RadioGroup 获取 RadioButton

    mFunMemu = findViewById(R.id.fun_menu);
            mFunMemu.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(RadioGroup group, int checkedId) {
                    RadioButton radioButton = (RadioButton) group.findViewById(checkedId);
                    currentFragmentTag = radioButton.getTag().toString();
                    replaceFragment(currentFragmentTag);
                }
            });
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    Fragment切换的问题

    参考

    常用的Fragment类

    • DiagFragment
    • ListFragment
    • webViewFragment
    1.获取Fragment Manager
    每个Activity对象都内置了一个FragmentManager对象,使用getFragmentManager()即可获得:
    FragmentManager fragmentManager = getFragmentManager();
    
    2.ActivityLayout布局中添加一个FrameLayout,用做容器来存放fragmennt
    <FrameLayout  
      android:id="@+id/ui_container"  
      android:layout_width="match_parent"  
      android:layout_height="match_parent"/>  
        
    3.使用FragmentTransaction      
    FragmentTransaction可以在运行时添加,删除或替换Fragment,从而实现UI的动态变化。Fragment TransactionFragment ManagerbeginTransaction()方法创建,然后可以进行Fragment的添加,删除和替换,最后通过commit()方法提交修改。
          
    添加,删除和替换Fragment
    使用FragmentTransaction的add方法可以添加一个新的Fragmentadd()方法的主要参数是Fragment的容器View(或其ID)及Fragment实例,例如:
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();  
    fragmentTransaction.add(R.id.ui_container, new MyListFragment());  
    fragmentTransaction.commit();  
    
    3.2/删除Fragment需要FragmentTransactionremove()方法,参数为Fragment对象,Fragment对象可以通过FragmentManagerfindFragmentById()方法获得。
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();  
    Fragment fragment = fragmentManager.findFragmentById(R.id.details_fragment);  
    fragmentTransaction.remove(fragment);  
    fragmentTransaction.commit();  
    
    3.3/替换Fragment使用的是FragmentTransactionreplace()方法,参数分别为所要替代Fragment所在容器的ID和新的FragmentFragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();  
    fragmentTransaction.replace(R.id.details_fragment, new DetailFragment(selected_index));  
    fragmentTransaction.commit();  
    
    4.获取指定的Fragment
    有两种方法可以获取某个特定的Fragment,如果这个Fragment已经被添加到某个layout文件中,则可以使用xml文件中的id作为参数:
    MyFragment myFragment = (MyFragment)fragmentManager.findFragmentById(R.id.MyFragment);  
    
    也可以通过创建Fragment时添加的tag获取特定的FragmentMyFragment myFragment = (MyFragment)fragmentManager.findFragmentByTag(MY_FRAGMENT_TAG); 
    
    5.Fragment和Back Stack
    Activity拥有Activity Stack,从而在用户按”返回”按钮时,回到前一个ActivityFragment也可以响应”返回”事件,方法是FragmentTransaction在commit之前调用addToBackStack()方法。这样,在用户按返回键后,Android会首先重现之前的UI布局。
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();  
    fragmentTransaction.add(R.id.ui_container, new MyListFragment());  
    Fragment fragment = fragmentManager.findFragmentById(R.id.details_fragment);  
    fragmentTransaction.remove(fragment);  
    String tag = null;  
    fragmentTransaction.addToBackStack(tag);  
    fragmentTransaction.commit();  
    原理和Activity类似,调用addToBackStack()后,Fragment会被push到back stack中,而不是销毁。
    
    6.Fragment Transaction的动画效果
    Fragment Transaction有两种方法实现动画效果,分别是:
    设置渐进:
    transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
    设置动画效果:
    fragmentTransaction.setCustomAnimations(R.animator.slide_in_left, R.animator.slide_out_right);  
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55

    注意点:

    add方法就是调用了fragmentmanager的添加方法;
    replace 则是先删除fragmentmanager中所有已添加的fragment中,容器id与当前要添加的fragment的容器id相同的fragment;然后再添加当前fragment; 
    即replace 会删除LinearLayout中所有fragment ,然后再添加传入fragment对象;
        
    通过add/show/hide方法替换Fragment,Fragment实例没有被销毁,状态仍保留,生命周期不受影响
    [参考](http://t.zoukankan.com/mingjie-c-p-12385997.html)
    
    如果我们在切换fragment的时候想要不保留之前的显示的状态,我们就可以把之前的fragment remove掉然后添加我们想要的新fragment。我觉得还是用replace比add+remove方便多了。
    但是有的时候我们想要保留之前的fragment状态,这个时候我们就不能去remove和add了,我们应该要show和hidden。
    [参考](https://www.freesion.com/article/6545927841/)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
  • 相关阅读:
    JavaScript面试常见问题(三)
    java计算机毕业设计河南口腔医疗机构线上服务系统源码+mysql数据库+系统+lw文档+部署
    基于VueCli创建自定义项目
    查看当前所有的数据库
    【2023年数学建模国赛】C题解题思路
    制作Windows下可移植Python环境和使用cx_Oracle
    【C刷题】day5
    【初学者入门C语言】之运算符及表达式(二)
    【Java】List、Set、数据结构、Collections
    Python中map、apply、applymap的用法
  • 原文地址:https://blog.csdn.net/weixin_44008788/article/details/126071806