题外话:这篇博客包含了我其中一个最耗费我时间去理解的知识点。理解能力渣渣,接口回调技术曾经用了两星期去理解才理解,说理解我都不自信,因为我都不知道我写的是不是接口回调,只是因为我看着长得像,就写出来看看,顺便做自我记录,恳请大神们拍砖。
首先感谢loader大神和龙伟大神的指点还有csdn各个大神的博客,没有你们写的接口回调我也不会理解。
接口回调技术,说起来高大上,很多人一听云里雾里的,官方解释更是让人感到乱七八糟,初学者很容易迷惑,跟特么说绕口令似的,比如,比较官方的解释是这么说的 :所谓回调:就是A类中调用B类中的某个方法C,然后B类中反过来调用A类中的方法D,D这个方法就叫回调方法。卧槽,好特么绕,谁念到这里也会晕菜。要我说,干脆就把他功能说出来就行了,接口回调就是来传递结果的,多简单!比如你有两个类,A类B类,B类完成一个加法运算,那么它怎么把结果告诉A类呢,你就考虑接口回调就行了!说到这你不理解没关系,继续看。
先说一个需求,需求很简单,点击一下button,改一下textview的值,用接口回调方法实现。(我知道不用接口回调就能实现,这只是为了简单起见,一些大神们写博客为了把接口回调解释的清楚,说了很详细的例子,结果我是越看越晕,我们一刀见血,从最常见开始)。
首先定义一个button按钮的xml文件,很简单吧?
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="wrap_content" >
-
- <Button
- android:id="@+id/id_custombutton"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text="按钮" />
-
- </LinearLayout>
下面我们定义这个button为CustomButton,代码如下
- package com.derek.com;
-
- import android.content.Context;
- import android.util.AttributeSet;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.widget.Button;
- import android.widget.LinearLayout;
-
- public class CustomButton extends LinearLayout {
-
- private View customView;
-
-
-
-
-
- private CustomClickListener listener;
-
-
- private Button button;
-
- private int mCount = 0;
- public CustomButton(Context context, AttributeSet attrs, int defStyleAttr) {
- super(context, attrs, defStyleAttr);
- initview(context);
- initEvent();
- }
-
- public CustomButton(Context context, AttributeSet attrs) {
- this(context, attrs,0);
-
- }
- public CustomButton(Context context) {
- this(context,null);
-
- }
-
- private void initview(Context context) {
- LayoutInflater inflater = LayoutInflater.from(context);
-
- customView = inflater.inflate(R.layout.custombutton, this,true);
-
- button = (Button) customView.findViewById(R.id.id_custombutton);
-
- }
-
- private void initEvent() {
- button.setOnClickListener(new OnClickListener() {
-
- @Override
- public void onClick(View v) {
-
-
-
-
- mCount++;
-
-
-
-
-
-
-
- listener.onCustomClick(mCount);
- }
-
- });
-
- }
-
-
-
-
-
-
-
-
-
-
- public interface CustomClickListener{
-
-
- void onCustomClick(int count);
- }
-
-
-
-
-
-
-
-
-
-
- public void setCustomClickListener(CustomClickListener listener) {
-
- this.listener = listener;
- }
- }
然后主界面调用,方法如下
- package com.derek.com;
-
- import com.derek.com.CustomButton.CustomClickListener;
-
- import android.os.Bundle;
- import android.support.v7.app.ActionBarActivity;
- import android.widget.TextView;
-
- public class MainActivity extends ActionBarActivity {
-
- private CustomButton button;
- private TextView tv;
-
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
-
- tv = (TextView) findViewById(R.id.id_maintext);
- button = (CustomButton) findViewById(R.id.id_maincustombutton);
-
- button.setCustomClickListener(new CustomClickListener() {
-
- @Override
- public void onCustomClick(int count) {
-
- tv.setText("" + count);
-
- }
- });
- }
-
- }
是不是很简单,刚写博客的时候又发现了一个规律,如果你看完博客还是不明白,你就这么记
首先:接口回调是用来传递你的结果的。
其次:接口回调很简单,你定义一个接口,里面写一个方法,你需要传递什么结果,就把结果当方法的参数。
再次:写一个set方法初始化这个接口对象。
再再次:实例化后的接口对象调用你接口里的方法,并将你的结果传入。
哦了,接口回调写完了
我把这个规律写到注释中,你们就明白了
其实这个说白了就是利用了接口里的方法是必须要重写的,当接口的方法重写的时候,count的值就被传到另一个类里啦。
明白了吗大家,下一篇记录一下inflate方法里root参数,因为我在写这个例子的时候也特么出现问题了就是因为root不理解,擦擦擦。
对了,效果图如下:
擦,图片预览点击好快,跟特么撸管似的,醉了,不知道上传后怎么样。第一次用gif截图,
--------------安卓小小鸟成长记