Im пытается реализовать динамически textview, в моем держателе просмотра, в первом прокрутке все идет хорошо, карты и так являются свойствами для каждого, но если если я прокручу резервную копию снова текстовое начало начинает появляться, где им не следует.Добавление динамически текстовых просмотров в RecyclerView ViewHolder испортило представления
мой пользовательский адаптер:
public class CustomAdapter extends RecyclerView.Adapter<CustomAdapter.ViewHolder> {
public interface OnItemClickListener {
void onItemClick(MyProduct product);
}
int counter = 0;
private OnItemClickListener listener;
private Context context;
List<MyProduct> myProducts;
public CustomAdapter(OnItemClickListener listener, List<MyProduct> myProducts, Context context)
{
super();
this.listener = listener;
this.myProducts = myProducts;
this.context = context;
}
@Override
public CustomAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.product_list, parent, false);
ViewHolder viewHolder = new ViewHolder(v);
return viewHolder;
}
@Override
public void onBindViewHolder(CustomAdapter.ViewHolder holder, int position) {
if(myProducts != null) {
holder.bind(myProducts.get(position), listener);
counter++;
}
}
@Override
public int getItemCount() {
return myProducts.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
//Views
public ImageView imageView;
public TextView textViewProductTitle;
public TextView textViewProductDescription;
public TextView textViewProductSerialNumber;
public TextView textViewProductPrice;
List<TextView> textViews;
ViewGroup viewGroup;
//Initializing Views
public ViewHolder(View itemView) {
super(itemView);
viewGroup = (ViewGroup)itemView.findViewById(R.id.linearCatalog);
textViews = new LinkedList<>();
imageView = (ImageView) itemView.findViewById(R.id.thumbnail);
textViewProductTitle = (TextView) itemView.findViewById(R.id.textViewProductTitle);
textViewProductDescription = (TextView) itemView.findViewById(R.id.textViewProductDescription);
textViewProductSerialNumber = (TextView) itemView.findViewById(R.id.textViewProductSerialNumber);
textViewProductPrice = (TextView) itemView.findViewById(R.id.textViewProductPrice);
}
public void bind(final MyProduct item, final OnItemClickListener listener) {
Glide.with(context).load(item.getProductImage())
/*.asBitmap().format(DecodeFormat.PREFER_ARGB_8888).*/
.crossFade()
.diskCacheStrategy(DiskCacheStrategy.ALL)
.into(imageView);
textViewProductTitle.setText(item.getProductTitle());
textViewProductDescription.setText(item.getProductDescription());
textViewProductSerialNumber.setText(item.getProductSn());
textViewProductPrice.setText(item.getProductPrice());
if(counter< myProducts.size())
{
for (int i = 0; i < item.getBranchCounter(); i++) {
TextView textView = new TextView(context);
textView.setText(item.getAddressList().get(i));
textViews.add(textView);
viewGroup.addView(textView);
}
}
itemView.setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View v) {
listener.onItemClick(item);
}
});
}
}
любой совет будет полезен, ти!
Хорошо, я видел трюк с GONE и VISIBLE, но это если вы знаете, сколько текстового поля вам нужно, мне может понадобиться от 1-5 зависит элемент. switch()? – 2Stoned
Тогда у вас будет какая-то боль. Но я по-прежнему не рекомендую динамически добавлять текстовые комментарии - вам лучше играть с видимостью всех 5 или использовать разные типы просмотров. Альтернативой будет динамическое удаление и добавление в bind, что будет стоить некоторых из эффективности просмотра recycler и быть болью в заднице для кодирования. –
Ок, сэр, переключатель() от 1-5 :) TY !!! – 2Stoned