我遇到了问题,我不明白为什么CardViews
会产生这种影响。
我需要选择一个或多个CardViews
,但我的适配器无法正常工作。我的适配器实现不正确吗?
我的适配器
public class PersonasAdapter extends RecyclerView.Adapter < PersonasAdapter.PersonasViewHolder > {
private Persona subject;
private List < Persona > personastList;
public PersonasAdapter() {}
public PersonasAdapter(List < Persona > personastList) {
this.personastList = personastList;
}
@Override
public void onBindViewHolder(final PersonasViewHolder personasViewHolder, int i) {
Persona ci = personastList.get(i);
personasViewHolder.txtNombre.setText(ci.getNombre());
personasViewHolder.txtUsuario.setText(ci.getUsuario());
personasViewHolder.txtTwitter.setText(ci.getTwitter());
subject = personastList.get(i);
personasViewHolder.card_view.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
Toast.makeText(v.getContext(), "Eliminar Cardview", Toast.LENGTH_SHORT).show();
personasViewHolder.card_view.setBackgroundResource(R.color.colorPrimary);
return false;
}
});
personasViewHolder.card_view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(v.getContext(), "Reproducir", Toast.LENGTH_SHORT).show();
}
});
}
@Override
public PersonasViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View itemView = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.card_layout_persona, viewGroup, false);
cardView
return new PersonasViewHolder(itemView);
}
类静态
public static class PersonasViewHolder extends RecyclerView.ViewHolder {
protected TextView txtNombre;
protected TextView txtUsuario;
protected TextView txtTwitter;
protected CardView card_view;
public PersonasViewHolder(View v) {
super(v);
txtNombre = (TextView) v.findViewById(R.id.txtNombre);
txtUsuario = (TextView) v.findViewById(R.id.txtUsuario);
txtTwitter = (TextView) v.findViewById(R.id.txtTwitter);
card_view = (CardView) v.findViewById(R.id.card_view);
}
}
//Metodo que nos retornarta la cantidad de personas en la lista
@Override
public int getItemCount() {
return personastList.size();
}
}
答案 0 :(得分:0)
问题如下:
您可以通过将每个cardview的状态存储在一个数组中,然后为每个位置绑定正确的颜色来修复它。我修改了你的代码来表明这个想法:
public class PersonasAdapter extends RecyclerView.Adapter < PersonasAdapter.PersonasViewHolder > {
private Persona subject;
private List < Persona > personastList;
private boolean[] selectedPersonas;
public PersonasAdapter() {}
public PersonasAdapter(List < Persona > personastList) {
this.personastList = personastList;
this.selectedPersonas = new boolean[personasList.size()];
for (int i = 0; i < personasList.size(); i++) {
this.selectedPersonas[i] = false;
}
}
@Override
public void onBindViewHolder(final PersonasViewHolder personasViewHolder, final int i) {
Persona ci = personastList.get(i);
personasViewHolder.txtNombre.setText(ci.getNombre());
personasViewHolder.txtUsuario.setText(ci.getUsuario());
personasViewHolder.txtTwitter.setText(ci.getTwitter());
subject = personastList.get(i);
updatePersonaHolderBackground(personaViewHolder, i);
personasViewHolder.card_view.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
Toast.makeText(v.getContext(), "Eliminar Cardview", Toast.LENGTH_SHORT).show();
selectedPersonas[i] = !selectedPersonas[i];
updatePersonaHolderBackground(personaViewHolder, i);
return false;
}
});
personasViewHolder.card_view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(v.getContext(), "Reproducir", Toast.LENGTH_SHORT).show();
}
});
}
@Override
public PersonasViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View itemView = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.card_layout_persona, viewGroup, false);
cardView
return new PersonasViewHolder(itemView);
}
private void updatePersonaHolderBackground(final PersonasViewHolder personasViewHolder, final int i) {
if (selectedPersonas[i]) {
personasViewHolder.card_view.setBackgroundResource(R.color.colorPrimary);
} else {
// TODO: change the color of personasViewHolder back to normal using whatever color you want.
}
}
答案 1 :(得分:0)
我解决了我的问题。 问题是cardview的位置。
我宣布全局变量
int globalPosition;
和
personasViewHolder.card_view.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
Toast.makeText(v.getContext(), "Eliminar Cardview", Toast.LENGTH_SHORT).show();
personasViewHolder.card_view.setBackgroundResource(R.color.colorPrimary);
globalPosition=i;
notifyDataSetChanged();
return false;
}
});
并在
中进行验证和inBindViewHolder中的if
if (i == globalPosition) {
personasViewHolder.card_view.setBackgroundResource(R.color.colorPrimary);
} else {
personasViewHolder.card_view.setBackgroundResource(R.color.colorAccent);
}
结果就是这个。
public class PersonasAdapter extends RecyclerView.Adapter < PersonasAdapter.PersonasViewHolder > {
private Persona subject;
private List < Persona > personastList;
public PersonasAdapter() {}
int globalPosition;
public PersonasAdapter(List < Persona > personastList) {
this.personastList = personastList;
}
@Override
public void onBindViewHolder(final PersonasViewHolder personasViewHolder, final int i) {
Persona ci = personastList.get(i);
personasViewHolder.txtNombre.setText(ci.getNombre());
personasViewHolder.txtUsuario.setText(ci.getUsuario());
personasViewHolder.txtTwitter.setText(ci.getTwitter());
subject = personastList.get(i);
personasViewHolder.card_view.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
Toast.makeText(v.getContext(), "Eliminar Cardview", Toast.LENGTH_SHORT).show();
personasViewHolder.card_view.setBackgroundResource(R.color.colorPrimary);
globalPosition=i;
notifyDataSetChanged();
return false;
}
});
personasViewHolder.card_view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(v.getContext(), "Reproducir", Toast.LENGTH_SHORT).show();
}
});
if(i==globalPosition)
{
//change color like
personasViewHolder.card_view.setBackgroundResource(R.color.colorPrimary);
}
else
{
personasViewHolder.card_view.setBackgroundResource(R.color.colorAccent);
}
}
@Override
public PersonasViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View itemView = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.card_layout_persona, viewGroup, false);
return new PersonasViewHolder(itemView);
}
public static class PersonasViewHolder extends RecyclerView.ViewHolder {
protected TextView txtNombre;
protected TextView txtUsuario;
protected TextView txtTwitter;
protected CardView card_view;
public PersonasViewHolder(View v) {
super(v);
txtNombre = (TextView) v.findViewById(R.id.txtNombre);
txtUsuario = (TextView) v.findViewById(R.id.txtUsuario);
txtTwitter = (TextView) v.findViewById(R.id.txtTwitter);
card_view = (CardView) v.findViewById(R.id.card_view);
}
}
//Metodo que nos retornarta la cantidad de personas en la lista
@Override
public int getItemCount() {
return personastList.size();
}
}