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); –