我已在Android Studio项目中对回收站视图进行了编程。当我单击此回收视图的项目之一时,打开了一个新片段,以为当我返回到回收站视图屏幕时,回收视图的项目是重复的,我该怎么做,以免发生这种情况?
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
// Inflate the layout for this fragment
View view= inflater.inflate(R.layout.fragment_config, container, false);
final Context context = view.getContext();
recyclerView= view.findViewById(R.id.recyclerConfig);
this.adicionarConfig();
adapterMain adapter = new adapterMain( listaConfig );
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(context);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setHasFixedSize(true);
recyclerView.addItemDecoration(new DividerItemDecoration(context, LinearLayout.VERTICAL));
recyclerView.setAdapter(adapter);
recyclerView.addOnItemTouchListener(
new RecyclerItemClickListener(
context, recyclerView, new RecyclerItemClickListener.OnItemClickListener() {
@Override
public void onItemClick(View view, int position) {
if(position == 0){
FragmentTransaction fr = getFragmentManager().beginTransaction();
fr.replace(R.id.frameA, new configContaFragment());
fr.addToBackStack(null).commit();
}
if(position == 1){
FragmentTransaction fr = getFragmentManager().beginTransaction();
fr.replace(R.id.frameA, new configLayoutFragment());
fr.addToBackStack(null).commit();
}
if(position == 2){
FragmentTransaction fr = getFragmentManager().beginTransaction();
fr.replace(R.id.frameA, new configNotificacoesFragment());
fr.addToBackStack(null).commit();
}
if(position == 3){
FragmentTransaction fr = getFragmentManager().beginTransaction();
fr.replace(R.id.frameA, new sobreFragment());
fr.addToBackStack(null).commit();
}
}
@Override
public void onLongItemClick(View view, int position) {
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
}
}
)
);
return view;
}
public void adicionarConfig(){
Configuracoes config = new Configuracoes( R.mipmap.ic_secure , "Conta", "Informações, Sala, Escola");
this.listaConfig.add(config);
config = new Configuracoes( R.mipmap.ic_config , "Layout", "Cor de fundo, Estilos, Propriedades");
this.listaConfig.add(config);
config = new Configuracoes( R.mipmap.ic_notification , "Notificação", "Tipo de notificação, ");
this.listaConfig.add(config);
config = new Configuracoes( R.mipmap.ic_about , "Sobre nós", null);
this.listaConfig.add(config);
}
适配器类代码如下
public class adapterMain extends RecyclerView.Adapter<adapterMain.MyViewHolder> {
private List<Configuracoes> listaConfig;
public adapterMain(List<Configuracoes> lista) {
this.listaConfig = lista;
}
@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View listaMain = LayoutInflater.from(viewGroup.getContext())
.inflate(R.layout.adapter_main, viewGroup, false);
return new MyViewHolder(listaMain);
}
@Override
public void onBindViewHolder(@NonNull MyViewHolder myViewHolder, int i) {
Configuracoes config = listaConfig.get(i);
myViewHolder.imagem.setImageResource(config.getImagem());
myViewHolder.nome.setText(config.getNome());
myViewHolder.conteudo.setText(config.getConteudo());
}
@Override
public int getItemCount() {
return listaConfig.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder{
ImageView imagem;
TextView nome, conteudo;
public MyViewHolder(@NonNull View itemView) {
super(itemView);
imagem = itemView.findViewById(R.id.imgIcone);
nome = itemView.findViewById(R.id.txtNome);
conteudo = itemView.findViewById(R.id.txtDesc);
}
}
答案 0 :(得分:1)
将adicionarConfig
方法更改为
public void adicionarConfig(){
if(this.listaConfig != null) {
this.listaConfig.clear();
}
Configuracoes config = new Configuracoes( R.mipmap.ic_secure , "Conta", "Informações, Sala, Escola");
this.listaConfig.add(config);
config = new Configuracoes( R.mipmap.ic_config , "Layout", "Cor de fundo, Estilos, Propriedades");
this.listaConfig.add(config);
config = new Configuracoes( R.mipmap.ic_notification , "Notificação", "Tipo de notificação, ");
this.listaConfig.add(config);
config = new Configuracoes( R.mipmap.ic_about , "Sobre nós", null);
this.listaConfig.add(config);
}