四大组件随笔-生活工作点滴

017_Android Fragment 碎片

2019-07-09  本文已影响68人  wakawakai

Fragment的继承关系


Fragment的继承关系.png

Fragment的实现方式

  1. 创建一个类,继承Fragment
  2. 重写父类的方法onCreateView()
  3. 在onCreateView()方法中,为Fragment创建UI界面

Java代码

​ MyFragment类

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

public class MyFragment extends Fragment {
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return getLayoutInflater().inflate(R.layout.fragment_layout, null);
    }
}

​ MainActivity类

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;

public class MainActivity extends AppCompatActivity {

    private MyFragment myFragment;
    private MyFragment1 myFragment1;
    private FragmentManager fragmentManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //创建fragment对象
        myFragment = new MyFragment();
        myFragment1 = new MyFragment1();
        //获得fragment的管理类对象
        fragmentManager = getSupportFragmentManager();
        //获得事务管理者
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        //添加fragment
        fragmentTransaction.add(R.id.zhen, myFragment, "叽里咕噜");
        fragmentTransaction.add(R.id.zhen, myFragment1, "123");
        fragmentTransaction.hide(myFragment1);
//        //移除fragment
//        fragmentTransaction.remove(myFragment);
//        //显示fragment
//        fragmentTransaction.show(myFragment);
//        //隐藏fragment
//        fragmentTransaction.hide(myFragment);
//        //替换
//        fragmentTransaction.replace(R.id.zhen,myFragment);
        //提交事务
        fragmentTransaction.commit();
    }

    public void show(View view) {
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.show(myFragment1);
        fragmentTransaction.commit();
    }

    public void remove(View view) {
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.remove(myFragment);
        fragmentTransaction.commit();
    }

    public void hide(View view) {
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.hide(myFragment1);
        fragmentTransaction.commit();
    }

    public void replace(View view) {
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.replace(R.id.zhen, myFragment);
        fragmentTransaction.commit();
    }
}

效果图


Fragment效果图.gif

Fragment的生命周期

  1. onAttach() Fragment与Activity关联
  2. onCreate() 创建Fragment
  3. onCreateView() 创建Fragment视图
  4. onActivityCreated() 当Activity中的onCreate方法执行完后调用
  5. onStart() 启动Fragment
  6. onResume() Fragment可见
  7. onPause() Fragment不可见
  8. onStop() Fragment停止
  9. onDestoryView() 销毁Fragment视图
  10. onDestory() 销毁Fragment对象
  11. onDetach() Fragment和Activity解除关联的时候调用

add生命周期执行

  1. onAttach()
  2. onCreate()
  3. onCreateView()
  4. onActivityCreated()
  5. onStart()
  6. onResume()

remove时生命周期执行

  1. onPause()
  2. onStop()
  3. onDestoryView()
  4. onDestory()
  5. onDetach()

replace时生命周期执行

  1. 新Fragment --> onAttach()
  2. 新Fragment --> onCreate()
  3. 旧Fragment --> onPause()
  4. 旧Fragment --> onStop()
  5. 旧Fragment --> onDestroyView()
  6. 旧Fragment --> onDesttoy()
  7. 旧Fragment --> onDetach()
  8. 新Fragment --> onCreateView()
  9. 新Fragment --> onActivityCreated()
  10. 新Fragment --> onStart()
  11. 新Fragment --> onResume()

add时Fragment和Fctivity的生命周期执行

  1. Activity --> onCreate()
  2. Fragment --> onAttach()
  3. Fragment --> onCreate()
  4. Fragment --> onCreateView()
  5. Fragment --> onActivityCreated()
  6. Fragment --> onStart()
  7. Activity --> onResume()
  8. Fragment --> onResume()

Fragment的回退栈

Activity切换时是通过栈的形式,不断压栈出栈,在Fragment的时候,如果你不是手动开启回退栈,它是直接销毁再重建,但如果将Fragment任务添加到回退栈,情况就会不一样了,它就有了类似Activity的栈管理方式。

实现

 MyFragment myFragment1 = new MyFragment();
 FragmentManager fragmentManager = getSupportFragmentManager();
 fragmentTransaction = fragmentManager.beginTransaction();
 //添加到回退栈
 fragmentTransaction.addToBackStack("1");
 fragmentTransaction.commit();

回退

 //回退一个
 fragmentManager.popBackStack();
 //立刻回退一个
 fragmentManager.popBackStackImmediate();
 

Fragment的传值

setArguments() & getArguments()

MyFragment类

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

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

public class MyFragment extends Fragment {

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        View view = getLayoutInflater().inflate(R.layout.fragment_layout, null);
        view.findViewById(R.id.send).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Bundle bundle = new Bundle();
                bundle.putString("key","我是fragment发送过来的数据");
                MyFragment.this.setArguments(bundle);
            }
        });
        return  view;
    }

Activity类

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

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

public class MainActivity extends AppCompatActivity {

    MyFragment myFragment = new MyFragment();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void show(View view) {
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.add(R.id.frameLayout, myFragment, "name");
        fragmentTransaction.commit();
    }

    public void receive(View view) {
        Bundle bundle = myFragment.getArguments();
        String key = bundle.getString("key");
        if (key != null) {
            Toast.makeText(this, key, Toast.LENGTH_SHORT).show();
        }
    }
}

​ 效果图

Fragment_set&get传值.gif
handler传值

MyFragment类

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

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

public class MyFragment extends Fragment {
    private Handler handler;
    public MyFragment(Handler handler) {
        this.handler = handler;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        View view = getLayoutInflater().inflate(R.layout.fragment_layout, null);
        view.findViewById(R.id.send).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Message message = Message.obtain();
                message.what = 1;
                message.obj = "我是fragment发送过来的数据";
                handler.sendMessage(message);
            }
        });
        return  view;
    }

}

Activity类

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Toast;

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

public class MainActivity extends AppCompatActivity {

    Handler handler = new Handler(){
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            switch (msg.what){
                case 1:
                    Toast.makeText(MainActivity.this, (String)msg.obj, Toast.LENGTH_SHORT).show();
                    break;
            }
        }
    };
    MyFragment myFragment = new MyFragment(handler);
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void show(View view) {
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.add(R.id.frameLayout, myFragment, "name");
        fragmentTransaction.commit();
    }
    
}

​ 效果图

Fragment_handler传值.gif
接口回调传值
MyFragment
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;

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

public class MyFragment extends Fragment {

    private EditText input;
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        //设置布局
        View view = getLayoutInflater().inflate(R.layout.fargment, null);
        input = view.findViewById(R.id.input);
        return view;
    }

    //接口回调
    public void getEditText(Interface i){
        String msg = input.getText().toString();
        i.getInput(msg);
    }

    //接口
    public interface Interface{
        public void getInput(String msg);
    }
}

MainActivity类

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

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

public class MainActivity extends AppCompatActivity {

    private MyFragment fragment;
    private FragmentManager fragmentManager;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        fragment = new MyFragment();
        fragmentManager = getSupportFragmentManager();
    }


    //添加fragment
    public void show(View view) {
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.add(R.id.framelayout,fragment,"123");
        fragmentTransaction.addToBackStack("123");
        fragmentTransaction.commit();
    }


    public void receive(View view) {
        //使用接口回调的方法获取数据
        fragment.getEditText(new MyFragment.Interface() {
            @Override
            public void getInput(String msg) {
                Toast.makeText(MainActivity.this, "输入了 --->"+msg, Toast.LENGTH_SHORT).show();
            }
        });
    }
}

效果图


Fragment_接口回调.gif
上一篇下一篇

猜你喜欢

热点阅读