android学习笔记之Fragment(三)

写的只是我的的理解,但愿有错大神们能指出来。 java

经过Fragments来构建你的动态Activity布局: android

预先构建出模型,like this:
<?xml version=”1.0” encoding=”utf-8”?>
<LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android”
   android:orientation=”horizontal”
   android:layout_width=”match_parent”
   android:layout_height=”match_parent”>
   <FrameLayout
     android:id=”@+id/ui_container
     android:layout_width=”match_parent”
     android:layout_height=”match_parent”
     android:layout_weight=”1”
   />
   <FrameLayout
     android:id=”@+id/details_container
     android:layout_width=”match_parent”
     android:layout_height=”match_parent”
     android:layout_weight=”3”
   />
</LinearLayout> ide

这是某个activity的布局,一个大容器里面包含了2个小容器,那么小容器什么用处呢? 模块化

为了动态在程序中添加fragment,预先先构建模型,到时候动态填充。(以前咱们曾提到容器,其实本质就是Layout)。这样一个坑位就对应一个fragment,这也就是为何咱们FragmentManager它的方法中像add、replace都会涉及到一个容器id,实际上是一个思想,先构建蓝图,而后不一样模块也就是不一样容器放不一样的fragment,而后每一个容器又能够随时更换本身的fragment,这样就实现了动态机制。 布局

 

Fragments 和 Back Stack (后台的栈) 学习

咱们知道操做事务能够动态改变fragments,从而改变整个UI,那么既然涉及到了事务,从SQL学习咱们知道事务通常都有回滚的机制,那么fragment的事务也有吗? 动画

答案是确定的,情形好比你在用户交互中经过事务改变了界面,而后用户又但愿回退回上个界面,那么只要在commit以前调用addToBackStack就能够将这个事务保存起来,而后按回退按钮就能够回退了。 (~。~) ui

例子:
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); this

fragmentTransaction.add(R.id.ui_container, new MyListFragment()); 线程

Fragment fragment = fragmentManager.findFragmentById(R.id.details_fragment);
fragmentTransaction.remove(fragment);

String tag = null; 
ragmentTransaction.addToBackStack(tag); //commit以前哦!

fragmentTransaction.commit();

这样在你回退的时候,MyListFragment会被移除,DetailFragment会被重启恢复。


注意:很是注意:若是你的fragment是在xml中直接定义的,你是没法正常使用一些事务的(好比回退什么的),因此用事务你的fragment必须用事物增长进来的。

按照个人话来讲,从哪里来就从哪里回去。

如何使用Fragment的过分动画,又如何自定义本身的动画?
要使用默认的动画,须要使用FragmentTransaction的方法setTransition方法,传递FragmentTransaction.TRANSIT_FRAGMENT_*,就像FragmentTransaction.TRANSIT_FRAGMENT_OPEN.

例子:transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);

你也能够自定义本身的过分动画,使用setCustomAnimations()方法,里面传递2个XML动画资源,第一个参数的动画是在事务加入fragment的时候,第二个参数的动画是在事务移除或者替换了某个fragment时触发。

例子:fragmentTransaction.setCustomAnimations(R.animator.slide_in_left, R.animator.slide_out_right);

注意:animator是3.0引入的(API Level 11)动画,3.0以后的应该使用animator(Property Animation)这个,3.0以前support library 的只能用之前的动画资源。


Fragments 和 Activities之间的接口:

在Fragment类中,你可使用getActivity即可得到与其绑定的activity。这对找到当前的上下文对象(context)很是有用,能够访问其余的fragments经过FragmentManager,还能够找到层次布局中的某个View.

TextView textView = (TextView)getActivity().findViewById(R.id.textview);

可能有种需求,如何让你的fragment与activity共享事件呢?看例子

//这是某个fragment类的片断: 经过定义接口,让后让activity去实现这个接口,fragment这边在onAttach的时候获取到activity的引用,而后经过多态能够将其转化为接口对象,而后你就能够作共享事件的事儿了。

public interface OnSeasonSelectedListener { 
  public void onSeasonSelected(Season season); 
}

private OnSeasonSelectedListener onSeasonSelectedListener; 
private Season currentSeason;

@Override 
public void onAttach(Activity activity) { 
  super.onAttach(activity);

  try { 
     onSeasonSelectedListener = (OnSeasonSelectedListener)activity; 
  } catch (ClassCastException e) { 
     throw new ClassCastException(activity.toString() + 
                 “ must implement OnSeasonSelectedListener”); 
  } 
}

private void setSeason(Season season) { 
  currentSeason = season; 
  onSeasonSelectedListener.onSeasonSelected(season); 
}

没有布局的Fragments的使用:

在绝大多数状况,fragments仍是用来分装模块化的UI设计而生的,然而你也能够建立一个没有UI的fragment,用来提供后台的行为(好比保存activity的状态。(当配置发生改变形成activity重启)).

下面给出些代码片断:

public class NewItemFragment extends Fragment { 
   @Override 
   public void onAttach(Activity activity) { 
     super.onAttach(activity);

     //获取activity的引用。 
   }

   @Override 
   public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState);

    // 建立后台工做线程和任务。 
   }

   @Override 
   public void onActivityCreated(Bundle savedInstanceState) { 
     super.onActivityCreated(savedInstanceState);

     // 初始化工做线程和任务 
   } 
}


FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

fragmentTransaction.add(workerFragment, MY_FRAGMENT_TAG);  //由于没有UI因此不能指定父容器ID,只能指定tag了。

fragmentTransaction.commit();

 

其它的Fragments: (这些暂时不讨论,是否是很像activity的扩展呢?)
1.DialogFragment

2.ListFragment

3.WebViewFragment

相关文章
相关标签/搜索