Android中添加监听回调接口的方法

在Android中,我们经常会添加一些监听回调的接口供别的类来回调,比如自定义一个PopupWindow,需要让new这个PopupWindow的Activity来监听PopupWindow中的一些组件点击后的回调,再比如写一个RecyclerView的适配器时,也需要让相应的类来监听它的条目点击事件。

下面我们就以一个PopupWindow来体验一下如何来添加一个监听回调接口

方法一

首先,我们需要定义一个接口,然后再写一个设置监听接口的方法

  • 自定义PopupWindow类

    /**
    * Created by Tryking on 2016/4/3.
    */
    public class MyPopupWindow extends PopupWindow { private static final int DOG = 1;
    private static final int PIG = 2;
    private final View view;
    private final Button dog;
    private final Button pig; //这里,我们定义一个接口
    public interface OnItemClickListener {
    public void onItemClick(Button button, int tag);
    } private OnItemClickListener mListener; //写一个设置接口监听的方法
    public void setOnItemClickListener(OnItemClickListener listener) {
    mListener = listener;
    } public MyPopupWindow(Context context, AttributeSet attrs) {
    super(context, attrs);
    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    view = inflater.inflate(R.layout.my_popup_window, null);
    setContentView(view);
    setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
    setHeight(ViewGroup.LayoutParams.MATCH_PARENT); dog = (Button) view.findViewById(R.id.dog);
    pig = (Button) view.findViewById(R.id.pig); dog.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
    mListener.onItemClick(dog, DOG);
    }
    }); pig.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
    mListener.onItemClick(pig, PIG);
    }
    });
    } public MyPopupWindow(Context context) {
    this(context, null);
    }
    }
  • 主Activity类

    直接给new出来的PopupWindow设置回调接口

    public class MainActivity extends Activity {
    private static final int DOG = 1;
    private static final int PIG = 2;
    private MyPopupWindow mPopupWindow;
    private Button button; @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_practice_listener);
    button = (Button) findViewById(R.id.button); mPopupWindow = new MyPopupWindow(this);
    mPopupWindow.setOnItemClickListener(new MyPopupWindow.OnItemClickListener() {
    @Override
    public void onItemClick(Button button, int tag) {
    if (tag == DOG) {
    button.setText(button.getText() == "我是小狗" ? "点我是小狗" : "我是小狗");
    } else if (tag == PIG) {
    button.setText(button.getText() == "我是小猪" ? "点我是小猪" : "我是小猪");
    }
    }
    });
    button.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
    mPopupWindow.setOutsideTouchable(true);
    mPopupWindow.setFocusable(true);
    mPopupWindow.showAsDropDown(v);
    }
    });
    }
    }

方法二

我们还可以直接在构造函数里直接让构造的时候就传入一个实现了的接口

  • 自定义PopupWindow类

    /**
    * Created by Tryking on 2016/4/3.
    */
    public class MyPopupWindow extends PopupWindow {
    private final View view;
    private final Button dog;
    private final Button pig; public interface OnItemClickListener {
    public void onItemClick(Button button, int tag);
    } private OnItemClickListener mListener; public void setOnItemClickListener(OnItemClickListener listener) {
    mListener = listener;
    } //直接让构造时就传入一个接口
    public MyPopupWindow(Context context, AttributeSet attrs,View.OnClickListener listener) {
    super(context, attrs);
    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    view = inflater.inflate(R.layout.my_popup_window, null);
    setContentView(view);
    setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
    setHeight(ViewGroup.LayoutParams.MATCH_PARENT); dog = (Button) view.findViewById(R.id.dog);
    pig = (Button) view.findViewById(R.id.pig); dog.setOnClickListener(listener);
    pig.setOnClickListener(listener);
    }
    public MyPopupWindow(Context context,View.OnClickListener listener) {
    this(context, null,listener);
    }
    }
  • 主Activity类

    public class PracticeListenerActivity extends Activity {
    private MyPopupWindow mPopupWindow;
    private Button button; @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_practice_listener);
    button = (Button) findViewById(R.id.button); //new PopupWindow时就传入一个接口
    mPopupWindow = new MyPopupWindow(this, MyListener); button.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
    mPopupWindow.setOutsideTouchable(true);
    mPopupWindow.setFocusable(true);
    mPopupWindow.showAsDropDown(v);
    }
    });
    } // 写一个接口供new的时候传入
    private View.OnClickListener MyListener = new View.OnClickListener() {
    @Override
    public void onClick(View v) {
    switch (v.getId()) {
    case R.id.dog:
    ((Button) v).setText(((Button) v).getText() == "我是小狗" ? "点我是小狗" : "我是小狗");
    break;
    case R.id.pig:
    ((Button) v).setText(((Button) v).getText() == "我是小猪" ? "点我是小猪" : "我是小猪");
    }
    }
    };
    }

    好了,以上就是两种用来添加监听回调接口的方法,我个人倾向于使用第一种。

上一篇:PopuWindow_1


下一篇:Spring Boot启动过程及回调接口汇总