Save status of a fragment with Recyclerview [closed]

-1
@Override
    public void onStart() {
        super.onStart();

        FirebaseRecyclerAdapter<Item,SandwichViewHolder> firebaseRecyclerAdapter=new FirebaseRecyclerAdapter<Item, SandwichViewHolder>(

                Item.class,
                R.layout.row,
                SandwichViewHolder.class,
                mDatabase
        ) {
            @Override
            protected void populateViewHolder(SandwichViewHolder viewHolder, Item model, int position) {

                final String sandwich_key = getRef(position).getKey();

                viewHolder.setTitulo(model.getNombreItem());
                viewHolder.setDescripcion(model.getDescripcionItem());
                viewHolder.setImagen(getContext(),model.getImagenItem());

                viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        mListener.onSandwichSeleccionado(sandwich_key);

                    }
                });

            }


        };

        mReciclerSandwich.setAdapter(firebaseRecyclerAdapter);
    }

Hello please I need help, I have created a recyclerview in fragments, each one has its recycler the problem is that when changing the fragment in the navigation drawer these recycler reloads. my question is how can I save the state of the fragment or the position of the recycler so that it is not loading?

    
asked by Juan Solano 20.03.2017 в 04:15
source

1 answer

1

Let's see, you can do something like this:

private void changeFragment(Fragment f, String tag) {
    FragmentManager fragmentManager = getSupportFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    if (fragmentManager.findFragmentByTag(tag) == null) {
        fragmentTransaction.add(R.id.frameContainer, f, tag);
    } else {
        f = fragmentManager.findFragmentByTag(tag);
        fragmentTransaction.replace(R.id.frameContainer, f, tag);
    }
    fragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE).commit();
}

And by calling that method you do:

TuFragment f = new TuFragment();
changeFragment(f, "el_tag_que_quieras_asociar_a_tu_fragment");

What you achieve this way is that if the fragment is not created yet, you create it, but if it is already created and added to the fragmentManager then it looks for it and loads it.

Try it and tell me how it goes.

Greetings.

    
answered by 20.03.2017 / 16:28
source