Fragment学习

这篇博客介绍了如何在Android中使用Android Studio(as)创建和管理Fragment,包括新建两个Fragment及展示它们的Activity。还提到了运行效果以及Fragment之间参数传递的教程链接。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

官方教程 https://developer.android.com/training/basics/fragments/index.html

使用as新建两个空的fragment

这里写图片描述

新建一个activity用于显示fragment

package com.example.h.learn.activity;

import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.view.View;

import com.example.h.learn.R;
import com.example.h.learn.fragment.BlankFragment1;
import com.example.h.learn.fragment.BlankFragment2;

public class FragmentActivity extends AppCompatActivity  implements View.OnClickListener, BlankFragment1.OnFragmentInteractionListener, BlankFragment2.OnFragmentInteractionListener{

    //必须实现此方法,否则会报错
    @Override
    public void onFragmentInteraction(Uri uri) {

    }

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

        int[] ids = new int[]{R.id.btn_fragment1, R.id.btn_fragment2};

        for (int id:ids
             ) {
            findViewById(id).setOnClickListener(this);
        }

        if(findViewById(R.id.fragment_layout)!=null){

            //避免fragment重叠
            if(savedInstanceState !=null){
                return;
            }
            //第一次创建,默认显示第一个fragment
            BlankFragment1 itemFragment = new BlankFragment1();
            //传参数
            itemFragment.setArguments(getIntent().getExtras());

            getSupportFragmentManager().beginTransaction().add(R.id.fragment_layout, itemFragment).commit();
        }
    }


    @Override
    public void onClick(View v) {
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        switch (v.getId()){
            //显示第一个fragment
            case R.id.btn_fragment1:


                transaction.replace(R.id.fragment_layout, new BlankFragment1());
                transaction.addToBackStack(null);
                transaction.commit();

                break;
            //显示第二个fragment
            case R.id.btn_fragment2:
                transaction.replace(R.id.fragment_layout, new BlankFragment2());
                transaction.addToBackStack(null);
                transaction.commit();
                break;
            default:
        }
    }
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.h.learn.activity.FragmentActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <Button
            android:text="fragment1"
            android:id="@+id/btn_fragment1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
        <Button
            android:text="fragment2"
            android:id="@+id/btn_fragment2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </LinearLayout>
    <FrameLayout
        android:id="@+id/fragment_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"></FrameLayout>
</LinearLayout>

运行效果
这里写图片描述

这里写图片描述

fragment传参 http://dditblog.com/itshare_296.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值