android 开发 碎片Fragment布局例子(用按键切换碎片布局)

实现思路:

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

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

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

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


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

<?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>

2.写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;
    }
}

3.在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();
        transaction.replace(R.id.FrameLayout1,fragment);
        transaction.commit();

    }
}


运行效果:

点击后:

  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Fragment表现Activity中用户界面的一个行为或者是一部分。你可以在一个单独的activity上把多个fragment组合成为一个多区域的UI,并且可以在多个activity中再使用。你可以认为fragment是activity的一个模块零件,它有自己的生命周期,接收它自己的输入事件,并且可以在activity运行时添加或者删除。 Fragment必须总是被嵌入到一个activity之中,并且fragment的生命周期直接受其宿主activity的生命周期的影响。例如,一旦activity被暂停,它里面所有的fragment也被暂停,一旦activity被销毁,它里面所有的fragment也被销毁。然而,当activity正在运行时(处于resumed的生命周期状态),你可以单独的操控每个fragment,比如添加或者删除。当你执行这样一项事务时,可以将它添加到后台的一个栈中,这个栈由activity管理着——activity里面的每个后台栈内容实体是fragment发生过的一条事务记录。这个后台栈允许用户通过按BACK键回退一项fragment事务(往后导航)。 当你添加一个fragment作为某个activity布局的一部分时,它就存在于这个activity视图体系内部的ViewGroup之中,并且定义了它自己的视图布局。你可以通过在activity布局文件中声明fragment,用<fragment>元素把fragment插入到activity的布局中,或者是用应用程序源码将它添加到一个存在的ViewGroup中。然而,fragment并不是一个定要作为activity布局的一部分;fragment也可以为activity隐身工作。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值