我有一个底部导航视图,其中包含3个选项和片段。
当我进入具有tabLayout且带有回收站视图的片段时,第一次正确加载列表,但是在更改片段并返回列表后,该列表未显示。
代码:
RecyclerView的片段。
public class CadastroContaPatrimonialFragment extends Fragment {
@BindView(R.id.recyclerView_conta_patrimonial)
public RecyclerView recyclerViewContaPatrimonial;
private CadastroContaPatrimonialAdapter adapter;
private List<ContaPatrimonial> listaContas;
public static CadastroContaPatrimonialFragment newInstance() {
CadastroContaPatrimonialFragment fragment = new CadastroContaPatrimonialFragment();
return fragment;
}
public CadastroContaPatrimonialFragment() {}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_cadastro_conta_patrimonial, container, false);
ButterKnife.bind(this, view);
if (this.listaContas == null) {
configuraRecycler();
}
return view;
}
private void configuraRecycler() {
MainActivity.loading(true);
LinearLayoutManager manager = new LinearLayoutManager(getContext());
recyclerViewContaPatrimonial.setLayoutManager(manager);
ContaPatrimonialDAO contaPatrimonialDAO = PatrimonioDatabase.getInstance(getContext()).criaContaPatrimonialDAO();
this.listaContas = contaPatrimonialDAO.getAll();
adapter = new CadastroContaPatrimonialAdapter(this.listaContas);
recyclerViewContaPatrimonial.setAdapter(adapter);
recyclerViewContaPatrimonial.addItemDecoration(new DividerItemDecoration(getContext(), DividerItemDecoration.VERTICAL));
MainActivity.loading(false);
}
}
适配器:
public class CadastroContaPatrimonialAdapter extends RecyclerView.Adapter<CadastroContaPatrimonialAdapter.ContaPatrimonialViewHolder> {
List<ContaPatrimonial> contas;
public CadastroContaPatrimonialAdapter(List<ContaPatrimonial> contas) {
this.contas = contas;
}
@NonNull
@Override
public ContaPatrimonialViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
return new ContaPatrimonialViewHolder(LayoutInflater.from(parent.getContext())
.inflate(R.layout.conta_patrimonial_item_recycler, parent, false));
}
@Override
public void onBindViewHolder(@NonNull ContaPatrimonialViewHolder holder, int position) {
holder.txtContaDescricao.setText(contas.get(position).getDescricao());
}
@Override
public int getItemCount() {
return contas != null ? contas.size() : 0;
}
/* VIEW HOLDER */
public class ContaPatrimonialViewHolder extends RecyclerView.ViewHolder {
@BindView(R.id.item_conta_patr_descricao)
public TextView txtContaDescricao;
public ContaPatrimonialViewHolder(@NonNull View itemView) {
super(itemView);
ButterKnife.bind(this, itemView);
}
}
}
切换片段:
private void trocaFragment(Fragment fragment, boolean backStack) {
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction tx = fm.beginTransaction();
tx.replace(R.id.main_frame, fragment);
if (backStack) {
tx.addToBackStack(null);
}
tx.commit();
}
答案 0 :(得分:0)
您的RecyclerView
不会在片段切回时消失,只是无法正确呈现。当片段从FragmentTransaction.replace
切回时,由于视图已被破坏,它再次调用Fragment.onCreateView
。在您的情况下,由于RecyclerView
之前已初始化,因此您可以跳过配置listaContas
的过程。要解决此问题,您应该删除if (this.listaContas == null)
条件:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_cadastro_conta_patrimonial, container, false);
}
@Override public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
ButterKnife.bind(this, view);
configuraRecycler();
}