RecyclerView点击条目传值=====EventBus(粘性事件)

Adapter

//创建接口
    public interface ViewholderInterface{
        void vh(Movies.ResultBean resultBean);
    }
    //声明接口名
    private ViewholderInterface mViewholderInterface;
    //暴露方法
    public void setViewholderInterface(ViewholderInterface viewholderInterface){
        mViewholderInterface=viewholderInterface;
    }


    //点击条目跳转=====onBind
        viewholder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mViewholderInterface.vh(list.get(i));
            }
        });

接收值的Activity页面

import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.wanghuiqi20181130.bean.Movies;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

public class EventBusActivity extends AppCompatActivity {

    private TextView event_text;
    private SimpleDraweeView simpl_img;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_event_bus);
        //初始化控件
        initView();
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }

    }

    private void initView() {
        event_text = (TextView) findViewById(R.id.event_text);
        simpl_img = (SimpleDraweeView) findViewById(R.id.simpl_img);
    }

    //接收
    @Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
    public void receiveMovies(Movies.ResultBean resultBean) {
        event_text.setText(resultBean.getName());
        simpl_img.setImageURI(Uri.parse(resultBean.getImageUrl()));
    }
}

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