Giới thiệu
Interface Listener tùy chỉnh (Custom Listener Interface) cho phép giao tiếp giữa các thành phần (Fragment ↔ Activity, View ↔ Presenter, v.v.) theo hướng giải cáp (decoupled).
Các bước thực hiện
Bước 1: Tạo dự án & Giao diện
Layout: một TextView hiển thị dữ liệu nhận được từ interface.
Bước 2: Định nghĩa Interface (src/OnCustomClickListener.java)
package com.example.myapplication;
public interface OnCustomClickListener {
void onCustomClick(String data);
}
Bước 3: Fragment/Component phát sự kiện (src/FirstFragment.java)
package com.example.myapplication;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class FirstFragment extends Fragment {
private OnCustomClickListener listener;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment, container, false);
TextView textView = view.findViewById(R.id.text);
textView.setOnClickListener(v -> {
if (listener != null) {
listener.onCustomClick("Dữ liệu từ FirstFragment: " + System.currentTimeMillis());
}
});
return view;
}
// Activity phải implement interface này
@Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof OnCustomClickListener) {
listener = (OnCustomClickListener) context;
} else {
throw new RuntimeException(context.toString() + " phải implement OnCustomClickListener");
}
}
@Override
public void onDetach() {
super.onDetach();
listener = null; // Tránh memory leak
}
}
Bước 4: Activity implement Interface (src/MainActivity.java)
package com.example.myapplication;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.widget.TextView;
public class MainActivity extends FragmentActivity implements OnCustomClickListener {
TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = findViewById(R.id.text);
}
@Override
public void onCustomClick(String data) {
// Nhận dữ liệu từ Fragment
textView.setText("Nhận được: " + data);
}
}
Bước 5: Layout Fragment (res/layout/fragment.xml)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click để gửi dữ liệu"
android:textSize="20sp" />
</LinearLayout>
Bước 6: Layout Activity chứa Fragment (res/layout/activity_main.xml)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<fragment
android:name="com.example.myapplication.FirstFragment"
android:id="@+id/fragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
Chạy ứng dụng
Nhấn Run. Màn hình hiển thị Fragment với TextView "Click để gửi dữ liệu". Nhấn vào → Activity chính nhận dữ liệu và hiển thị "Nhận được: Dữ liệu từ FirstFragment: ...".

Các biến thể phổ biến
- Listener có nhiều phương thức:
onSuccess(), onError(), onProgress(). - Sử dụng cho RecyclerView Adapter: Interface
OnItemClickListenervới tham số position. - Kotlin: Dùng function type
(String) -> Unithoặc SAM interface.