1.依赖:
implementation 'com.jakewharton:butterknife:8.8.1'
annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1'
implementation 'org.greenrobot:eventbus:3.0.0'
2.主页面 把Bean类 提示为全局的
private BannerBean.DataBean data1;
//商品名字
cz.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Message object=new Message(data1.getTitle(),"title");
//黏型事件
EventBus.getDefault().postSticky(object);
}
});
//价格
jg.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//黏贴时间
Message object=new Message(data1.getPrice(),"price");
Toast.makeText(getActivity(),data1.getPrice()+"",Toast.LENGTH_LONG).show();
//黏型事件
EventBus.getDefault().postSticky(object);
}
});
3.接受名称
public class two extends Fragment {
private TextView t;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.two,container,false);
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
t=view.findViewById(R.id.t);
//声明注册
EventBus.getDefault().register(this);
}
//判断 是否是title
@Subscribe(threadMode =ThreadMode.MAIN,sticky = true) //必写 重要
public void onMessage(Message bean)
{
if (bean.getId().equals("title"))
{
t.setText(bean.getObject()+"");
}
}
//销毁
@Override
public void onDestroy() {
super.onDestroy();
EventBus.getDefault().unregister(this);
}
}
4.接受价格
public class three extends Fragment {
private TextView p;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.three,container,false);
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
p=view.findViewById(R.id.p);
EventBus.getDefault().register(this);
}
@Subscribe(threadMode =ThreadMode.MAIN,sticky = true) //必写 重要
public void onMessage(Message bean)
{
if (bean.getId().equals("price"))
{
p.setText(bean.getObject()+"");
}
}
//销毁
@Override
public void onDestroy() {
super.onDestroy();
EventBus.getDefault().unregister(this);
}
}
6.再写一个message类
package com.bw.ymy.week2_text1.bean;
public class Message {
Object object;
String id;
public Message(Object object, String id) {
this.object = object;
this.id = id;
}
public Object getObject() {
return object;
}
public void setObject(Object object) {
this.object = object;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
}
注:在Fragment中用 butterknife
注册中必写 this.view
再点击切换的时候容易炸 传值
根据切换多少页 写入数字 3;
Bundle+EventButs传值
//发送方
Bundle bundl1=new Bundle();
bundl1.putString("na1me",mCommentNum+"");
EventBus.getDefault().post(bundl1);
//接收方
//判断 是否是na1me
@Subscribe(threadMode =ThreadMode.MAIN,sticky = true) //必写 重要
public void onMumean(Bundle bean)
{
sum=bean.getString("na1me");
h_xx_num.setText(sum);
}