一般我們在做一個項目的時候會出現許多的重複的界面使用(如界面頂面導航),一般我們使用自定義組合控件,頂部設定的點選事件我們一般都會回傳給使用者來調用,這是就用到了接口回調。我寫了一個最簡單的例子來了解接口回調。
public class TitleView extends RelativeLayout{
private ImageView back;
private TextView title;
private OnBackClickListener onBackClickListener;
public TitleView(Context context, AttributeSet attrs) {
super(context, attrs);
initView();
initListener();
}
private void initView() {
View inflate = View.inflate(getContext(), R.layout.titleview, this);
back = (ImageView) inflate.findViewById(R.id.activate_back);
title = (TextView) inflate.findViewById(R.id.title);
}
private void initView() {
back.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (onBackClickListener != null) {
onBackClickListener.onbackClickListener(v);
}
}
});
}
public void setTitle(String str) {
title.setText(str);
}
public void setOnBackClickListener(OnBackClickListener onBackClickListener) {
this.onBackClickListener = onBackClickListener;
}
public interface OnBackClickListener {
void onbackClickListener(View v);
}
}