Android - Transfer Data and Change Fragment by Click

Asked

Viewed 410 times

2

Hi guys. I need help to transfer from Fragment when I click my button inside a Listview.

Currently I have a Mainactivity (extends Appcompatactivity) to manage the Fragments.

Below are some images of how the App is

Fragment 1:

inserir a descrição da imagem aqui

Itemlistview:

inserir a descrição da imagem aqui

Fragment 2:

inserir a descrição da imagem aqui

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 for the Adapter, but I was lost when it comes to transferring from one Fragment to another.

On the 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.

2 answers

2


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!

0

Cassius, Good night.

Brother I have a very similar doubt, but I continued to doubt. Because mine is inside a Recycle view.

//Configuração evento de clique
    recyclerBook.addOnItemTouchListener(
            new RecyclerItemClickListener(
                    getActivity(),
                    recyclerBook,
                    new RecyclerItemClickListener.OnItemClickListener() {
                        @Override
                        public void onItemClick(View view, int position) {

                            Book book = new Book();
                            book.setTitle(book.getTitle());
                            book.setWriter(book.getWriter());
                            book.setThumbnailHd(book.getThumbnailHd());

                        }

                        @Override
                        public void onLongItemClick(View view, int position) {

                        }

                        @Override
                        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                        }
                    }
            )
    );

and how I would get that information in the other Fragment?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.