14 fragment 创建

静态展示

注意 静态的开始进入界面的生命周期和动态的不同 详情:14 fragment注意点

  • 步骤一:创建一个类继承 Fragment

    • 代码类型一:

      package com.fmy.demo1;
      
      import android.app.Fragment;
      import android.graphics.Color;
      import android.os.Bundle;
      import android.view.LayoutInflater;
      import android.view.View;
      import android.view.View.OnClickListener;
      import android.view.ViewGroup;
      import android.widget.TextView;
      
      public class MyFragment extends Fragment {
          private TextView tv;
      
          @Override
          public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
              tv = new TextView(getActivity());
              tv.setText("我是一个fragment");
              tv.setOnClickListener(new OnClickListener() {
      
                  @Override
                  public void onClick(View v) {
                      tv.setTextColor(Color.rgb((int) (Math.random() * 256), (int) (Math.random() * 256),
                              (int) (Math.random() * 256)));
      
                  }
              });
              return tv;
          }
      }
      
      • 代码类型二(创建一个view填充):
      package com.fmy.demo2.fragment;
      
      import com.fmy.demo2.R;
      import com.fmy.demo2.UpDate;
      
      import android.annotation.TargetApi;
      import android.app.Activity;
      import android.app.Fragment;
      import android.content.Context;
      import android.os.Bundle;
      import android.view.LayoutInflater;
      import android.view.View;
      import android.view.View.OnClickListener;
      import android.view.ViewGroup;
      import android.widget.Button;
      import android.widget.EditText;
      import android.widget.TextView;
      import android.widget.Toast;
      
      public class MyFragment extends Fragment {
      
          private Button bt;
          private EditText et;
          private UpDate activity2;
      
          @TargetApi(23)
          @Override
          public void onAttach(Context context) {
              super.onAttach(context);
              activity2 = (UpDate) context;
              //注意API23以下 getContext 空指针
          }
      
          @Override
          public void onAttach(Activity activity) {
              // TODO Auto-generated method stub
              super.onAttach(activity);
              activity2 = (UpDate) activity;
          }
      
          @Override
          public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
      
              View view = inflater.inflate(R.layout.fragment01, container, false);
              Bundle bundle = getArguments();
              String string = bundle.getString("a");
              TextView tv = (TextView) view.findViewById(R.id.tv);
              et = (EditText) view.findViewById(R.id.et);
              bt = (Button) view.findViewById(R.id.bt);
              bt.setOnClickListener(new OnClickListener() {
      
                  @Override
                  public void onClick(View v) {
                      activity2.sendMessage(et.getText().toString());
                  }
              });
              tv.setText(string);
              return view;
          }
      }
      
      • xml文件
      <?xml version="1.0" encoding="utf-8"?>
      <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:orientation="vertical" >
      
          <TextView
              android:id="@+id/tv"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text="我是fragment" />
      
          <EditText
              android:id="@+id/et"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:hint="请输入回传" />
      
          <Button
              android:id="@+id/bt"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:text="回传" />
      
      </LinearLayout>
    • 步骤二 在界面的xml中写入fragment标签

      • id一定要写
      • name指向继承fragment的类
      <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:tools="http://schemas.android.com/tools"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:orientation="vertical" >
      
      
      <fragment 
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:id="@+id/f1"
          android:name="com.fmy.demo2.fragment.MyFragment"
          />
      
      
      </LinearLayout>

动态创建fragment

  • 步骤一:创建一个类继承 Fragment:
    • 和静态一模一样的 故略
  • 步骤二:在界面的xml设置一容器标签

    • 如下

      <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:tools="http://schemas.android.com/tools"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:orientation="vertical" >
      
      
      <!--以下作为容器  -->
          <LinearLayout
              android:id="@+id/ll"
              android:layout_width="match_parent"
              android:layout_height="300dp"
              android:orientation="horizontal" >
          </LinearLayout>
      
      </LinearLayout>
  • 步骤三:在界面的逻辑代码处填写代码

    • 如下

          //获得fragment管理器
              FragmentManager manager = getFragmentManager();
              //开启事务
              FragmentTransaction tarTransaction = manager.beginTransaction();
              //创建一个fragment对象
              MyFragment fragment = new MyFragment();
              //创建一个bundle 可以用于传值
              Bundle bundle = new Bundle();
      
              //放入数值到bundle
              bundle.putString("a", "aaa");
              //把bundle放入
              fragment.setArguments(bundle);
              //放入fragment 
              //第一个参数 布局界面填充容器 
              //第二个参数 fragment对象
              tarTransaction.add(R.id.ll,fragment);
              //第二种方法
              //transaction.replace(R.id.ll,fragment);
              //提交事务
              tarTransaction.commit();
原文地址:https://www.cnblogs.com/muyuge/p/6152254.html