【Android之本菜鸡的毕设】第二节 fragment的简单使用

0.开头

我在毕设中,希望通过点击切换底部菜单栏改变显示内容,意思是这样子的:
fragment使用示意图
所以先测试一下fragment的使用。

1.简单介绍一下fragment

  • Fragment在Android3.0中引入,设计初衷是适应大屏幕的平板电脑;
  • 我们可以把他看成一个小型的Activity,又称Activity片段;
  • 使用Fragment可以把屏幕划分成几块,然后进行分组,进行模块化管理;
  • 使用它可以更加方便的在运行过程中动态地更新Activity的用户界面。
  • 参考http://www.runoob.com/w3cnote/android-tutorial-fragment-base.html

2.代码编写思路

此次要实现的目标,并不是开头说的底部栏切换主界面,而是这样的:
fragment测试实现效果

  • 首先,创建编写主界面布局:activity_main.xml,如上图;并加载到MainActivity.java中;
  • 其次,编写fragment_demo1.xml和fragment_demo2.xml;并对应加载到FragmentDemo1.java和FragmentDemo2.java中;
  • 最后,在MainActivity.java中为按钮设置监听事件,点击实现fragment的动态替换。

3.对应代码及运行情况

activity_main.xml

<?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:background="#7003A9F4"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_gravity="center"
        android:layout_marginTop="50dp"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:layout_marginBottom="20dp"
        android:background="#52226883"
        >
        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            android:layout_marginTop="50dp"
            android:layout_marginBottom="50dp"
            android:layout_marginLeft="50dp"
            android:layout_marginRight="50dp"
            android:textSize="30sp"
            android:text="按钮1"
            />

        <Button
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            android:layout_marginTop="50dp"
            android:layout_marginBottom="50dp"
            android:layout_marginRight="50dp"
            android:textSize="30sp"
            android:text="按钮2"
            />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/linearlayout_fragment"
        android:layout_width="340dp"
        android:layout_height="428dp"
        android:layout_gravity="center"
        android:layout_margin="20dp"
        android:layout_marginTop="50dp"
        android:background="#57000000"
        android:orientation="horizontal">


    </LinearLayout>

</LinearLayout>

fragment_demo1.xml

<?xml version="1.0" encoding="utf-8"?>
<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"
    android:gravity="center"
    tools:context=".FragmentDemo1">

    <TextView
        android:id="@+id/textview_fragment1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="11111Fragment11111"
        android:textSize="30sp" />

</LinearLayout>

fragment_demo2.xml

<?xml version="1.0" encoding="utf-8"?>
<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"
    android:gravity="center"
    tools:context=".FragmentDemo2">

    <TextView
        android:id="@+id/textview_fragment2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="22222Fragment22222"
        android:textSize="30sp" />

</LinearLayout>

FragmentDemo1.java

package com.yh.fragmenttest;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

/**
 * A simple {@link Fragment} subclass.
 */
public class FragmentDemo1 extends Fragment {
    
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_demo1, container, false);
    }

}

FragmentDemo2.java

package com.yh.fragmenttest;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

/**
 * A simple {@link Fragment} subclass.
 */
public class FragmentDemo2 extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_demo2, container, false);
    }
}

MainActivity.java

package com.yh.fragmenttest;

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 android.widget.LinearLayout;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{
    private Button bt1;
    private Button bt2;
    private LinearLayout linearLayoutFragment;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        bt1.setOnClickListener(this);
        bt2.setOnClickListener(this);
    }

    //初始化组件
    private void initView(){
        bt1 = findViewById(R.id.button1);
        bt2 = findViewById(R.id.button2);
        linearLayoutFragment = findViewById(R.id.linearlayout_fragment);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.button1:
                //创建碎片fragment实例
                FragmentDemo1 fragmentDemo1 = new FragmentDemo1();
                //获取fragment manager
                FragmentManager fragmentManager = getSupportFragmentManager();
                //开启一个事务
                FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
                //用replace方法向容器中添加碎片fragment
                fragmentTransaction.replace(R.id.linearlayout_fragment,fragmentDemo1);
                //提交事务
                fragmentTransaction.commit();
                break;
            case R.id.button2:
                FragmentDemo2 fragmentDemo2 = new FragmentDemo2();
                FragmentManager fragmentManager2 = getSupportFragmentManager();
                FragmentTransaction fragmentTransaction2 = fragmentManager2.beginTransaction();
                fragmentTransaction2.replace(R.id.linearlayout_fragment,fragmentDemo2);
                fragmentTransaction2.commit();
                break;
            default:
                break;
        }
    }
}

运行情况:
点击按钮1
点击按钮2

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值