您的位置:首页 > 移动开发 > Android开发

Android Fragment全攻略

2015-12-14 10:30 417 查看
4000




转:http://wobushi.ren/android-fragment.html


Android Fragment全攻略

Fragment—直译“片段”,顾名思义,有自己独立的生命周期,子事件,是组成Android复杂ui必不可少的元素,本文将总结一些个人工作中使用fragment的一些tips


兼容篇

v4一般与v7联动使用,就一起总结了吧。


1.引用:

1234dependencies { compile 'com.android.support:appcompat-v7:+' compile 'com.android.support:support-v4:+'}
当然,这里使用的是最新版本的,实际情况如果有需要考虑到其他兼容问题的可以用于api level对应的版本。

2.Theme:

1

2

3

4

5

<stylename="AppTheme"parent="Theme.AppCompat.Light.DarkActionBar">

<itemname="colorPrimary">@color/primary</item>

<itemname="colorPrimaryDark">@color/primary_dark</item>

<itemname="colorAccent">@color/accent</item>

</style>

tips:如果要使用toolbar来替代传统的Actionbar,那么你必须继承NoActionBar:

12<style name="AppTheme" parent="Theme.AppCompat.NoActionBar">......
然后在代码中SetSupportActionBar为你的toolbar

3.code:

1

2

3

publicclassYourActivityextendsAppCompatActivity{

//......

}


4.getActionBar() -> getSupportActionBar() / setActionBar() -> setSupportActionBar()

使用v7包中的set /get SupportActionBar而不是原level api中的。

或者为了多向兼容你也可以:

1234567if (getActivity() instanceof ActionBarActivity) { ActionBar actionBar = ((ActionBarActivity) getActivity()).getSupportActionBar(); if (actionBar != null) { // ... }}

5.xml android:xxx -> app:xxx

比如showAsAction,你需要使用app:前缀而不是android前缀来进行兼容性加载

1

2

3

4

<itemandroid:id="@+id/action_search"

android:icon="@drawable/icon_search"

app:showAsAction="ifRoom"

android:title="@string/search"/>


6.set/get fragmentTransaction() -> set/get supportFragmentTransaction()


性能篇

在Fragment的切换中,有些场景是需要fragment重新创建,以便刷新其内容。而有些静态的fragment互相切换,或者在内部的组件与逻辑中已经布置好的情况下,我们不需要他重新创建,只需要从内存中拿出来再次使用即可。实际情况中,肯定也是后者居多。


1.我们可以通过在onCreateView中缓存已经加载过的view来避免其多次被重新创建。

1234567891011121314private View fragmentView = null; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { if (fragmentView == null) { fragmentView = LayoutInflater.from(context).inflate( R.layout.fragment_main, null); } ViewGroup parent = (ViewGroup) fragmentView.getParent(); if (parent != null) { parent.removeView(fragmentView); } return fragmentView; }

2.我们可以通过add/show/hide方法来进行fragment的切换而不是replace

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

publicvoidswitchFragment(FragmentdestFragment,Stringtag){

FragmentTransactionft=getSupportFragmentManager().beginTransaction().setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);

if(currentFragment==null){

ft.add(R.id.content_fl,destFragment,tag);

}elseif(currentFragment!=destFragment){

if(destFragment.isAdded()){

ft.hide(currentFragment).show(destFragment);

}else{

ft.hide(currentFragment).add(R.id.content_fl,destFragment,tag);

}

}

ft.commit();

currentFragment=destFragment;

}

两者都可以在某些情况下避免重复加载浪费硬件资源的事情发生,不过个人倾向于后者,把需要动态的部分封装到各个fragment的内部,而控件只需要创建一次。


3.BackStack的适时使用

如果并非navigationDrawer创建的结构,想要方便的返回fragment就要用到backstack了。

在适当的时候加入后退栈:

12345678910111213private void replaceFragment (Fragment fragment){ String backStateName = fragment.getClass().getName(); FragmentManager manager = getSupportFragmentManager(); boolean fragmentPopped = manager.popBackStackImmediate (backStateName, 0); if (!fragmentPopped){ //fragment不在堆栈中,创建. FragmentTransaction ft = manager.beginTransaction(); ft.replace(R.id.content_frame, fragment); ft.addToBackStack(backStateName); ft.commit(); }}
在你需要再次返回他的时候,使用
popBackStackImmediate
()即可。

UI篇

1.在xml中使用fragment

对于一些简单的fragment无需用代码控制,xml即可

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

<?xmlversion="1.0"encoding="utf-8"?>

<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"

android:orientation="horizontal"

android:layout_width="match_parent"

android:layout_height="match_parent">

<fragmentandroid:name="com.example.news.ArticleListFragment"

android:id="@+id/list"

android:layout_weight="1"

android:layout_width="0dp"

android:layout_height="match_parent"/>

<fragmentandroid:name="com.example.news.ArticleReaderFragment"

android:id="@+id/viewer"

android:layout_weight="2"

android:layout_width="0dp"

android:layout_height="match_parent"/>

</LinearLayout>

好处是可以在预览中直观的看到ui的布局情况。


2.fragment切换动画

fragment简单的切换动画可以通过编写xml实现。

这个项目就为我们编写好了很多种酷炫的切换动画,为作者鼓掌(啪啪啪)。

当然Android5.0以后添加了新的Activity动画,比如照片的缩放,共享组件等,只是暂时不兼容之前的版本。

https://github.com/DesarrolloAntonio/FragmentTransactionExtended

这个项目可以把他搬到5.0之前的版本使用,但不能与api中其他方法,比如共享组件进行联动,不是很方便,不过之追求ui效果的话倒是可以达到。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: