2017-02-17 23 views
1

直升机家伙得到了最后一个问题与我的回收站和这里的事情,Recyclerview将不保存更改的项目的第一个和最后一个数据位置

当我点击该按钮,相应的监听器会改变按钮的颜色,并设置启用为false,这是完美的工作,并在大量的数据运行说,从0到99从1到98的视图将保持按钮的颜色和启用设置为false,但位置0和99将不会粘贴适配器,并会apreciate如果你们中的任何人可以帮助找到问题是什么..或者如果我需要改变一些recyclerview设置,我不知道..反正先谢谢你的帮助。

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> { 

private ArrayList<Comanda> list_comandas; 
private LayoutInflater inflater; 

public MyAdapter (Context context, ArrayList<Comanda> list_comandas) 
{ 
    inflater = LayoutInflater.from(context); 
    this.list_comandas = list_comandas; 
} 

@Override 
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) 
{ 
    View view = inflater.inflate(R.layout.row, parent, false); 
    MyViewHolder holder = new MyViewHolder(view); 
    return holder; 
} 

@Override 
public int getItemCount() 
{ 
    return list_comandas.size(); 
} 

@Override 
public void onBindViewHolder(final MyViewHolder holder, final int position) { 

    Comanda comanda = list_comandas.get(position); 

    holder.setData(comanda); 

    for (final Button be : holder.Articulos) 
    { 
     be.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       be.setEnabled(false); 
       be.setBackgroundTintList(inflater.getContext().getResources().getColorStateList(R.color.cboff, null)); 
       mostrarMensaje("Plato Listo " + be.getText().toString()); 
      } 
     }); 
    } 

    holder.btn_cerrar.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      removeItem(holder.getLayoutPosition()); 
     } 
    }); 
} 

private void mostrarMensaje(String mensaje) 
{ 
    Toast.makeText(inflater.getContext(), mensaje, Toast.LENGTH_SHORT).show(); 
} 

public void removeItem(int position) { 
    list_comandas.remove(position); 
    notifyItemRemoved(position); 
} 

public void addItem(int position, Comanda comanda) { 
    list_comandas.add(position, comanda); 
    notifyItemInserted(position); 
} 

class MyViewHolder extends RecyclerView.ViewHolder { 

    //definicioin de variables 

    List<Button> Articulos; 

    private TextView txt_comanda; 
    private TextView txt_mesa; 
    private TextView tid; 
    private TextView torden; 
    private TextView te; 

    String tag=""; 

    private Button btn_cerrar; 
    private Button be; 
    private LinearLayout le; 
    private LinearLayout lyocomanda; 
    private LinearLayout elpadre; 


    public int getScreenWidth() { 
     return Resources.getSystem().getDisplayMetrics().widthPixels; 
    } 

    private MyViewHolder(View itemView) { 
     super(itemView); 

     //inicializacion de controles 

     elpadre = (LinearLayout) itemView.findViewById(R.id.elpadre); 

     lyocomanda = (LinearLayout) itemView.findViewById(R.id.lyocomanda); 

     txt_comanda = (TextView) itemView.findViewById(R.id.txt_comanda); 

     txt_mesa = (TextView) itemView.findViewById(R.id.txt_mesa); 

     tid = (TextView) itemView.findViewById(R.id.tid); 

     torden = (TextView) itemView.findViewById(R.id.torden); 

     btn_cerrar = (Button) itemView.findViewWithTag("primal"); 

     Articulos = new ArrayList<Button>(); 

    } 

