片段在返回时不会更新 - 底部导航视图

时间:2017-07-30 17:48:03

标签: android android-fragments android-recyclerview

我正在使用底部导航视图和片段。 我面临以下问题: 例如,在初始片段中,我有一些信息,当返回时更改片段为空,不显示内容的任何内容。

此片段包含来自Firebase数据库的信息,并使用RecyclerView检索。

屏幕示例: enter image description here enter image description here

Code ActivityHome:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
        final Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        toolbar.setTitle("NomeApp");



        BottomNavigationView bottomNavigationView =(BottomNavigationView) findViewById(R.id.bottomNavView_Bar);
        BottomNavigationViewHelper.disableShiftMode(bottomNavigationView);
        final Menu menu = bottomNavigationView.getMenu();
        //MenuItem menuItem = menu.getItem(0);
        //menuItem.setChecked(true);
        fragmentManager = getSupportFragmentManager();
        final FragmentTransaction transaction = fragmentManager.beginTransaction();

        /* Definindo Fragmento Inicial */
        transaction.replace(R.id.content_home, new MainFragment()).commit();


        bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {

                FragmentManager fragmentManager = getSupportFragmentManager();
                FragmentTransaction transaction = fragmentManager.beginTransaction();

                switch (item.getItemId()){

                    case R.id.ic_home:

                        transaction.replace(R.id.content_home, new MainFragment()).commit();
                        //fragment = new MainFragment();
                        //toolbar.setTitle("Main");
                        item = menu.getItem(0);
                        item.setChecked(true);

                        break;

                    case R.id.ic_explore:
                        transaction.replace(R.id.content_home, new GalleryFragment()).commit();
                        //fragment = new GalleryFragment();
                        toolbar.setTitle("Galeria");
                        item = menu.getItem(1);
                        item.setChecked(true);
                        break;

                    case R.id.ic_calendar:
                        transaction.replace(R.id.content_home, new MaisOpcoes()).commit();
                        //fragment = new MaisOpcoes();
                        toolbar.setTitle("Agendamentos");
                        item = menu.getItem(2);
                        item.setChecked(true);
                        break;

                    case R.id.ic_person:
                        transaction.replace(R.id.content_home, new Opcoes()).commit();
                        //fragment = new Opcoes();
                        toolbar.setTitle("Opções");
                        item = menu.getItem(3);
                        item.setChecked(true);
                        break;



                }

               // FragmentTransaction transaction = fragmentManager.beginTransaction();
                //transaction.replace(R.id.content_home, fragment).commit();

                return false;

            }
        });

Code Fragment Main

public class MainFragment extends Fragment {

    private RecyclerView mCardEmpresaList;

    private DatabaseReference mDatabase;
    private DatabaseReference mDatabaseNomeEmpresa;


