源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

Android学习之Fragment

  • 时间:2022-04-14 07:11 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:Android学习之Fragment
[b]Fragment 是什么[/b] 碎片(Fragment)是一种可以嵌入在活动(activity)当中的 UI 片段。 [b]一、碎片的简单用法[/b] 创建两个布局文件:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <Button
      android:id="@+id/button"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_gravity="center_horizontal"
      android:text="Button"
      />
  </LinearLayout>
//left_fragment.xml
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#00ff00"
    android:orientation="vertical" >
    <TextView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_gravity="center_horizontal"
      android:textSize="20sp"
      android:text="This is right fragment"
      />
  </LinearLayout>

//right_fragment.xml
所有的自定义 Fragment 都需要继承 Fragment 类:
public class LeftFragment extends Fragment {
  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
  View view = inflater.inflate(R.layout.left_fragment, container, false); return view;
  }
}
public class RightFragment extends Fragment {
  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
  View view = inflater.inflate(R.layout.right_fragment, container, false); return view;
  }
}
最后定义 activity_main.xml
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
    <fragment
      android:id="@+id/left_fragment"
      android:name="com.example.fragmenttest.LeftFragment"
      android:layout_width="0dp"
      android:layout_height="match_parent"
      android:layout_weight="1" />
    <fragment
      android:id="@+id/right_fragment"
      android:name="com.example.fragmenttest.RightFragment"
      android:layout_width="0dp"
      android:layout_height="match_parent"
      android:layout_weight="1" />
  </LinearLayout>
[b]二、动态添加碎片[/b] 可以在代码当中动态添加碎片:
@Override
    public void onClick(View v) {
      switch (v.getId()) {
      case R.id.button:
        AnotherRightFragment fragment = new AnotherRightFragment();
        FragmentManager fragmentManager = getFragmentManager();
        FragmentTransaction transaction = fragmentManager.
  beginTransaction();
        transaction.replace(R.id.right_layout, fragment);
        transaction.commit();
        break;
      default:
        break;
  } }
动态添加碎片主要分为 5 步。 1、创建待添加的碎片实例。 2、获取到 FragmentManager,在活动中可以直接调用 getFragmentManager()方法得到。 3、开启一个事务,通过调用 beginTransaction()方法开启。 4、向容器内加入碎片,一般使用 replace()方法实现,需要传入容器的 id 和待添加的碎片实例。 5、提交事务,调用 commit()方法来完成。 [b]三、在碎片中模拟返回栈[/b] 通过点击按钮添加了一个碎片之后,这时按下 Back 键程序就会直接退出。
 public class MainActivity extends Activity implements OnClickListener {
......
    @Override
    public void onClick(View v) {
      switch (v.getId()) {
      case R.id.button:
        AnotherRightFragment fragment = new AnotherRightFragment();
        FragmentManager fragmentManager = getFragmentManager();
        FragmentTransaction transaction = fragmentManager.
  beginTransaction();
        transaction.replace(R.id.right_layout, fragment);
        transaction.addToBackStack(null);
        transaction.commit();
        break;
      default:
  break; }
  }
}
[b]四、碎片和活动之间进行通信[/b] 为了方便碎片和活动之间进行通信,FragmentManager 提供了一个类似于 findViewById() 的方法,专门用于从布局文件中获取碎片的实例,代码如下所示:
  RightFragment rightFragment = (RightFragment) getFragmentManager()
      .findFragmentById(R.id.right_fragment);
在每个碎片中都可以通过调用getActivity() 方法来得到和当前碎片相关联 的活动实例,代码如下所示:
  MainActivity activity = (MainActivity) getActivity();
[b]五、碎片的生命周期[/b] 运行状态:当一个碎片是可见的,并且它所关联的活动正处于运行状态时,该碎片也处于运行状态。 暂停状态:当一个活动进入暂停状态时(由于另一个未占满屏幕的活动被添加到了栈顶),与它相关联的可见碎片就会进入到暂停状态。 停止状态:当一个活动进入停止状态时,与它相关联的碎片就会进入到停止状态。 销毁状态:碎片总是依附于活动而存在的,因此当活动被销毁时,与它相关联的碎片就会进入 到销毁状态。 下面是碎片的一些回调方法: [list] [*][b]onAttach() 当碎片和活动建立关联的时候调用。 [/b][/*] [*][b]onCreateView() 为碎片创建视图(加载布局)时调用。 [/b][/*] [*][b]onActivityCreated() 确保与碎片相关联的活动一定已经创建完毕的时候调用。 [/b][/*] [*][b]onDestroyView() 当与碎片关联的视图被移除的时候调用。 [/b][/*] [*][b]onDetach() 当碎片和活动解除关联的时候调用。[/b][/*] [/list] 以上就是关于Android中碎片Fragment的全部内容,希望对大家的学习有所帮助。
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部