版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/nzzl54/article/details/51314252
1.回调:我的理解是,一个类A实现一个接口CallBack(该接口可以是B类的内部接口,也可以是独立的接口),类A包含类B的引用b,类B有参数为callBack的方法或构造函数,最后类A调用B中的方法或构造函数,当类B触发某事件即可调用类A中CallBack的方法,最后将操作结果返回给类A,此为回调。
2.例子:自定义公用Adapter适配器
(1)类A:LActivity
public class LActivity extends BaseActivity implements View.OnClickListener,LAdepter.LCallBack{
Context mContext;
private ListView listView;
private LAdepter lAdepter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list_item_lenita_item); //layout,其包含一个ListView
initData();
}
private void initData(){
mContext = this;
String[] s = new String[3];
s[0] = "first line";
s[1] = "second line";
s[2] = "third line";
lAdepter = new LAdepter(mContext,LActivity.this,s);
listView = (ListView)findViewById(R.id.lv_lenita); //ListView
listView.setAdapter(lAdepter);
}
@Override
public void onClick(View view) {
}
@Override
public ComponentName getBroadcastComponent() {
return getComponentName();
}
@Override
public void answer(String str) {
LogUtil.log("test","call back answer = "+ str); //具体的返回内容看类B给什么String
}
}
public class LAdepter extends BaseAdapter {
LCallBack lCallBack;
private String[] strings;
private Context mContext;
public LAdepter(Context context,LCallBack lCallBack,String[] strings){
this.lCallBack = lCallBack;
this.strings = strings;
mContext = context;
}
@Override
public int getCount() {
return strings.length;
}
@Override
public String getItem(int i) {
return strings[i];
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
if(view == null){
view = LayoutInflater.from(mContext).inflate(R.layout.list_item_spinner, null); //自定义的ListView样式
view.setTag(R.id.tv_list_item, view.findViewById(R.id.tv_list_item));
}
String content = getItem(i);
TextView tv_list_item = (TextView) view.getTag(R.id.tv_list_item);
tv_list_item.setText(content);
tv_list_item.setOnClickListener(onClickListener); //条目点击监听
tv_list_item.setTag(i);
return view;
}
View.OnClickListener onClickListener = new View.OnClickListener() {
@Override
public void onClick(View view) {
int position = (int)view.getTag();
lCallBack.answer(getItem(position)); //回调
}
};
public interface LCallBack {
public void answer(String str); //类B的内部接口
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/lv_lenita"
android:divider="@null">
</ListView>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/layout_city_list">
<TextView
android:id="@+id/tv_list_item"
android:layout_width="match_parent"
android:layout_height="40dp"
android:text="test"
android:textSize="17.7dp"
android:textColor="@color/black_word"
android:gravity="center_vertical"
android:paddingLeft="20dp"/>
</LinearLayout>