Simple application of callback method in Android development

Source: Internet
Author: User

The word "callback/callback method" is certainly not a stranger to our programmers. In the learning and development process of Android, we often hear and use the word "callback", so what is a callback: A method is defined in Class A that uses an interface (Interface) and a method in that interface. However, this method does not have a specific implementation, it needs to be implemented in Class B, class B implementation of the method of the specific business processing, and then passed to class A, for Class A to invoke, this mechanism is called callback. A to B to sound a bit awkward, let's use a simple example to achieve.

We defined a ListView list in activity, a button on each item on the list, and we could have implemented the button's Click event directly in the ListView adapter class, but we didn't, Instead, we define an interface (Interface), invoke the method defined in the interface, and the method itself is implemented in the activity and then passed to the ListView item, which implements the Click event on the Item button. Why is it possible to add an interface directly to the adapter for right-click operation? In general, we do not need to do this, but many times we have to flexible data processing, if the item directly in the implementation of the click can not reach the effect, with the callback operation is easy to display. In short, a callback function allows a user to pass a pointer to a method that needs to be called as a parameter to a function so that the function can flexibly use different methods when dealing with similar events.

- -----------------Small example of XML-----------------------------------

< Relativelayout xmlns:android= "http://schemas.android.com/apk/res/android"
    xmlns:tools= "/http/ Schemas.android.com/tools "
    android:layout_width=" match_parent "
    Android:layout_ height= "Match_parent"
    android:background= "#f0f0f0",
    <listview
        android:id= "@+id/test_lv"
        android:layout_width= "Match_parent"
        android:layout_height= "wrap_content"
        android:divider= "@null "&NBSP;
        android:scrollbars=" none ";
    </listview>
</ Relativelayout>

--------------------the item layout of the ListView----------------------

<relativelayout xmlns:android= "Http://schemas.android.com/apk/res/android"
Xmlns:tools= "Http://schemas.android.com/tools"
Android:layout_width= "Match_parent"
android:layout_height= "Wrap_content"
android:background= "#ffffff" >
<textview
Android:id= "@+id/item_name"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
Android:layout_alignparentleft= "true"
Android:layout_centerinparent= "true"
android:layout_marginleft= "10DP"/>
<button
Android:id= "@+id/item_btn"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
Android:layout_alignparentright= "true"
Android:layout_centerinparent= "true"
android:layout_marginright= "10DP"
android:text= "click Me"/>
</RelativeLayout>

--------------------Activity-------------------------------------------

public class Mainactivity extends Actionbaractivity implements Itestimpl {
Private ListView MTESTLV;
Private Testadapter Madapter;
Private arraylist<string> dataList;
@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);
InitData ();
Initviewsbyid ();
}
private void Initviewsbyid () {
MTESTLV = (ListView) Findviewbyid (R.ID.TEST_LV);
Madapter = new Testadapter (this, dataList, this);
Mtestlv.setadapter (Madapter);
}
private void InitData () {
DataList = new arraylist<string> ();
for (int i = 0; i < i++) {
Datalist.add ("No." + i + "row data");
}
}
@Override
public void Onitemdo (int position) {
int index = position;
Toast.maketext (Mainactivity.this, "You clicked on the" + Index + "line", Toast.length_short). Show ();
}
}

----------Adapter---------------------------

Ublic class Testadapter extends Baseadapter {
Private context context;
Private arraylist<string> dataList;
Private Itestimpl Mtestimpl;
Public Testadapter (context context, arraylist<string> dataList, Itestimpl Mtestimpl) {
This.context = context;
This.datalist = dataList;
This.mtestimpl=mtestimpl;
}
@Override
public int GetCount () {
return Datalist.size ();
}
@Override
Public Object getItem (int position) {
return Datalist.get (position);
}

@Override
public long getitemid (int position) {
return position;
}

@Override
Public View GetView (final int position, View Convertview, ViewGroup parent) {
Viewholder holder = null;
if (null = = Convertview) {
Holder = new Viewholder ();
Convertview = Layoutinflater.from (context). Inflate (R.layout.test_item, NULL);
Holder.tv = (TextView) Convertview.findviewbyid (r.id.item_name);
HOLDER.BTN = (Button) Convertview.findviewbyid (R.ID.ITEM_BTN);
Convertview.settag (holder);
}else{
Holder= (Viewholder) Convertview.gettag ();
}
Holder.tv.setText (Datalist.get (position));
Holder.btn.setOnClickListener (New Onclicklistener () {
@Override
public void OnClick (View v) {
Here we use the callback method to manipulate in the activity
Mtestimpl.onitemdo (position);
}
});
return convertview;
}
public class Viewholder {
Private TextView TV;
Private Button btn;
}
}

----------defined interface--------------------------------

Public interface Itestimpl {
void Onitemdo (int position);
}

But now the Android Component Communication Library framework, such as Eventbus application, very often do not have to implement callbacks, but also to achieve the effect we want, and more convenient and efficient.

Simple application of callback method in Android development

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.