Fragmetn有关

来源:互联网 发布:如何安装c语言 编辑:程序博客网 时间:2024/06/06 09:04

第一行代码中的基础

静态用一个自定的fragment,rightfragment的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:id="@+id/tv_text"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="This is right fragment" /></LinearLayout>
rightfragment的代码把之前布局加载进来

public class right_fragment extends Fragment {    @Nullable    @Override    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {        View view = inflater.inflate(R.layout.right_fragment,container,false);        return view;    }}
MainActivity的xml布局,用定义好的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"    tools:context=".MainActivity">    <fragment        android:id="@+id/left_fragment"        android:name="com.example.sks.fragmenttest.activity.fragment.left_fragment"        android:layout_width="wrap_content"        android:layout_height="0dp"        android:layout_weight="1" />    <fragment        android:id="@+id/right_fragment"        android:name="com.example.sks.fragmenttest.activity.fragment.right_fragment"        android:layout_width="wrap_content"        android:layout_height="0dp"        android:layout_weight="1" /></LinearLayout>
动态添加碎片,left上按钮点击right的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"    tools:context=".MainActivity">    <fragment        android:id="@+id/left_fragment"        android:name="com.example.sks.fragmenttest.activity.fragment.Left_fragment"        android:layout_width="wrap_content"        android:layout_height="0dp"        android:layout_weight="1" />    <FrameLayout        android:id="@+id/fl_container"        android:layout_width="match_parent"        android:layout_height="0dp"        android:layout_weight="1">        <fragment            android:id="@+id/right_fragment"            android:name="com.example.sks.fragmenttest.activity.fragment.Right_fragment"            android:layout_width="match_parent"            android:layout_height="match_parent" />    </FrameLayout></LinearLayout>
点击跳,跟下面视屏里总结的一样

public void onClick(View v) {    Another_right_fragmetn another_right_fragmetn = new Another_right_fragmetn();    getFragmentManager().beginTransaction().replace(R.id.fl_container, another_right_fragmetn).commit();}



视频中的动态添加

**继承一个Fragment是导入support4下的,谨记
把一个activity根布局为FrameLayout,作为一个容器,往里面添加fragment
if(savedInstanceState==null){    getSupportFragmentManager().beginTransaction().add(R.id.container,new FragmentAAA()).commit();}
FragmentAAA()的onCreateView中的view的一个按钮
点击按钮跳转到另一个fragment中  getFragmentManager().beginTransaction().addToBackStack(null).replace(R.id.container,new FragmentBBB()).commit();
getFragmentManager().beginTransaction().addToBackStack(null)为点击后退键放回前一页面
getFragmentManager().popBackStack(); 为点击按钮主动返回前一页面

fragment为根布局,有两imageview,共三id
给根布局设置监听,如果a图在,显示b,否则显示a 实现点击切换图片


活动与碎片进行通信

在活动中得到碎片,用其中的方法,FragmentManager提供此方法

RightFragment rightFragment   = getFragmentManager().findFragmentById(R.id.right_fragment);

在碎片中获得活动实例,可直接调用getActivity()方法获取。有时用到Context也可以用此方法

MainActivity activity = (MainActivity)getActivity();

0 0
原创粉丝点击