Rumah > Java > javaTutorial > teks badan

Bagaimanakah anda boleh melaksanakan komunikasi antara Fragmen dan Penyesuai dalam Android?

Patricia Arquette
Lepaskan: 2024-11-15 07:20:02
asal
753 orang telah melayarinya

How can you implement communication between a Fragment and an Adapter in Android?

Interface Implementation between Fragment and Adapter

In the realm of Android development, interfacing between fragments and adapters is a common task. To facilitate this, a custom CursorAdapter can be employed with an adapter interface embedded within. This interface serves as a communication channel between the adapter and the fragment.

Consider a scenario where a fragment (MyListFragment) contains a ListView and a customized CursorAdapter. Each row in the list contains a button, and upon clicking it, an action needs to be performed in the fragment. To achieve this, an interface, AdapterInterface, is defined within the adapter.

public class MyListAdapter extends CursorAdapter {

    public interface AdapterInterface {
        public void buttonPressed();
    }

    private AdapterInterface buttonListener;

    // ...
}
Salin selepas log masuk

Within the adapter's bindView method, an OnClickListener is set for the button in each row.

@Override
public void bindView(final View view, final Context context, final Cursor cursor) {
    // ...
    holder.button.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            // some action
            // need to notify MyListFragment
            if (buttonListener != null) {
                buttonListener.buttonPressed();
            }
        }
    });
}
Salin selepas log masuk

The AdapterInterface should be implemented in the fragment (MyListFragment) to handle the button click event.

public class MyListFragment extends Fragment implements AdapterInterface {

    @Override
    public void buttonPressed() {
        // some action
    }
}
Salin selepas log masuk

To establish communication between the adapter and fragment, a new constructor is introduced in the adapter, along with an instance variable to hold the interface reference.

AdapterInterface buttonListener;

public MyListAdapter (Context context, Cursor c, int flags, AdapterInterface buttonListener)
{
  super(context,c,flags);
  this.buttonListener = buttonListener;
}
Salin selepas log masuk

When creating the adapter, the fragment is passed as the argument to the constructor to provide the interface implementation.

MyListAdapter adapter = new MyListAdapter (getActivity(), myCursor, myFlags, this);
Salin selepas log masuk

This approach ensures that when the button in the adapter is clicked, the buttonPressed method in the fragment is invoked, facilitating the desired communication between the adapter and the fragment.

Atas ialah kandungan terperinci Bagaimanakah anda boleh melaksanakan komunikasi antara Fragmen dan Penyesuai dalam Android?. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Artikel terbaru oleh pengarang
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan