Android Implementing Custom Listener Methods in Custom Classes

  • 2021-11-30 01:18:13
  • OfStack

Listeners can be said to be one of the most commonly used things in Android development. Through the listener, we can listen to all kinds of changes of objects and do some necessary processing, which is quite useful and simple to use. In fact, the listener is equivalent to the callback function in C + +, and the callback is executed when the condition is met.

Most of the time, we also need to implement a listener for property changes in the custom control class, and realize the same function as the native control listener. The following steps illustrate the implementation and use of custom listeners (taking the custom class MyClass load completion listener as an example):

1. Implementation of custom listener:

1. Define a loading completion listening interface


// Load listening interface 
public static interface LoadingListener {
 public void onFinishedLoading(boolean success);
}

In the MyClass custom class, a loading completion listening interface LoadingListener is defined, and a method onFinishedLoading is declared in the interface to realize the specific process of the class using the interface, and success indicates whether the loading is successful.

2. Declare the interface and implement a method to set the listener

Declare the LoadingListener interface

private LoadingListener mLoadingListener;

Provides a method for setting LoadingListener


public void setLoadingListener(LoadingListener listener) {
 mLoadingListener = listener;
}

3. The method of calling the interface in the corresponding case

When MyClass is loaded, the method of listening interface is called

mLoadingListener.onFinishedLoading(true);

2. Use of custom listeners:

1. Implement the method in the listening interface for the instance of MyClass


mMyClass.setLoadingListener(new MyClass.LoadingListener() {
   @Override
   public void onFinishedLoading(boolean success) {
   // Perform the necessary processing 
  }
}

Custom logic can be implemented in onFinishedLoading function.

Additional knowledge: android Activity sets the callback method for Fragment

This implementation method is very simple and can be divided into three steps:

1. Define interfaces in fragment

2. Implement this interface when activity is defined

3. Convert Context into corresponding interface in onAttach method of fragment;

The code is as follows:


public class AllFragment extends Fragment {

//...... Code omission 
 

 @Override
 public void onAttach(Context context) {
  super.onAttach(context);
  mOnChangeTabListener = (OnChangeTabListener) context;
 }

 public interface OnChangeTabListener{
  void onChangeTab(String tag);
 }
}

This interface is implemented when activity is defined, as follows:


public class SearchActivity extends AppCompactActivity implements AllFragment.OnChangeTabListener {

 // Code omission 
 @Override
 public void onChangeTab(String tag) {
  if (" Application ".equals(tag)){
   mViewPager.setCurrentItem(1);
  }else {
   mViewPager.setCurrentItem(2);
  }
 }
}


Related articles: