EventS传值(切换时炸)

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);

    }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值