Eventbus传值

//添加依赖
compile 'org.greenrobot:eventbus:3.0.0'
//MessageEvent类
public class MessageEvent {
String pic;
 String title;

    public MessageEvent(String pic, String title) {
        this.pic = pic;
        this.title = title;
    }

    public String getPic() {
        return pic;
    }

    public void setPic(String pic) {
        this.pic = pic;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }
}

//从网上获取数据然后在adapter里面设置要传值跳转的数据
holder.lin.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        EventBus.getDefault().postSticky(new MessageEvent(list.get(position).getPicUrl(),list.get(position).getTitle()));
        Intent intent=new Intent(context,Mainactivity2.class);
        context.startActivity(intent);
    }
});
//第二个布局设置页面
<com.facebook.drawee.view.SimpleDraweeView
    android:layout_width="150dp"
    android:layout_height="150dp"
    android:id="@+id/img"
    fresco:placeholderImage="@mipmap/ic_launcher"/>


<TextView
    android:id="@+id/tv"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:text="1" />

public class Mainactivity2 extends AppCompatActivity {
    @Bind(R.id.img)
    SimpleDraweeView img;
    @Bind(R.id.tv)
    TextView tv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        ButterKnife.bind(this);
        EventBus.getDefault().register(this);

    }
    @Subscribe(sticky = true, threadMode = ThreadMode.MAIN)
    public void onEvent(MessageEvent event) {
        String pic = event.getPic();
        img.setImageURI(pic);
        String title = event.getTitle();
        tv.setText(title);

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}


你可能感兴趣的:(Eventbus传值)