接口回調

           在適配器裏ide

public class RecyAdapter extends RecyclerView.Adapter<RecyAdapter.ViewHolder>implements View.OnClickListener{
    private Context context;
    private List<ShiTi.DataBean.DatasBean> datas;

    public RecyAdapter(List<ShiTi.DataBean.DatasBean> datas, Context context) {
        this.datas = datas;
        this.context = context;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View inflate = LayoutInflater.from(context).inflate(R.layout.item, parent, false);
        ViewHolder viewHolder = new ViewHolder(inflate);
        //第二步
——————————————————————————————————————————————————————————
        inflate.setOnClickListener(this);
——————————————————————————————————————————————————————————————————————
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        holder.content_.setText(datas.get(position).getPublisherName());
        holder.text_.setText(datas.get(position).getBriefShow());
        Picasso.with(context).load("http://file.market.xiaomi.com/mfc/thumbnail/png/w150q80/"+datas.get(position).getIcon()).into(holder.image_);

        //第三步
————————————————————————————————————————————————————————————
        holder.itemView.setTag(position);
——————————————————————————————————————————————————————————————————

    }

    @Override
    public int getItemCount() {
        return datas.size();
    }


    public class ViewHolder extends RecyclerView.ViewHolder {
        private  ImageView image_;
        private  TextView content_;
        private  TextView text_;

        public ViewHolder(View itemView) {
            super(itemView);
            itemView.findViewById(R.id.image_);
            image_=(ImageView) itemView.findViewById(R.id.image_);
            content_=(TextView) itemView.findViewById(R.id.content_);
            text_=(TextView)  itemView.findViewById(R.id.text_);
        }
    }

——————————————————————————————————————————————————————————————————————————————————————————————————————
    //1.第一步
    public interface OnItemClickListener {
        void setOnItemClickListener(View v, int position);
    }

    public OnItemClickListener itemClickListener;



    public void onClick(View v) {
        if (itemClickListener != null) {
            itemClickListener.setOnItemClickListener(v, (int) v.getTag());
        }
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.itemClickListener = listener;
    }
}

 

在Maniactivity中this

adapter.setOnItemClickListener(new RecyAdapter.OnItemClickListener() {
    @Override
    public void setOnItemClickListener(View v, int position) {
        Intent intent = new Intent(MainActivity.this, Main2Activity.class);
        intent.putExtra("position",datas.get(position).getPublisherName());
        intent.putExtra("position2",datas.get(position).getBriefShow());
        startActivity(intent);
    }
});

 

Main2Activity中接受get

Intent intent = getIntent();
String position = intent.getStringExtra("position");
String position2 = intent.getStringExtra("position2");
text.setText(position);
text1.setText(position2);
相關文章
相關標籤/搜索