Fragment的动态添加,Fragment的动态加载

Fragment的动态添加

效果展示



碎片的动态加载主要分为5步

1.创建碎片实例。

2.获取FragmentManager。

3.开启事务。

4.向容器中添加碎片。

5.提交事务。


代码演示

本实例中左侧为静态加载,右侧为动态添加

显示左侧的静态加载

left_fragment.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/colorAccent">
    <Button
        android:onClick="anotherRight"
        android:id="@+id/button"
        android:layout_gravity="center_horizontal"
        android:text="This is button in left_fragment"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</LinearLayout>

LeftFragment.class

public class LeftFragment extends Fragment {
//    重写onCreateView方法

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
//        此处数据类型为View,所以直接返回inflater即可
        return inflater.inflate(R.layout.left_fragment,container,false);
    }

}

右侧的动态添加

another_right_fragment.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorPrimary">
<TextView
    android:id="@+id/textview"
    android:layout_gravity="center_horizontal"
    android:text="这是动态添加的一个碎片s"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />
</LinearLayout>

AnotherRightFragment.class

public class AnotherRightFragment extends Fragment {
//    重写onCreateView方法

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
//        此处数据类型为View,所以直接返回inflater即可
        return inflater.inflate(R.layout.another_right_fragment,container,false);
    }
}
同样需要重写

只是在主活动中有所改变

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">

    <!--注意,fragment必须要有id,否则无法编译-->
    <fragment
        android:id="@+id/left_fragment"
        android:name="com.loser.fragmenttext.LeftFragment"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1" />

    <!--注意,这里不是fragment,是用来填充的一个布局-->
    <FrameLayout
        android:id="@+id/right_layout"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="0" />
</LinearLayout>

最主要的代码,在主活动中实现碎片的动态加载

MainActivity.class

public class MainActivity extends AppCompatActivity {
    FragmentManager fragmentManager;
    FragmentTransaction fragmentTransaction;
    FrameLayout frameLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

//    左边碎片中的按钮绑定的事件
    public void anotherRight(View v){
//         FragmentManager
        fragmentManager = getSupportFragmentManager();
//        通过begin开启事务
        fragmentTransaction = fragmentManager.beginTransaction();
//        使用replace向容器内添加碎片
        fragmentTransaction.replace(R.id.right_layout,new AnotherRightFragment());
//        将事务添加到返回栈中
        fragmentTransaction.addToBackStack(null);
//        拿到FrameLayout以便在设置其大小
        frameLayout = (FrameLayout)findViewById(R.id.right_layout);
//        重新设置右碎片的布局
        frameLayout.setLayoutParams(new LinearLayout.LayoutParams(0,MATCH_PARENT, 2.0f));
//        提交事务
        fragmentTransaction.commit();
//        土司一下,证明你点击有效
        Toast.makeText(MainActivity.this,"你点击了按钮",Toast.LENGTH_SHORT).show();
    }
}

此处

//        将事务添加到返回栈中
        fragmentTransaction.addToBackStack(null);

是当用户按下Back后就直接退出活动了,而如上设置为null则返回到上一个碎片。

至此,动态添加碎片的基本方法已经结束。

返回主目录

猜你喜欢

转载自blog.csdn.net/qq_38376757/article/details/79857598