android中必备的接口回调用法

1 ,这个方法很常见,本人觉得也很实用,分享下吧

public class DirverDistanceTool {
public void getDirverDistance(LatLng start, LatLng end,final OnDirverDistanceListener onDirverDistanceListener) {
RoutePlanSearch mSearch = RoutePlanSearch.newInstance();
mSearch.setOnGetRoutePlanResultListener(new OnGetRoutePlanResultListener() {
private DrivingRouteLine route;

public void onGetWalkingRouteResult(WalkingRouteResult result) {}//获取步行线路规划结果

public void onGetTransitRouteResult(TransitRouteResult result) {}//获取公交换乘路径规划结果

@Override
public void onGetMassTransitRouteResult(MassTransitRouteResult massTransitRouteResult) {}

public void onGetDrivingRouteResult(DrivingRouteResult result) {
if(result.error == SearchResult.ERRORNO.NO_ERROR && null != result && null != result.getRouteLines() && result.getRouteLines().size() > 0){
int distance = result.getRouteLines().get(0).getDistance();
BigDecimal bigDecimal = new BigDecimal(Double.valueOf(distance) / 1000);
if(null != onDirverDistanceListener)onDirverDistanceListener.dirverDistanceListener(bigDecimal.setScale(1, BigDecimal.ROUND_HALF_UP).doubleValue());
}else{
System.out.println("=============>抱歉,未计算出驾车距离...");
if(null != onDirverDistanceListener)onDirverDistanceListener.dirverDistanceListener(null);
}
}//获取驾车线路规划结果

@Override
public void onGetIndoorRouteResult(IndoorRouteResult indoorRouteResult) {}

@Override
public void onGetBikingRouteResult(BikingRouteResult bikingRouteResult) {}
});
mSearch.drivingSearch((new DrivingRoutePlanOption()).from(PlanNode.withLocation(start)).to(PlanNode.withLocation(end)));
}

public interface OnDirverDistanceListener{
void dirverDistanceListener(Double returnKm);
}
}

分析 :
这种方法是接口回调套接口回调,用来处理管理代码,让代码更有层次感
RoutePlanSearch mSearch = RoutePlanSearch.newInstance(); 
                  中有 onGetWalkingRouteResult(); onGetTransitRouteResult();
                   onGetMassTransitRouteResult(); onGetDrivingRouteResult()
                  onGetIndoorRouteResult(); onGetBikingRouteResult();
                  有六个方法如果都放在一个类中会让代码很臃肿,通过接口回调可以很的解决这个问题了

写了一个接口, 把 dirverDistanceListener ()抛到使用类中
public interface OnDirverDistanceListener{
void dirverDistanceListener(Double returnKm);
}

在使用的类中直接new,拿到了返回的 returnKm,
(new DirverDistanceTool()).getDirverDistance(start, end, new DirverDistanceTool.OnDirverDistanceListener() {
@Override
public void dirverDistanceListener(Double returnKm) {
if (null != deliveryPrice && null != returnKm && !TextUtils.isEmpty(minimun)) {
returnKM = returnKm;
price = deliveryPrice * returnKm * (isWeightHundred ? Integer.valueOf(minimun) : TextUtils.isEmpty(other_t.getText().toString()) ? 0 : Double.parseDouble(other_t.getText().toString().replace("吨", "")));
if (null != price)
delivery_price_text.setText((price > 0) ? "¥ " + DubString.keepPrecision(price + "", 2) : "0.00");
}//end of if
}
});
2.这个方法是基本用法
定义接口
  public interface CallBackGetAddress {
  public void editListener(int position);
  public void deleteListener(int position);
}
通过构造方法传
private CallBackGetAddress callBack;
public AddressAdapter(Context context, CallBackGetAddress callBack) {
this.context = context;
this.callBack = callBack;
}
使用回调
vh.tvEdit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
callBack.editListener(i);
}
});


在类中使用直接implements AddressAdapter.CallBackGetAddress
重写
@Override
public void editListener(int position) {
BeanAdressList.Address address = list.get(position);
Bundle bundle = new Bundle();
bundle.putSerializable(AdressEditActivity.AdressDetail, address);
StephenToolUtils.startActivityNoFinish(activity, AdressEditActivity.class, bundle, Url.editAdress);
}


3.第三种实现方法,跟第二种差不多,是用activity实现的,比较简单粗暴
申明方法
private final PublishActivity publishActivity;
public ExpadnableAdapter(PublishActivity publishActivity) {
this.publishActivity = publishActivity;
}


把方法回调过去
publishActivity.changeExpandableListShow(groupPosition, isExpanded);


在activity中使用
public void changeGroupPrice(int groupPosition, float price) {//设置父项是否编辑,是否有价格
purchase_type.get(groupPosition).setEditing(false);
purchase_type.get(groupPosition).setCurPrice(price);
}
4. 通过set 来回调
private OnRightClickListener onRightClickListener;

/**
* 右边图标点击的回调
*/
public interface OnRightClickListener {
void onClick(EditText editText);
}

public void setOnRightClickListener(OnRightClickListener onRightClickListener) {
this.onRightClickListener = onRightClickListener;
}

很实用,个人项目中摘要
 
posted @ 2017-11-27 12:11  dub  阅读(3433)  评论(0编辑  收藏  举报