我真的可以在这里使用一些帮助。有几天试图弄清楚什么是错的,无法找到它。这是事情
每次我执行data.remove(position)notifyitemremove(position)它都会对项目进行解除和删除,但项目会很好地删除,直到列表到达第一项,然后应用程序崩溃。
然后我使用notifyitemrangechange(position,data.size())并且id保持位置跟踪良好但是视图中发生了某些事情并且他们开始从已删除的视图中获取垃圾数据...这个问题让我发疯,生病了图像和适配器,因为每个都发生在那里。
重要的是要注意,如果我从右到左删除视图,一切都很完美。回收者水平滚动。
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder{
private ArrayList<Comanda> list_comandas;
private LayoutInflater inflater;
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(MyViewHolder holder, int position) {
Comanda comanda = list_comandas.get(position);
holder.setData(comanda, position);
// mostrarMensaje("agregado:" + position);
private void mostrarMensaje(String mensaje)
{
Toast.makeText(inflater.getContext(), mensaje, Toast.LENGTH_SHORT).show();
}
class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
//definicioin de variables
TextView txt_comanda;
TextView txt_mesa;
TextView tid;
TextView torden;
TextView te;
Button btn_cerrar;
Button be;
LinearLayout le;
private LinearLayout lyocomanda;
private int position;
private int anchomanda = Math.round(inflater.getContext().getResources().getDimension(R.dimen.parents_size));
public MyViewHolder(View itemView) {
super(itemView);
//inicializacion de controles
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");
btn_cerrar.setOnClickListener(MyViewHolder.this);
}
void setData(Comanda comanda, int position) {
//setear la data
this.position = position;
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 tag="";
this.txt_comanda.setText(this.position+"");
this.txt_mesa.setText(comanda.getMesa());
this.tid.setText(comanda.get_id());
this.torden.setText(comanda.getOrden());
this.btn_cerrar.setOnClickListener(MyViewHolder.this);
for (int i = 0; i < tope ; i++) {
apuntador =cadena.charAt(i);
if (Buscado.equals("Bebidas"))
{
cadena="";
break;
}
else
{
if (apuntador == '$')
{
cadena="";
break;
}
else
{
//EJECUCION PRINCIPAL
if (apuntador == '#' && !tijera_categoria) {
if (i == 0) {
indiin = i + 1;
}
}
if (apuntador == '!' && !tijera_categoria) {
tijera_categoria = true;
tijera_articulo = false;
indisup = i;
id =id+1;
Buscado = cadena.substring(indiin, indisup);
indiin = indisup + 1;
Buscado_categoria = Buscado;
tag= position + ""+id;
this.be = new Button(inflater.getContext());
creador= true;
this.be.setTag(tag);
// this.be.setId(position);
this.be.setOnClickListener(MyViewHolder.this);
}
if (apuntador == '%' && !tijera_articulo) {
indisup = i;
tijera_articulo = true;
tijera_contorno = false;
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")) {
this.be.setBackgroundTintList(inflater.getContext().getResources().getColorStateList(R.color.fondos, null));
}
if (Buscado_categoria.equals("Entradas")) {
this.be.setBackgroundTintList(inflater.getContext().getResources().getColorStateList(R.color.entradas, null));
}
if (Buscado_categoria.equals("Postres")) {
this.be.setBackgroundTintList(inflater.getContext().getResources().getColorStateList(R.color.postres, null));
}
be.setText(Buscado);
be.setLayoutParams(new LinearLayout.LayoutParams(this.anchomanda, LinearLayout.LayoutParams.WRAP_CONTENT));
this.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;
this.te = new TextView(inflater.getContext());
this.te.setText(Buscado);
this.te.setLayoutParams(new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
this.te.setTextSize((inflater.getContext().getResources().getDimension(R.dimen.txt_size)) / 2);
if (creador){
this.le = new LinearLayout((inflater.getContext()));
creador=false;
}
this.le.addView(this.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("")) {
this.le.setLayoutParams(new LinearLayout.LayoutParams(anchomanda, ViewGroup.LayoutParams.WRAP_CONTENT));
this.le.setOrientation(LinearLayout.VERTICAL);
this.le.setBackground(inflater.getContext().getDrawable(customborder));
this.lyocomanda.addView(this.le);
//cadena ="";
}
}
}//FIN DE LA EJECUCION PRINCIPAL
}//DEL INICIO DE EJECUCION PRINCIPAL
}// DEL FOR
}
@Override
public void onClick(View v){
if (v.getTag().equals("primal")){
mostrarMensaje("borrado" + position);
removeItem(position);
}else{
mostrarMensaje("BOTON NO CONFIGURADO:" + v.getTag().toString());
}
}
private void removeItem(int position) {
list_comandas.remove(position);
notifyItemRemoved(position);
//notifyItemRangeChanged(position,list_comandas.size());
//notifyItemRangeChanged(position, getItemCount() - position);
// notifyDataSetChanged();
}
private void addItem(int position, Comanda comanda) {
list_comandas.add(position, comanda);
notifyItemInserted(position);
notifyItemRangeChanged(position,list_comandas.size());
//notifyDataSetChanged();
}
}
}
基本适配器回收器行是:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:focusableInTouchMode="true"
android:layout_height="wrap_content">
<!-- Recycler View Item Row -->
<RelativeLayout
android:id="@+id/comanda"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:focusableInTouchMode="true"
android:background="@drawable/customborder">
<RelativeLayout
android:id="@+id/lyocabeza"
android:layout_width="@dimen/parents_size"
android:layout_height="wrap_content">
<TextView
android:id="@+id/lbl_comanda"
android:layout_alignParentTop="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="@dimen/txt_size"
android:text="Comanda:"/>
<TextView
android:id="@+id/txt_comanda"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="@dimen/txt_size"
android:layout_toRightOf="@id/lbl_comanda"
android:hint="00000"/>
<TextView
android:id="@+id/txt_mesa"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:textSize="@dimen/txt_size"
android:hint="9999"/>
<TextView
android:id="@+id/lbl_mesa"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="@dimen/txt_size"
android:text="Mesa:"
android:layout_toLeftOf="@+id/txt_mesa"/>
<Button
android:id="@+id/btn_cerrar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/lbl_comanda"
android:text="Cerrar Comanda"
android:tag="primal"/>
<TextView
android:id="@+id/tid"
android:layout_width="1dp"
android:layout_height="1dp"
android:visibility="invisible"/>
<TextView
android:id="@+id/torden"
android:layout_width="1dp"
android:layout_height="1dp"
android:visibility="invisible"/>
</RelativeLayout>
<ScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/lyocabeza">
<LinearLayout
android:id="@+id/lyocomanda"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<!-- AUTO GENERATED CONTROLS -->
</LinearLayout>
</ScrollView>
</RelativeLayout>
</LinearLayout>
因为你可以看到我的数据是动态的,我需要或多或少的控制取决于数据所以行CANT是静态的,这就是现在,提前感谢任何帮助
答案 0 :(得分:1)
您的方法的主要问题是在position
内使用MyViewHolder
。您应该在onBindViewHolder
上注意到这一点:
RecyclerView不会调用此方法 再次,如果项目的位置在数据集中发生变化,除非项目本身是 无效或新职位无法确定。出于这个原因,你应该只 在获取内部的相关数据项时使用
position
参数 这种方法并不应该保留它的副本。如果您以后需要某个项目的位置 on(例如在点击监听器中),使用{@link ViewHolder#getAdapterPosition()},它将具有更新的适配器位置。
因此,在删除项目后,其他人的位置会受到影响,并且在触发删除事件时可能会使用错误的位置结束,请记住视图已被回收。
您应该使用holder.getAdapterPosition()
参考当前项目的位置。
尝试这样的事情:
处理ViewHolder外的onCLickListener
<强> MyViewHolder 强>
class MyViewHolder extends RecyclerView.ViewHolder {
//definicioin de variables
TextView txt_comanda;
TextView txt_mesa;
TextView tid;
TextView torden;
TextView te;
Button btn_cerrar;
Button be;
LinearLayout le;
private LinearLayout lyocomanda;
private int anchomanda = Math.round(inflater.getContext().getResources().getDimension(R.dimen.parents_size));
public MyViewHolder(View itemView) {
super(itemView);
//inicializacion de controles
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");
}
void setData(Comanda comanda) {
//setear la data
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 tag="";
this.txt_comanda.setText(this.position+"");
this.txt_mesa.setText(comanda.getMesa());
this.tid.setText(comanda.get_id());
this.torden.setText(comanda.getOrden());
for (int i = 0; i < tope ; i++) {
apuntador =cadena.charAt(i);
if (Buscado.equals("Bebidas"))
{
cadena="";
break;
}
else
{
if (apuntador == '$')
{
cadena="";
break;
}
else
{
//EJECUCION PRINCIPAL
if (apuntador == '#' && !tijera_categoria) {
if (i == 0) {
indiin = i + 1;
}
}
if (apuntador == '!' && !tijera_categoria) {
tijera_categoria = true;
tijera_articulo = false;
indisup = i;
id =id+1;
Buscado = cadena.substring(indiin, indisup);
indiin = indisup + 1;
Buscado_categoria = Buscado;
tag= position + ""+id;
this.be = new Button(inflater.getContext());
creador= true;
this.be.setTag(tag);
// this.be.setId(position);
this.be.setOnClickListener(MyViewHolder.this);
}
if (apuntador == '%' && !tijera_articulo) {
indisup = i;
tijera_articulo = true;
tijera_contorno = false;
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")) {
this.be.setBackgroundTintList(inflater.getContext().getResources().getColorStateList(R.color.fondos, null));
}
if (Buscado_categoria.equals("Entradas")) {
this.be.setBackgroundTintList(inflater.getContext().getResources().getColorStateList(R.color.entradas, null));
}
if (Buscado_categoria.equals("Postres")) {
this.be.setBackgroundTintList(inflater.getContext().getResources().getColorStateList(R.color.postres, null));
}
be.setText(Buscado);
be.setLayoutParams(new LinearLayout.LayoutParams(this.anchomanda, LinearLayout.LayoutParams.WRAP_CONTENT));
this.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;
this.te = new TextView(inflater.getContext());
this.te.setText(Buscado);
this.te.setLayoutParams(new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
this.te.setTextSize((inflater.getContext().getResources().getDimension(R.dimen.txt_size)) / 2);
if (creador){
this.le = new LinearLayout((inflater.getContext()));
creador=false;
}
this.le.addView(this.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("")) {
this.le.setLayoutParams(new LinearLayout.LayoutParams(anchomanda, ViewGroup.LayoutParams.WRAP_CONTENT));
this.le.setOrientation(LinearLayout.VERTICAL);
this.le.setBackground(inflater.getContext().getDrawable(customborder));
this.lyocomanda.addView(this.le);
//cadena ="";
}
}
}//FIN DE LA EJECUCION PRINCIPAL
}//DEL INICIO DE EJECUCION PRINCIPAL
}// DEL FOR
}
<强> MyAdapter 强>
...
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
Comanda comanda = list_comandas.get(position);
holder.setData(comanda);
// set onCLickListener
holder.btn_cerrar.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
removeItem(holder.getAdapterPosition());
}
});
// mostrarMensaje("agregado:" + position);
}
...
或者您也可以处理ViewHolder中的onCLickListener
<强> MyViewHolder 强>
class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
//definicioin de variables
TextView txt_comanda;
TextView txt_mesa;
TextView tid;
TextView torden;
TextView te;
Button btn_cerrar;
Button be;
LinearLayout le;
private LinearLayout lyocomanda;
private int anchomanda = Math.round(inflater.getContext().getResources().getDimension(R.dimen.parents_size));
public MyViewHolder(View itemView) {
super(itemView);
//inicializacion de controles
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");
btn_cerrar.setOnClickListener(MyViewHolder.this);
}
void setData(Comanda comanda) {
//setear la data
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 tag="";
this.txt_comanda.setText(this.position+"");
this.txt_mesa.setText(comanda.getMesa());
this.tid.setText(comanda.get_id());
this.torden.setText(comanda.getOrden());
for (int i = 0; i < tope ; i++) {
apuntador =cadena.charAt(i);
if (Buscado.equals("Bebidas"))
{
cadena="";
break;
}
else
{
if (apuntador == '$')
{
cadena="";
break;
}
else
{
//EJECUCION PRINCIPAL
if (apuntador == '#' && !tijera_categoria) {
if (i == 0) {
indiin = i + 1;
}
}
if (apuntador == '!' && !tijera_categoria) {
tijera_categoria = true;
tijera_articulo = false;
indisup = i;
id =id+1;
Buscado = cadena.substring(indiin, indisup);
indiin = indisup + 1;
Buscado_categoria = Buscado;
tag= position + ""+id;
this.be = new Button(inflater.getContext());
creador= true;
this.be.setTag(tag);
// this.be.setId(position);
this.be.setOnClickListener(MyViewHolder.this);
}
if (apuntador == '%' && !tijera_articulo) {
indisup = i;
tijera_articulo = true;
tijera_contorno = false;
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")) {
this.be.setBackgroundTintList(inflater.getContext().getResources().getColorStateList(R.color.fondos, null));
}
if (Buscado_categoria.equals("Entradas")) {
this.be.setBackgroundTintList(inflater.getContext().getResources().getColorStateList(R.color.entradas, null));
}
if (Buscado_categoria.equals("Postres")) {
this.be.setBackgroundTintList(inflater.getContext().getResources().getColorStateList(R.color.postres, null));
}
be.setText(Buscado);
be.setLayoutParams(new LinearLayout.LayoutParams(this.anchomanda, LinearLayout.LayoutParams.WRAP_CONTENT));
this.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;
this.te = new TextView(inflater.getContext());
this.te.setText(Buscado);
this.te.setLayoutParams(new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
this.te.setTextSize((inflater.getContext().getResources().getDimension(R.dimen.txt_size)) / 2);
if (creador){
this.le = new LinearLayout((inflater.getContext()));
creador=false;
}
this.le.addView(this.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("")) {
this.le.setLayoutParams(new LinearLayout.LayoutParams(anchomanda, ViewGroup.LayoutParams.WRAP_CONTENT));
this.le.setOrientation(LinearLayout.VERTICAL);
this.le.setBackground(inflater.getContext().getDrawable(customborder));
this.lyocomanda.addView(this.le);
//cadena ="";
}
}
}//FIN DE LA EJECUCION PRINCIPAL
}//DEL INICIO DE EJECUCION PRINCIPAL
}// DEL FOR
}
@Override
public void onClick(View v){
int position = getAdapterPosition();
if (v.getTag().equals("primal")){
mostrarMensaje("borrado" + position);
removeItem(position);
}else{
mostrarMensaje("BOTON NO CONFIGURADO:" + v.getTag().toString());
}
}
}