Android开发Fragment的使用学习

基本概念

Fragment是Android3.0(API11)提出的概念,support-v4库中也开发了一套Fragment API,最低兼容Android 1.6。所以在开发中要注意不要导错包
导入support-v4 compile 'com.android.support:support-v4:24.2.1
导入support-fragment com.android.support:support-fragment:24.2.1

  • 一个Activity可以有多个Fragment
  • Frament可以被多个Activity重用
  • 它有自己的生命周期,可以接收监听事件
  • 在运行Activity时可以被删除、替代。

生命周期

底部导航Demo

1、编写activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
    tools:context=".MainActivity">

    <fragment
        android:id="@+id/fragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:name="com."></fragment>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:orientation="horizontal"
        android:layout_alignParentBottom="true">
        <ImageView
            android:id="@+id/btn1"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:src="@mipmap/icon1"
            android:layout_weight="1"
            />
        <ImageView
            android:id="@+id/btn2"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:src="@mipmap/icon2"
            android:layout_weight="1"
            />
        <ImageView
            android:id="@+id/btn3"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:src="@mipmap/icon3"
            android:layout_weight="1"
            />
        <ImageView
            android:id="@+id/btn4"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:src="@mipmap/icon4"
            android:layout_weight="1"
            />
    </LinearLayout>

</RelativeLayout>

2、一式4份准备fragment1_activity.xml的布局文件(背景和文件名改下)

<?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:background="@drawable/f1">
</LinearLayout>

3、Fragment_one.java文件一式4份,对于上一步的fragment布局

package com.example.fragmenttest;
import androidx.fragment.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.Nullable;

public class Fragment_one extends Fragment {
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment1_activty,container,false);
    }
}

4、编写MainActivty.java

package com.example.fragmenttest;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity {

    ImageView btn1;
    ImageView btn2;
    ImageView btn3;
    ImageView btn4;

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

    private void initView() {
        btn1 = findViewById(R.id.btn1);
        btn2 = findViewById(R.id.btn2);
        btn3 = findViewById(R.id.btn3);
        btn4 = findViewById(R.id.btn4);

        btn1.setOnClickListener(l);
        btn2.setOnClickListener(l);
        btn3.setOnClickListener(l);
        btn4.setOnClickListener(l);
    }

    View.OnClickListener l =  new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            FragmentManager fm = getSupportFragmentManager();
            FragmentTransaction ft = fm.beginTransaction();
            Fragment f = null;
            switch (v.getId()){
                case R.id.btn1:
                    f =  new Fragment_one();
                    break;
                case R.id.btn2:
                    f =  new Fragment_two();
                    break;
                case R.id.btn3:
                    f =  new Fragment_three();
                    break;
                case R.id.btn4:
                    f =  new Fragment_four();
                    break;
                default:
                    break;
            }
            ft.replace(R.id.fragment,f);
            ft.commit();
        }
    };
}

效果:

猜你喜欢

转载自www.cnblogs.com/lzpq/p/12767805.html
今日推荐