tablayoutジャンプとEventBusパス

1751 ワード

第一依存
//EventBus  
 implementation'org.greenrobot:eventbus:3.0.0'

MainActivityでジャンプ取得データを書きます
 public void Jump(){
        pager.setCurrentItem(1);
    }

Adapterにクリックイベント送信メッセージを書く
  //  
    onClink onClink;

    public void setOnClink(MyAdapter.onClink onClink) {
        this.onClink = onClink;
    }

    public interface onClink{
        void onClink(int pition);
    }
  //    tab02     
           myViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
               @Override
               public void onClick(View v) {
                   TextBean textBean = new TextBean();

                   textBean.setImg(list.getMlss().getCommodityList().get(i).getMasterPic());
                   textBean.setName(list.getMlss().getCommodityList().get(i).getCommodityName());
                   textBean.setPrice(list.getMlss().getCommodityList().get(i).getPrice()+"");
                   EventBus.getDefault().postSticky(textBean);
                   EventBus.getDefault().post(textBean);
                   ((MainActivity)context).Jump();
               }
           });
    }


fragment 02に書く
//         
     EventBus.getDefault().register(this);
  //  
    @Subscribe(threadMode = ThreadMode.MAIN,sticky = true)
    public void Event(TextBean textBean){
       textView.setText(textBean.getName());
       textView1.setText(textBean.getPrice());
       Glide.with(getActivity()).load(textBean.getImg()).into(imageView);
    }
    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }