【发布时间】:2017-02-17 02:53:08
【问题描述】:
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
}
}
}
【问题讨论】:
-
我建议您将启用状态作为状态存储在您的 viewHolder 中,并相应地在您的
onBindViewHolder中设置按钮状态。对background color遵循相同的模式。关键是,随着所有的回收和东西的进行,最好控制每个项目的状态,而不是依赖 Recyclerview。补充一下,我在第一项上遇到了类似的问题,调试后发现第一个视图onBindViewHolder被多次调用,并且已经有一段时间了。 -
听起来不错,会尝试报告。
标签: android android-studio android-recyclerview