Hi, Victor. I’m gonna write a code real quick here to try and help you out. These data transfers between Fragments will always have Activity as an intermediary. The Fragment that received the click will pass the element to Activity and Activity will transfer the element to the second Fragment.
So, to begin, the idea is that your Activity implements an interface that the click Fragment knows and calls. So, let’s create such interface that will be called when there was the click on Fragment:
public interface OnPesquisarClickListener {
public void onPesquisarClick(MeuObjeto meuObjeto);
}
And we will then have Activity inherit this interface and implement the method:
public class MinhaActivity extends AppCompact implements OnPesquisarClickListener {
//... Outros métodos comuns da Activity
public void onPesquisarClick(MeuObjeto meuObjeto) {
//Transfira o objeto para o fragmento dois aqui. Por exemplo, fragmentoDois.pesquisar(meuObjeto)...
}
}
Right, now in your Ragment that will be clicked, during the method onAttach()
, which is called during the Fragment lifecycle when a Fragment is attached to Activity, capture the Activity using polymorphism to terms in hands an instance give Onpesquisarclicklistener. Thus:
public void FragmentTwo extends Fragment {
OnPesquisarClickListener onPesquisarClickListener
@Override
public void onAttach(Context context) {
if(context instanceof OnPesquisarClickListener) {
onPesquisarClickListener = (OnPesquisarClickListener) context;
}
super.onAttach(context);
}
//Métodos comuns ao fragment
}
And in that method you described will call the instance of Onpesquisarclicklistener we received:
holder.search = (ImageView) view.findViewById(R.id.iv_search);
holder.search.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//onPesquisarClickListener.onPesquisarClick(...
}
});
Any doubts just say that I try to explain in other ways. Abs!
That’s exactly what I wanted. Thanks a lot, man. It helped a lot!
– Victor Magalhães