首页 > 代码库 > Adapter,Fragment向所属的activity中传值

Adapter,Fragment向所属的activity中传值

1、在adapter或fragment中定义接口

  如:

  public interface LongClickListener {
      void getClickName(String name);
  }

 然后在需要触发该接口方法中添加

  如:

  holder.img_avatar.setOnLongClickListener(new View.OnLongClickListener() {
    @Override
    public boolean onLongClick(View v) {
    String ClickName = datas.get(position).getToUserName();
    Toast.makeText(cxt, "长按点击事件"+ClickName,Toast.LENGTH_SHORT).show();


    if (cxt instanceof LongClickListener){
    ((LongClickListener) cxt).getClickName(ClickName);      //这两行是关键
      }


    return false;
    }
});

Adapter,Fragment向所属的activity中传值