Recyclerview不会保存第一个和最后一个数据位置的已更改项目

时间:2017-02-17 02:53:08

标签: android android-studio android-recyclerview

Helo家伙在我的Recycler上遇到了最后一个问题,就是这个问题,

当我单击按钮时,相应的监听器将更改按钮颜色并将启用设置为false,这非常有效,并且在大量数据中,从0到99,从1到98的视图将保持按钮颜色并且启用设置为false但位置0和99将不会..将粘贴适配器并且如果你们中的任何人可以帮助找到问题是什么,或者如果我需要更改一些我不知道的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 个答案:

答案 0 :(得分:1)

好吧,好吧,我已经通过增加RecyclerView.RecycledViewPool来解决了这个问题,它默认值为5,如果你的观点都是均匀的,那么保持一切都没有问题,但是,如果你喜欢的话在我的情况下,更改视图,一旦你将池从5增加到DataArray.size(),你就开始在每个视图中获取垃圾。

所以到目前为止我已经设法保存按钮启用状态和每个视图的颜色,无论DataArray.size()与

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

但如果视图不是这样的话,我会在其他视图中发现垃圾我必须将其他设置更改为动态数据

 recyclerView.setItemViewCacheSize(DataArray.size());

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

感谢@Dibzmania的回复:) .. ps。哦顺便说一下@Dibzmania我试过你告诉我的东西但它没有用,但它让我走上了正确的轨道..