如何将数据从recyclerview适配器传递到fragment.I已经检查了堆栈溢出中的链接,但无法获取正确的代码。
Most efficient and professional method
Using CallBacks to communicate between adapter and fragment:
In Adapter:
private Callback mlistener;
public Interface Callback extends EventListener{
public void onPassData(int data);
}
public void setListener(Callback listener)
{
this.mlistener = listener;
}
And then you can Call listener method from anywhere in adapter as:
mlistener.onPassData(4);
Receive Call back in Fragment:
adapter.setListener(new Callback) {
@Override
public void onPassData(int data)
{
//YOu will receive data here whenever you call onPassData
}}
public interface AdapterCallback{
void onReponse(Object data);
}
CustomAdapter(...., AdapterCallback mCallback){
....
}
Call in Adapter like:
mCallback.onResponse(data);
CustomAdapter obj = new CustomAdapter(....,new AdapterCallback() {
@Override
public void onReponse(Object data) {
}
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.