观心静

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
  422 随笔 :: 0 文章 :: 86 评论 :: 139万 阅读

实现思路

1.写一个父类布局,里面写一个按键和一个帧布局(用于给Fragment布局后续替代

2.写3个子布局,并且在写3个class继承Fragment布局

3.在MainActivity的class中写替换碎片布局的方法

(包含:FragmentManger(碎片管理器)、getSupportFragmentManager(得到支持碎片管理器)、FragmenTransaction(碎片交换器)、beginTransaction(开始碎片交换)、replace(替换)、commit(交付))

写一个父类布局,里面写一个按键和一个帧布局

复制代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >
   <Button
       android:id="@+id/Button1"
       android:layout_width="match_parent"
       android:layout_height="0dp"
       android:layout_weight="1"
       android:text="改变下面的碎片"/>
    <FrameLayout
        android:id="@+id/FrameLayout1"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="8">
    </FrameLayout>


</LinearLayout>
复制代码

写3个子布局,并且在写3个class继承Fragment布局

复制代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="10dp">
    <ImageView
        android:id="@+id/fragment_1_ImageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:src="@drawable/ace"/>
    <TextView
        android:id="@+id/fragment_1_TextView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="@string/ace"
        android:textColor="@color/colorBlack"
        android:layout_marginTop="20dp"/>
</LinearLayout>
复制代码

相同布局在写2个

写一个class继承fragment实现碎片布局实例化

复制代码
package com.example.lenovo.myfragmentdemo2.fragment;
// 注意Fragment的依赖库建议统一使用support-v4库中的
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.example.lenovo.myfragmentdemo2.R;

/**
 * Created by lenovo on 2018/5/7.
 */

public class Fragment1 extends Fragment {
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_1,container,false);
        return view;
    }
}
复制代码

在MainActivity的class中写替换碎片布局的方法

复制代码
package com.example.lenovo.myfragmentdemo2;
// 注意Fragment的依赖库
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import com.example.lenovo.myfragmentdemo2.fragment.Fragment1;
import com.example.lenovo.myfragmentdemo2.fragment.Fragment2;
import com.example.lenovo.myfragmentdemo2.fragment.Fragment3;

public class MainActivity extends AppCompatActivity {
    private Button button;
    private int i = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button = (Button)findViewById(R.id.Button1);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switch (i){
                    case 1:
                        replaceFragment(new Fragment1());
                        i++;
                        break;
                    case 2:
                        replaceFragment(new Fragment2());
                        i++;
                        break;
                    case 3:
                        replaceFragment(new Fragment3());
                        i++;
                        break;
                    default:
                        replaceFragment(new Fragment1());
                        i=1;
                        break;
                }

            }
        });
    }

    public void  replaceFragment(Fragment fragment){
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction transaction = fragmentManager.beginTransaction();//注意!每次要执行commit()方法的时候都需要重新获取一次FragmentTransaction,否则用已经commit过的FragmentTransaction再次commit会报错!
        transaction.replace(R.id.FrameLayout1,fragment);
        transaction.commit();

    }
}
复制代码

 

运行效果:

点击后:

 

posted on   观心静  阅读(2714)  评论(0编辑  收藏  举报
编辑推荐:
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
点击右上角即可分享
微信分享提示