1. 程式人生 > >Adapter,Fragment向所屬的activity中傳值

Adapter,Fragment向所屬的activity中傳值

activit .sh use 點擊 rri hold pan col avatar

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中傳值