3
I’m having a problem working with RecyclerView
. Man Adapter
has the following code:
public class CustomAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private List<String> list;
private ViewHolder holder;
private int codOpe;
public void updateList(List<String> lista, int codOpe) {
this.list = lista;
this.codOpe = codOpe;
this.notifyDataSetChanged();
}
public void addItem(int position, String lista) {
this.list.add(position, lista);
this.notifyItemInserted(position);
}
public void removeItem(int position) {
this.list.remove(position);
this.notifyItemRemoved(position);
}
public void limparLista() {
list.clear();
notifyDataSetChanged();
}
@Override
public int getItemCount() {
if (list == null) {
return 0;
} else {
return list.size();
}
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
View view;
if (codOpe != 2) {
view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.lst_capitulos,viewGroup,false);
return ViewHolder.vhCapitulo(view);
} else {
view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.lst_versiculos,viewGroup,false);
return ViewHolder.vhVersiculo(view);
}
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int position) {
int inilst;
int fimlst;
int intRef;
int intTot;
String listaLimpa;
String desCap;
String obsCap;
holder = (ViewHolder) viewHolder;
if (codOpe == 1) {
inilst = this.list.get(position).indexOf(")") + 1;
fimlst = this.list.get(position).length();
listaLimpa = this.list.get(position).substring(inilst, fimlst);
intRef = listaLimpa.indexOf("-");
intTot = listaLimpa.length();
desCap = listaLimpa.substring(0, intRef);
obsCap = listaLimpa.substring(intRef + 1, intTot);
holder.setItemText(desCap, obsCap);
} else if (codOpe == 2) {
holder.setItemText(list.get(position));
} else if (codOpe == 3) {
inilst = this.list.get(position).indexOf(")") + 1;
fimlst = this.list.get(position).length();
listaLimpa = this.list.get(position).substring(inilst, fimlst);
intRef = listaLimpa.indexOf("-");
intTot = listaLimpa.length();
desCap = listaLimpa.substring(0, intRef);
obsCap = listaLimpa.substring(intRef + 1, intTot);
holder.setItemText(desCap, obsCap);
}
}
}
Holder:
public class ViewHolder extends RecyclerView.ViewHolder {
public TextView desCap;
public TextView obsCap;
public WebView wbdVer;
public ViewHolder(View view) {
super(view);
}
public ViewHolder(View view, TextView desCap, TextView obsCap) {
super(view);
this.desCap = desCap;
this.obsCap = obsCap;
}
public ViewHolder(View view, WebView wbdVer) {
super(view);
this.wbdVer = wbdVer;
}
public static RecyclerView.ViewHolder vhCapitulo(View view) {
TextView desCap = (TextView) view.findViewById(R.id.tv_descap);
TextView obsCap = (TextView) view.findViewById(R.id.tv_obscap);
return new ViewHolder(view, desCap, obsCap);
}
public static RecyclerView.ViewHolder vhVersiculo(View view) {
WebView desVer = (WebView) view.findViewById(R.id.wv_desver);
return new ViewHolder(view, desVer);
}
public void setItemText(String desCap, String obsCap) {
this.desCap.setText(desCap);
this.obsCap.setText(obsCap);
}
public void setItemText(String desVer) {
desVer = "<html><body><p align=\"justify\"; style=\"line-height: 1.5\">" + desVer + "</p></body></html>";
new LongOperation().execute(desVer);
}
private class LongOperation extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... params) {
return params[0];
}
@Override
protected void onPostExecute(String result) {
wbdVer.loadData(result, "text/html; charset=utf-8", "utf-8");
}
@Override
protected void onPreExecute() {
}
@Override
protected void onProgressUpdate(Void... values) {
}
}
}
Adding the click event.
rvCapitulo.addOnItemTouchListener(
new ItemClickListener(ActInicio.this, new ItemClickListener.OnItemClickListener() {
@Override
public void onItemClick(View view, int position) {
String retorno = listCapitulo.get(position);
Intent intent = new Intent(ActInicio.this, ActMove.class);
intent.putExtra(Capitulo.DESCAP, descricaoCapitulo);
startActivityForResult(intent, 0);
}
})
);
The problem I’m having is that by reusing recyclerview. When I populate a list, I clear the list with the clean method List() present in Adapter, and then populate again with different data, when receiving the click opens two screens Actmove.class. I believe I have to remove the old views in another way or still reuse the same view but I haven’t found a way that works. Anyone know how to handle it? Thank you.
Where are you running the
rvCapitulo.addOnItemTouchListener
?– ramaral
Remember that he is a add (not a set), he records a Listener each time it is used.
– ramaral
Hello @ramaral, the problem was that I was adding the addOnItemTouchListener event more than once. Your comment helped me see this. Thank you very much.
– Fábio Azevedo