Fragment挂载进Activity

来源:互联网 发布:雅思6 知乎 编辑:程序博客网 时间:2024/06/07 14:52

Fragment挂载进Activity中有两个方法

  • 在xml布局文件里直接声明出来
  • 用Fragment的事物动态添加

1.在xml布局文件中添加:

1.1 代码实现:

先把Fragment准备好:

public class TitleFragment extends Fragment implements View.OnClickListener {    @Nullable    @Override    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {        //Fragment具体显示的视图        View view = View.inflate(UiUtils.getContext(), R.layout.xml_title,null);        return view;    }

public class ContentFragment extends Fragment {    private TextView mContent;    @Nullable    @Override    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {        View view = View.inflate(UiUtils.getContext(), R.layout.xml_content,null);        return view;    }

布局文件内容:

  • name属性:代表具体是添加哪一个Fragment,类似于自定义控件时,更改标签
  • tools:属性:使Fragment的内容可以直接显示在andoird studio的阅览窗口
<fragment            android:layout_centerInParent="true"            android:id="@+id/fragment_content"                                                                                                                                     android:name="com.itheima.fragmentevent.xml_fragment.ContentFragment"            android:layout_width="match_parent"            android:layout_height="match_parent"            tools:layout="@layout/xml_content">        </fragment>    <fragment        android:id="@+id/fragment_title"        android:name="com.itheima.fragmentevent.xml_fragment.TitleFragment"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        tools:layout="@layout/xml_title"        android:layout_alignParentTop="true"        >    </fragment>

1.2具体效果如下:
这里写图片描述

上面四个按钮是TitleFragment
下面的内容是ContentFragment


使用事物动态添加:

  • 在布局文件里声明一个FrameLayout,使用TitleFragment的按钮,动态添加Fragment进FrameLayout
<RelativeLayout    xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:id="@+id/activity_main"    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:context="com.itheima.fragmentevent.MainActivity">    <!--TitleFragment-->    <fragment        android:id="@+id/fragment_title"        android:name="com.itheima.fragmentevent.xml_fragment.TitleFragment"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        tools:layout="@layout/xml_title"        android:layout_alignParentTop="true"        android:layout_alignParentLeft="true"        android:layout_alignParentStart="true">    </fragment>    <RelativeLayout        android:layout_below="@id/fragment_title"        android:layout_width="match_parent"        android:layout_height="match_parent">        <!--ContentFragment-->        <fragment            android:layout_centerInParent="true"            android:id="@+id/fragment_content"            android:name="com.itheima.fragmentevent.xml_fragment.ContentFragment"            android:layout_width="match_parent"            android:layout_height="match_parent"            tools:layout="@layout/xml_content">        </fragment>        <!--将动态添加的Fragment覆盖ContentFragment-->        <FrameLayout            android:id="@+id/fl_blank"            android:layout_width="match_parent"            android:layout_height="match_parent">        </FrameLayout>        <!--测试Activity和Fragment之间的信息传递-->        <Button            android:id="@+id/but_send"            android:onClick="sendMassage"            android:layout_alignParentBottom="true"            android:layout_centerHorizontal="true"            android:text="发送消息"            android:layout_width="wrap_content"            android:layout_height="wrap_content"/>    </RelativeLayout></RelativeLayout>

2.2在Activity里可以拿到TitleFragment视图对应的View对象,也就可以拿到四个Button,通过Button动态添加新的Fragment

  • 拿到FragmentManager
  • 开启Fragment的事物
  • 将新的Fragment替换占位的FrameLayout
public class MainActivity extends AppCompatActivity  {    private Button mBut01;    private Button mBut02;    private Button mBut03;    private Button mBut04;    private FragmentTransaction mTransaction;    private View view;    private FragmentManager mFragmentManager;    private View mContent;    private Button mSendMassage;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        initView();        initListen();        initFragment();    }    private void initFragment() {        //拿到Fragment管理器        mFragmentManager = getSupportFragmentManager();    }    private void initListen() {        mBut01.setOnClickListener(this);        mBut02.setOnClickListener(this);        mBut03.setOnClickListener(this);        mBut04.setOnClickListener(this);    }    private void initView() {        mSendMassage = (Button) findViewById(R.id.but_send);        view = findViewById(R.id.fragment_title);        mContent = findViewById(R.id.fragment_content);        mBut01 = (Button) view.findViewById(R.id.fragment01);        mBut02 = (Button) view.findViewById(R.id.fragment02);        mBut03 = (Button) view.findViewById(R.id.fragment03);        mBut04 = (Button) view.findViewById(R.id.fragment04);    }    @Override    public void onClick(View v) {        //开启事物        mTransaction = mFragmentManager.beginTransaction();        mContent.setVisibility(View.INVISIBLE);        switch (v.getId()) {            case R.id.fragment01:                //通过事物将新的Fragment替换FrameLayout                mTransaction.replace(R.id.fl_blank,new Fragment01(),"frag01");                break;            case R.id.fragment02:                mTransaction.replace(R.id.fl_blank,new Fragment02());                break;            case R.id.fragment03:                mTransaction.replace(R.id.fl_blank,new Fragment03());                break;            case R.id.fragment04:                mTransaction.replace(R.id.fl_blank,new Fragment04());                break;        }        mTransaction.commit();    }

效果如下:
这里写图片描述

0 0
原创粉丝点击