Android - Transfer data and change Fragment per Click

2

Hi, guys. I need help transferring from Fragment when I click on my button inside a ListView.

Currently I have a MainActivity (extends AppCompatActivity) to manage the Fragments.

Below are some screenshots of how the App is

Fragment 1:

ItemListView:

Fragment2:

I want when I click the search button it goes to Fragment 2 passing the Id or the complete object to that other fragment.

I tried the Adapter, but I got lost when it comes to transferring from one Fragment to another.

No adapter I have something like:

 holder.search = (ImageView) view.findViewById(R.id.iv_search);
            holder.search.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    //Código para transferir o Fragment aqui?
                }
            });

How to solve this problem? Thanks in advance.

    
asked by anonymous 02.06.2016 / 17:42

1 answer

2

Hello, Victor. I'll write a very quick code here to try to help you. This data transfer between Fragments will always have Activity as an intermediary. The Fragment that received the click will pass the element to the Activity and Activity will transfer the element to the second Fragment.

So, to begin with, the idea is that your Activity implements an interface that the Fragment that will suffer the click knows and calls. So let's create the interface that will be called when there was a click on the fragment:

public interface OnPesquisarClickListener  {
    public void onPesquisarClick(MeuObjeto meuObjeto);
}

Then let's make Activity inherit from 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)...
    }

}

Okay, now in your clickable fragment, during the onAttach() method, which is called during the fragment lifecycle when a fragment is appended to Activity, capture the Activity using polymorphism for terms in one hand instance of OnPesquisarClickListener. This way:

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 that 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 questions just say that I try to explain in other ways. Abs!

    
04.06.2016 / 17:22