    public MainFragment() {
        // Required empty public constructor
    }



    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_main, container, false);
        //setHasOptionsMenu(true); /* Inserir menu no Fragmento */


        /* Referencias Firebase */
        mDatabase = FirebaseDatabase.getInstance().getReference().child("Empresas");
        //mDatabaseNomeEmpresa = FirebaseDatabase.getInstance().getReference().child("Empresas");
        mDatabase.keepSynced(true);


        /* Recupera reciclador */
        mCardEmpresaList = (RecyclerView) view.findViewById(R.id.cardEmpresa_list);

        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        layoutManager.setReverseLayout(true);
        layoutManager.setStackFromEnd(true);

        mCardEmpresaList.setHasFixedSize(true);
        mCardEmpresaList.setLayoutManager(new LinearLayoutManager(getActivity(), LinearLayoutManager.HORIZONTAL, false));



        return view;
    }



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

        FirebaseRecyclerAdapter<EmpresasCard, CardEmpViewHolder> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<EmpresasCard, CardEmpViewHolder>(

                EmpresasCard.class,
                R.layout.card_empresa_row,
                CardEmpViewHolder.class,
                mDatabase

        ) {
            @Override
            protected void populateViewHolder(final CardEmpViewHolder viewHolder, final EmpresasCard model, int position) {


                final String empresa_key = getRef(position).getKey();
                final String nome1 = model.getNome_empresa();


                viewHolder.setName(model.getNome_empresa());
                viewHolder.setCity(model.getCidade());
                viewHolder.setImage(getActivity().getApplicationContext(), model.getFoto_capa());


                /* Clique na view */
                viewHolder.mView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {


                        //Toast.makeText(getActivity().getApplication(), nome1, Toast.LENGTH_LONG).show();
                        Intent perfilEmpIntent = new Intent(getActivity().getApplication(), PerfilEmpresaActivity.class);
                        perfilEmpIntent.putExtra("id_empresa", empresa_key);
                        perfilEmpIntent.putExtra("nome_empresa",nome1);
                        startActivity(perfilEmpIntent);




                    }
                });

            }
        };

        mCardEmpresaList.setAdapter(firebaseRecyclerAdapter);

    }




    public static class CardEmpViewHolder extends RecyclerView.ViewHolder{

        View mView;

        public CardEmpViewHolder(View itemView) {
            super(itemView);

            mView = itemView;

        }

        public void setName(String name){

            TextView cardE_nomeEmpresa = (TextView) mView.findViewById(R.id.tvCardEmp_nome);
            cardE_nomeEmpresa.setText(name);

        }

        public void setCity (String city){

            TextView cardE_nomeCidade = (TextView) mView.findViewById(R.id.tvCardEmp_cidade);
            cardE_nomeCidade.setText(city);

        }

        public void setImage (final Context c, final String image){

            final ImageView cardE_foto = (ImageView) mView.findViewById(R.id.ivCardEmp_image);

            Picasso.with(c).load(image).networkPolicy(NetworkPolicy.OFFLINE).into(cardE_foto, new Callback() {
                @Override
                public void onSuccess() {

                }

                @Override
                public void onError() {

                    Picasso.with(c).load(image).into(cardE_foto);

                }
            });


        }
    }

当我回到初始片段时会自动加载吗?

1 个答案:

答案 0 :(得分:0)

所以尝试将放在方法中:

Public void loaddata(){
    FirebaseRecyclerAdapter<EmpresasCard, CardEmpViewHolder> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<EmpresasCard, CardEmpViewHolder>(

                    EmpresasCard.class,
                    R.layout.card_empresa_row,
                    CardEmpViewHolder.class,
                    mDatabase

            ) {
                @Override
                protected void populateViewHolder(final CardEmpViewHolder viewHolder, final EmpresasCard model, int position) {


                    final String empresa_key = getRef(position).getKey();
                    final String nome1 = model.getNome_empresa();


                    viewHolder.setName(model.getNome_empresa());
                    viewHolder.setCity(model.getCidade());
                    viewHolder.setImage(getActivity().getApplicationContext(), model.getFoto_capa());


                    /* Clique na view */
                    viewHolder.mView.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {


                            //Toast.makeText(getActivity().getApplication(), nome1, Toast.LENGTH_LONG).show();
                            Intent perfilEmpIntent = new Intent(getActivity().getApplication(), PerfilEmpresaActivity.class);
                            perfilEmpIntent.putExtra("id_empresa", empresa_key);
                            perfilEmpIntent.putExtra("nome_empresa",nome1);
                            startActivity(perfilEmpIntent);




                        }
                    });

                }
            };

            mCardEmpresaList.setAdapter(firebaseRecyclerAdapter);
}

并在 onCreateView()中调用该方法。这样的事情:

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_main, container, false);
        //setHasOptionsMenu(true); /* Inserir menu no Fragmento */


        /* Referencias Firebase */
        mDatabase = FirebaseDatabase.getInstance().getReference().child("Empresas");
        //mDatabaseNomeEmpresa = FirebaseDatabase.getInstance().getReference().child("Empresas");
        mDatabase.keepSynced(true);


        /* Recupera reciclador */
        mCardEmpresaList = (RecyclerView) view.findViewById(R.id.cardEmpresa_list);

        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        layoutManager.setReverseLayout(true);
        layoutManager.setStackFromEnd(true);

        mCardEmpresaList.setHasFixedSize(true);
        mCardEmpresaList.setLayoutManager(new LinearLayoutManager(getActivity(), LinearLayoutManager.HORIZONTAL, false));


         loaddata();

        return view;
    }

onStart()中删除代码。

其他方法是尝试将代码放在 onResume()方法中。