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);
}

activity 用fragment中方法

Fragment FragmentManager fragmentManager = getFragmentManager();
// 布局中fragment对应的id
Fragment fragment = fragmentManager.findFragmentById(R.id.--); 
// 或者通过tag标记查找
Fragment fragment2 = fragmentManager.findFragmentByTag(tag);

注意:在获取到fragment之后,调用getView方法是要明确改fragment是否已构建完布局,否则将会报空指针异常。

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>

切换颜色

<?xml version="1.0" encoding="utf-8"?>
<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>

切换:通过 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);
            }
        });

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);  

注意点:

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/)
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值