EventBus
EventBus能够简化各组件间的通信,让我们的代码书写变得简单,能有效的分离事件发送方和接收方(也就是解耦的意思)。
1。EventBus三大要素
Event 事件。它可以是任意类型。
Subscriber 事件订阅者。
Publisher 事件的发布者。我们可以在任意线程里发布事件,一般情况下,使用 EventBus.getDefault()就可以得到一个EventBus对象,然后再调用post(Object)方法即可。
2。EventBus四种线程模型
POSTING (默认) 表示事件处理函数的线程跟发布事件的线程在同一个线程。
MAIN 表示事件处理函数的线程在主线程(UI)线程,因此在这里不能进行耗时操作。
BACKGROUND 表示事件处理函数的线程在后台线程,因此不能进行UI操作。如果发布事件的线 程是主线程(UI线程),那么事件处理函数将会开启一个后台线程,如果果发布事件的线程是在后台线程,那么事件处理函数就使用该线程。
ASYNC 表示无论事件发布的线程是哪一个,事件处理函数始终会新建一个子线程运行,同样不能进行UI操作。
//1.依赖 implementation ‘org.greenrobot:eventbus:3.0.0’
//2.定义消息事件类
//3.注册和解除注册+声明订阅者+事件发布
分别在Activity的onCreate()方法和onDestory()方法里,进行注册EventBus和解除注册
//主类
public class MainActivity extends AppCompatActivity {
Button button;
// 1.添加依赖:'org.greenrobot:eventbus:3.1.1'
// 2.发送者:发布者
// postSticky(Object)
// 3.接收者:订阅者
// 注册 onCreate
// 解除注册 onDestory
// 声明订阅者进行接受消息 @SubScribe
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button=findViewById(R.id.btn);
EventBus.getDefault().register(this);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
EventBus.getDefault().post(new Student("刘瑞",19,"男",185,180));
}
});
}
@Subscribe(threadMode = ThreadMode.MAIN)
public void back(Student student){
Toast.makeText(this,"姓名:"+student.getName()+",年龄:"+student.getAge()+",性别:"+student.getSex()+",身高:"+student.getHeight()+",体重:"+student.getWeight(),Toast.LENGTH_SHORT).show();
}
@Override
protected void onDestroy() {
super.onDestroy();
EventBus.getDefault().unregister(this);
}
}
//消息 学生类
public class Student {
private String name;
private int age;
private String sex;
private int height;
private int weight;
public Student(String name, int age, String sex, int height, int weight) {
this.name = name;
this.age = age;
this.sex = sex;
this.height = height;
this.weight = weight;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
}
public int getWeight() {
return weight;
}
public void setWeight(int weight) {
this.weight = weight;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
", sex='" + sex + '\'' +
", height=" + height +
", weight=" + weight +
'}';
}
}
Otto第三方框架
1, 导入依赖 implementation ‘com.squareup:otto:1.3.8’
2, 定义一个类 AppBus, 继承Bus , ---- 单例模式 , 返回Bus 的子类对象
3, 注册到Bus 的主线程中
4, 在onDestry() 方法中取消注册
5, 声明订阅者
6, 发起订阅 – 主线程中
//主类
public class MainActivity extends AppCompatActivity {
Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button=findViewById(R.id.btn);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//发送数据
Ottobus.getDefalut().post(new Student("刘瑞",19,"男",185,180));
}
});
Ottobus.getDefalut().register(this);
}
@Subscribe
public void back(Student student){
Toast.makeText(this,"姓名:"+student.getName()+",年龄:"+student.getAge()+",性别:"+student.getSex()+",身高:"+student.getHeight()+",体重:"+student.getWeight(),Toast.LENGTH_SHORT).show();
}
@Override
protected void onDestroy() {
super.onDestroy();
Ottobus.getDefalut().unregister(this);
}
}
//定义一个类 AppBus, 继承Bus
public class Ottobus extends Bus {
//饿汉单例
//构造私有化
private Ottobus(){}
//自己实例化
private static Ottobus ottoBus=new Ottobus();
//提供公开方法
public static Ottobus getDefalut(){
return ottoBus;
}
}
//学生消息类
public class Student {
private String name;
private int age;
private String sex;
private int height;
private int weight;
public Student(String name, int age, String sex, int height, int weight) {
this.name = name;
this.age = age;
this.sex = sex;
this.height = height;
this.weight = weight;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
}
public int getWeight() {
return weight;
}
public void setWeight(int weight) {
this.weight = weight;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
", sex='" + sex + '\'' +
", height=" + height +
", weight=" + weight +
'}';
}
}
Fragment
Fragment,英文碎片的意思,早期应用在平板设计中
步骤1:创建Fragment对象:(右击新建Fragment一步搞定)
(1).定义一个类, 继承Fragment
(2).重写父类的方法onCreateView()
(3).在onCreateView()方法中, 为Fragment 创建UI界面
步骤2:显示Fragment
(1)静态显示 Fragment – 布局页面中直接写
a.在Activity 对应的页面中, 通过标签 引入
b.在标签中, 必须要指定id属性, 用来标识Fragment 的唯一性 c.在标签中,必须要有name属性, 用来标识当前显示那个Fragment
(2)动态显示 Fragment — 在Activity.java 代码中显示
a.在Activity 对应的页面中, 需要通过布局容器占位
b.在Activity 代码中得到Fragment 管理器对象
c.得到Fragment 事务管理 添加、移除、显示、隐藏、替换Fragment 提交事务
Fragment的生命周期
1.onAttach() :Fragment与Activity有联系。
2.onCreate():创建Fragment
3.onCreateView():创建Fragment视图,尽量不要做耗时操作
4.onActivityCreated():当Activity中的onCreate方法执行完后调用。
5.onStart():启动。
6.onResume():可见
7.onPause():不可见
8.onStop():停止。
9. onDestroyView() :销毁Fragment视图
10.onDestroy():销毁fragment对象
11.onDetach():Fragment和Activity解除关联的时候调用
/**
* 如何实现微信切换
* 1.activity布局:FrameLayout(放fragment容器)
* 2.创建fragment类:自定义类继承Fragment 重写onCreateView方法 加载布局
* 3.点击按钮切换
getSupportFragmentManager().beginTransaction().replace(R.id.frame,fragment1).commit();
*
* */
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
RadioButton radioButton1,radioButton2,radioButton3;
Myframe1 myframe1;
Myframe2 myframe2;
Myframe3 myframe3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
radioButton1=findViewById(R.id.rb1);
radioButton2=findViewById(R.id.rb2);
radioButton3=findViewById(R.id.rb3);
myframe1=new Myframe1();
myframe2=new Myframe2();
myframe3=new Myframe3();
radioButton1.setOnClickListener(this);
radioButton2.setOnClickListener(this);
radioButton3.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.rb1:
//获得Fragment管理者
FragmentManager supportFragmentManager = getSupportFragmentManager();
//开启事务
FragmentTransaction transaction = supportFragmentManager.beginTransaction();
//操作
transaction.replace(R.id.frame,myframe1);//参数一 fragmelaypout容器
//提交
transaction.commit();
break;
case R.id.rb2:
getSupportFragmentManager().beginTransaction().replace(R.id.frame,myframe2).commit();
break;
case R.id.rb3:
getSupportFragmentManager().beginTransaction().replace(R.id.frame,myframe3).commit();
break;
}
}
}
//
public class Myframe1 extends Fragment {
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.frame1_layout,null);
return view;
}
}
//
public class Myframe2 extends Fragment {
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.frame2_layout,null);
return view;
}
}
//
public class Myframe3 extends Fragment {
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.frame3_layout,null);
return view;
}
}
//布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<FrameLayout
android:id="@+id/frame"
android:layout_weight="8"
android:layout_width="match_parent"
android:layout_height="0dp">
</FrameLayout>
<RadioGroup
android:layout_weight="2"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="0dp">
<RadioButton
android:id="@+id/rb1"
android:button="@null"
android:text="消息"
android:gravity="center"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent"/>
<RadioButton
android:id="@+id/rb2"
android:button="@null"
android:text="联系人"
android:gravity="center"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent"/>
<RadioButton
android:id="@+id/rb3"
android:button="@null"
android:text="我的"
android:gravity="center"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent"/>
</RadioGroup>
</LinearLayout>
//
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:background="#C42B2B"
android:layout_width="match_parent"
android:layout_height="match_parent">
</LinearLayout>
//
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:background="#009688"
android:layout_width="match_parent"
android:layout_height="match_parent">
</LinearLayout>
//
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:background="#FFC107"
android:layout_width="match_parent"
android:layout_height="match_parent">
</LinearLayout>