Android Studio 1-4 Fragment 基础知识与高级进阶

一、Fragmen基础知识

package com.example.day04ex.activity;

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

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import com.example.day04ex.R;
import com.example.day04ex.fragment.BlankFragment;

public class MainActivity extends AppCompatActivity {

    private Button btn_show;
    private Button btn_AtoF;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btn_show = findViewById(R.id.btn_show);
        btn_AtoF = findViewById(R.id.btn_AtoF);
        btn_AtoF.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
            }
        });

        btn_show.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                FragmentManager supportFragmentManager = getSupportFragmentManager();
                //创建一个事务
                FragmentTransaction fragmentTransaction = supportFragmentManager.beginTransaction();
                BlankFragment fragment = new BlankFragment();
//                BlankFragment fragment1 = new BlankFragment();

                //添加一个
                fragmentTransaction.add(R.id.fl_show,fragment);
//                //移除一个
//                fragmentTransaction.remove(fragment);
//                //替换一个 fragment (碎片) 调用了前两个方法
//                fragmentTransaction.replace(R.id.fl_show,fragment1);
//
//                //展示一个布局
//                fragmentTransaction.show(fragment1);
//                //隐藏一个布局
//                fragmentTransaction.hide(fragment1);

                //提交事务
                fragmentTransaction.commit();
            }
        });

    }

}

二、Fragmen高级进阶

1、fragment布局

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

    <TextView
        android:id="@+id/tv_id"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:textColor="@color/colorPrimary"
        android:textSize="30sp"
        android:text="@string/hello_blank_fragment" />

</FrameLayout>

2、fragment代码

package com.example.day04ex.fragment;


import android.content.Context;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.example.day04ex.R;

import static android.content.ContentValues.TAG;

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

    private TextView textView;

    public BlankFragment() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View inflate = inflater.inflate(R.layout.fragment_blank, container, false);

        textView = inflate.findViewById(R.id.tv_id);
        //控件赋值
        Bundle arguments = getArguments();
        //初始为空 需要判断
        if(arguments != null){
            String key = arguments.getString("key");
            textView.setText(key);
        }
        return inflate;
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        Log.i(TAG, "onAttach: 被添加到 活动 中时回调");
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.i(TAG, "onCreate: 只会被调用一次");
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        Log.i(TAG, "onActivityCreated: 所在的 活动 启动完成之后回调");
    }

    @Override
    public void onStart() {
        super.onStart();
        Log.i(TAG, "onStart: ");
    }

    @Override
    public void onResume() {
        super.onResume();
        Log.i(TAG, "onResume: ");
    }

    @Override
    public void onPause() {
        super.onPause();
        Log.i(TAG, "onPause: ");
    }

    @Override
    public void onStop() {
        super.onStop();
        Log.i(TAG, "onStop: ");
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        Log.i(TAG, "onDestroyView: ");
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.i(TAG, "onDestroy: ");
    }

    @Override
    public void onDetach() {
        super.onDetach();
        Log.i(TAG, "onDetach: ");
    }
}

3、activity布局

<?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"
    android:gravity="center_horizontal"
    tools:context=".AtoF_Activity">

    <EditText
        android:id="@+id/et_id"
        android:hint="写一些东西进来呐"
        android:layout_width="300dp"
        android:layout_height="wrap_content" />

    <!--在activity中定义了点击事件-->
    <Button
        android:id="@+id/button_id"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="点我就对了"
        android:onClick="click"
        />

    <!--用来显示内容的,是个容器,里面可以放fragment-->
    <LinearLayout
        android:orientation="horizontal"
        android:id="@+id/linear_layout_id"
        android:gravity="center_horizontal"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </LinearLayout>

</LinearLayout>

4、activity代码

package com.example.day04ex;

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

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

import com.example.day04ex.fragment.BlankFragment;
import com.example.day04ex.fragment.Show_Fragment;

public class AtoF_Activity extends AppCompatActivity {

    private EditText editText;
    private FragmentManager fragmentManager;
    private FragmentTransaction fragmentTransaction;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_atof_);
        editText =  findViewById(R.id.et_id);

        fragmentManager = getSupportFragmentManager();
        fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.add(R.id.linear_layout_id,new BlankFragment());
        fragmentTransaction.commit();
    }

    public void click(View view) {
        fragmentManager = getSupportFragmentManager();
        fragmentTransaction = fragmentManager.beginTransaction();
        
        //取到输入的值
        String string = editText.getText().toString();
        BlankFragment blankFragment = new BlankFragment();
        Bundle bundle = new Bundle();
        bundle.putString("key",string);

        //给fragment对象赋值
        blankFragment.setArguments(bundle);

        //动态修改fragment
        fragmentTransaction.replace(R.id.linear_layout_id,blankFragment);
        fragmentTransaction.commit();
    }
}

发布了20 篇原创文章 · 获赞 4 · 访问量 907

猜你喜欢

转载自blog.csdn.net/v1141261428/article/details/98651760