Android Activity和Adapter通过接口交互
2016-07-01 10:57
393 查看
之前都是调用系统的接口回调很随意并不知道原理是什么!当自己Activity和Adapter需要写个回调时就蒙圈了!然后的然后就去看系统的点击事件的实现!!!!SO 先看系统是怎么定义回调接口的`~~~~然后比葫芦画瓢写我们的回调
1.首先当然是定义一个接口了呢/** * Interface definition for a callback to be invoked when a view is clicked. */ public interface OnClickListener { /** * Called when a view has been clicked. * * @param v The view that was clicked. */ void onClick(View v); }
2.其次呢当然是写个注册的方法的呢大白话就是你想使用我的东西当然是要给我说一声了
/** * Register a callback to be invoked when this view is clicked. If this view is not * clickable, it becomes clickable. * * @param l The callback that will run * * @see #setClickable(boolean) */ public void setOnClickListener(@Nullable OnClickListener l) { if (!isClickable()) { setClickable(true); } getListenerInfo().mOnClickListener = l; }
3.然后呢系统当然需要有个地方去调用才会执行啊
/** * Call this view's OnClickListener, if it is defined. Performs all normal * actions associated with clicking: reporting accessibility event, playing * a sound, etc. * * @return True there was an assigned OnClickListener that was called, false * otherwise is returned. */ public boolean performClick() { final boolean result; final ListenerInfo li = mListenerInfo; if (li != null && li.mOnClickListener != null) { playSoundEffect(SoundEffectConstants.CLICK); li.mOnClickListener.onClick(this); result = true; } else { result = false; } sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_CLICKED); return result; }
说明:因为接口定义是抽象的并且方法没有具体实现处理当然是它的实现部分了
4 .最后呢当然是我们熟悉的部分了,注册接口
mTvAttention.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //具体操作 } });
看完系统定义的接口回调下面我们来实现自己的
使用场景: 在Adapter中通过回调接口通知Activity进行改变数据状态1.首先定义一个接口
public interface AttentionClickListener { void callBack(); }
2.其次呢编写注册方法
public void setAttentionClickListener(AttentionClickListener attentionClickListener) { mAttentionClickListener = attentionClickListener; }
3.然后呢当然是事件触发处理就是当什么时候我们去回调
params = new HashMap(); params.put("uid", SharePrefUtil.getString(mContext, Contants.UID, "")); params.put("agent_uid", agent_uid); mHttpHelper.post_header(UrlUtil.save_follow, params, new SpotsCallBack(mContext) { @Override public void onSuccess(Response response, BaseBean baseBean) { if (baseBean.getErr() == 0 || baseBean.getErr() == 1400) { mAttentionClickListener.callBack(); notifyDataSetChanged(); } else { Toast.makeText(mContext, baseBean.getInfo(), Toast.LENGTH_SHORT).show(); } } @Override public void onError(Response response, int code, Exception e) { } }, mContext); } " data-snippet-id="ext.734d1ef6ceecdd09759195c992dc7ee6" data-snippet-saved="false" data-codota-status="done">[code] mIvAttention.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (mData.get(getLayoutPosition()).getIs_follow().equals("0")) { requestAddAttentionData(mData.get(getLayoutPosition()).getUid()); } else { requestCancelAttentionData(mData.get(getLayoutPosition()).getUid()); } } }); /** * 关注 */ private void requestAddAttentionData(String agent_uid) { Map<String, String> params = new HashMap<String, String>(); params.put("uid", SharePrefUtil.getString(mContext, Contants.UID, "")); params.put("agent_uid", agent_uid); mHttpHelper.post_header(UrlUtil.save_follow, params, new SpotsCallBack<BaseBean>(mContext) { @Override public void onSuccess(Response response, BaseBean baseBean) { if (baseBean.getErr() == 0 || baseBean.getErr() == 1400) { mAttentionClickListener.callBack(); notifyDataSetChanged(); } else { Toast.makeText(mContext, baseBean.getInfo(), Toast.LENGTH_SHORT).show(); } } @Override public void onError(Response response, int code, Exception e) { } }, mContext); }
说明:这是一个项目中关注和取消关注中使用案例!通过一个按钮点击事件请求关注或者未关注触发回调函数!通知Activity请求改变后的数据
4.最后当然是我们在Activity中注册监听器实现回调方法刷新数据
mAdapter.setAttentionClickListener(new DiscoverNearbyAgentAdapter.AttentionClickListener() { @Override public void callBack() { requestData(); } });
相关文章推荐
- Android 自定义View (三部曲)
- android textView跑马灯效果
- Android clipChildren用法
- Android PieChart 饼图控件
- android alarm driver &framework 关键流程
- Activity管理 退出登录 或退出程序
- 6.0、Android Studio性能优化工具
- 6.0、Android Studio性能优化工具
- Error-Expected resource of type styleable [ResourceType]
- android studio生成jar包
- Android MTK 设置开机启动自定义Launcher Home键换出Launcher切换
- android studio 使用git添加远程库(这里用Github)
- 【Android】Android中 Paint 字体、粗细等属性的一些设置
- Android实战技巧之四十九:Usb通信之USB Host
- android 5.0以上状态栏标题栏颜色属性
- Android获取Ip地址
- R cannot be resolved to a variable
- android 影响开机速度因素
- Android中Activity生命周期和启动模式详解
- Android MTK 设置默认启动 Launcher