    void setData(Comanda comanda) { 

     String cadena= comanda.getOrden(); 

     Integer tope = cadena.length(); 

     Boolean tijera_categoria = false; 
     Boolean tijera_articulo = true; 
     Boolean tijera_contorno = true; 
     Boolean tijera_cambio = true; 
     Boolean creador = false; 

     Integer indisup; 
     Integer indiin = 0; 
     char apuntador; 
     String Buscado = ""; 
     String Buscado_contorno = ""; 
     String Buscado_categoria = ""; 
     Integer id = -1; 
     String comandero="";/////////////////////////////ACA VARIABLES DE SETEO DE DATA 


     if (getAdapterPosition()<10) 
     { 
      comandero = "0"; 
     } 
     else 
     { 
      comandero=""; 
     } 
     txt_comanda.setText(comandero + getAdapterPosition()+ ""); 

     txt_mesa.setText(comanda.getMesa()); 

     tid.setText(comanda.get_id());  //para el proceso de PUT 

     torden.setText(comanda.getOrden());//para el proceso de PUT 

     int copantalla; 
     int dipantalla = getScreenWidth(); 


     if (dipantalla < 2400){ 
      copantalla = 2;// smaller device 
     } 
     else{ 
      copantalla = 4;// 6.5inch device or bigger 
     } 

     elpadre.setLayoutParams(new LinearLayout.LayoutParams(dipantalla/copantalla, ViewGroup.LayoutParams.WRAP_CONTENT)); 

     for (int i = 0; i < tope ; i++) { 

      apuntador =cadena.charAt(i); 

      if (Buscado.equals("Bebidas")) 
      { 
       break; 
      } 
      else 
      { 
       if (apuntador == '$') 
       { 
        break; 
       } 
       else 
       { 
        //EJECUCION PRINCIPAL 

        if (apuntador == '#' && !tijera_categoria) { 
         if (i == 0) { 
          indiin = i + 1; 
         } 
        } 

        if (apuntador == '!' && !tijera_categoria) { 
         tijera_categoria = true; 
         tijera_articulo = false; 
         creador= true; 
         indisup = i; 
         id =id+1; 

         Buscado = cadena.substring(indiin, indisup); 
         indiin = indisup + 1; 

         Buscado_categoria = Buscado; 

         tag= txt_comanda.getText().toString() + id; 

         be = new Button(inflater.getContext()); 
         be.setTag(tag); 
         int idr = Integer.parseInt(tag); 
         be.setId(idr); 
         Articulos.add(be); 
        } 

        if (apuntador == '%' && !tijera_articulo) { 
         indisup = i; 
         tijera_articulo = true; 
         tijera_contorno = false; //aca viejo listener de be 

         Buscado = cadena.substring(indiin, indisup); 

         indiin = indisup + 1; 

         //this.be.setTextSize((inflater.getContext().getResources().getDimension(R.dimen.txt_size))/2); 
         if (Buscado_categoria.equals("Fondos")) { 
          be.setBackgroundTintList(inflater.getContext().getResources().getColorStateList(R.color.fondos, null)); 
         } 
         if (Buscado_categoria.equals("Entradas")) { 
          be.setBackgroundTintList(inflater.getContext().getResources().getColorStateList(R.color.entradas, null)); 
         } 
         if (Buscado_categoria.equals("Postres")) { 
          be.setBackgroundTintList(inflater.getContext().getResources().getColorStateList(R.color.postres, null)); 
         } 
         be.setText(Buscado); 
         be.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT)); 
         lyocomanda.addView(be); 
        } 

        if (apuntador == '*' && !tijera_contorno) { 
         indisup = i; 
         tijera_cambio = false; 

         Buscado = cadena.substring(indiin, indisup); 
         indiin = indisup + 1; 

         if (!Buscado.equals("")) { 
          Buscado_contorno = Buscado; 
          te = new TextView(inflater.getContext()); 
          te.setText(Buscado); 
          te.setLayoutParams(new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)); 
          //te.setTextSize((inflater.getContext().getResources().getDimension(R.dimen.txt_size))/2); 

          if (creador){ 
           le = new LinearLayout((inflater.getContext())); 
           creador=false; 
          } 

          le.addView(te); 

         } 

        } 

        if (apuntador == '#' && !tijera_cambio) { 
         indisup = i; 
         tijera_contorno = true; 
         tijera_cambio = true; 
         tijera_categoria = false; 

         Buscado = cadena.substring(indiin, indisup); 
         indiin = indisup + 1; 

         if (!Buscado_contorno.equals("")) { 
          le.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)); 
          le.setOrientation(LinearLayout.VERTICAL); 
          le.setBackground(inflater.getContext().getDrawable(customborder)); 
          lyocomanda.addView(le); 
         } 

        } 

       }//FIN DE LA EJECUCION PRINCIPAL 
      }//DEL INICIO DE EJECUCION PRINCIPAL 

     }// DEL FOR 

    } 

} 
} 
+1

我会建议你启用的状态存储在您的viewHolder状态,并相应地设置在按钮状态的'onBindViewHolder'。按照相同的背景颜色模式。关键在于,随着所有的回收和物料的继续,最好是控制每个物品的状态,而不是依赖Recyclerview。只是补充一点,我对第一个项目有类似的问题,并且调试意识到对于第一个视图'onBindViewHolder'被多次调用,并且它已经有一段时间了。 – Dibzmania

+0

听起来不错,会尝试并报告回来。 –

回答

0

好,好,我已经通过增加RecyclerView.RecycledViewPool解决了这个问题,它带有的5默认的,如果你的观点都是偶数,却没有这个问题上保持所有好处都没有,但是,如果你喜欢在我的情况下改变视图,一旦你将池从5增加到DataArray.size(),你开始在每个视图中获得垃圾。

这样的,现在我已成功地保存按钮启用状态,并为每种颜色与

recyclerView.getRecycledViewPool().setMaxRecycledViews(0,DataArray.size()); 

查看无论DataArray.size(),但我是在如果意见everyother观点越来越垃圾甚至没有,所以我不得不改变另一个设置是动态也与我的数据

recyclerView.setItemViewCacheSize(DataArray.size()); 

所以现在我可以有回收工作正常,希望这可以帮助任何人..

谢谢@Dibzmania的回复:) .. ps。哦BTW @Dibzmania我想你告诉我,它没有工作,但它让我在正确的轨道上..

相关问题