0
我正在使用帶片段的底部導航視圖。 我面臨以下問題: 例如,在最初的片段中,我有信息表明,當返回值爲空時更改片段時,不顯示任何內容。片段不返回時更新 - 底部導航視圖
此片段包含來自Firebase數據庫的信息,並使用RecyclerView進行檢索。
代碼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;
}
});
代碼片段主要
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);
}
});
}
}
說一個,當我回到它自動加載初始片段的方式?
我試過兩種方法。它僅在應用程序第一次打開時繼續渲染數據。 @BrunoFerreira – TiagoIB
嘗試清潔項目並再次運行 –
嘗試刪除此行太多mCardEmpresaList.setHasFixedSize(true); –