I have an Adapter that fills CardView with 3 layouts (LinerLayout), in turn, they have TextView and ImageView . When you fill out the list (when it goes off the screen) and flipping it over the elements that were shuffled above, several TextViews can climb into one LinearLayout .

public class MSGAdapter extends RecyclerView.Adapter<MSGAdapter.MSGViewHolder> { ArrayList<MessageModel> messages; public MSGAdapter(ArrayList<MessageModel> messages) { this.messages = messages; } @Override public MSGViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.msg_card_layout, parent, false); MSGAdapter.MSGViewHolder nh = new MSGAdapter.MSGViewHolder(v); return nh; } @Override public void onBindViewHolder(MSGViewHolder holder, int position) { Context ctx = holder.itemView.getContext(); LinearLayout.LayoutParams lparams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT); LinearLayout.LayoutParams lparamsFile = new LinearLayout.LayoutParams(150, 150); LinearLayout.LayoutParams mLayout = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT); LinearLayout mLinerLayout = new LinearLayout(ctx); //Основной ΠΊΠΎΠ½Ρ‚Π΅ΠΉΠ½Π΅Ρ€ mLinerLayout.setOrientation(LinearLayout.VERTICAL); mLinerLayout.setLayoutParams(mLayout); LinearLayout linearLayoutTextContent = new LinearLayout(ctx); //ΠšΠΎΠ½Ρ‚Π΅ΠΉΠ½Π΅Ρ€ для тСкста сообщСния linearLayoutTextContent.setOrientation(LinearLayout.HORIZONTAL); linearLayoutTextContent.setLayoutParams(lparams); LinearLayout linearLayoutFileContent = new LinearLayout(ctx); //ΠšΠΎΠ½Ρ‚Π΅ΠΉΠ½Π΅Ρ€ для влоТСния linearLayoutFileContent.setOrientation(LinearLayout.HORIZONTAL); linearLayoutFileContent.setLayoutParams(lparams); linearLayoutFileContent.setPadding(10,10,10,10); TextView messageContent = new TextView(ctx); //ВСкст сообщСния messageContent.setLayoutParams(lparams); messageContent.setPadding(20,20,20,20); messageContent.setText(messages.get(position).getContent().toString()); // Π”ΠΎΠ±Π°Π²Π»Π΅Π½ΠΈΠ΅ тСкста сообщСния if(messages.get(position).getFile() != null) { for (int i = 0; i < messages.get(position).getFile().size(); i++) { ImageView file = new ImageView(ctx);//Π˜Π·ΠΎΠ±Ρ€Π°ΠΆΠ΅Π½ΠΈΠ΅ влоТСния file.setScaleType(ImageView.ScaleType.CENTER_CROP); file.setLayoutParams(lparamsFile); switch (messages.get(position).getFile().get(i).getType().toString()) { case "img": Glide.with(ctx).load(messages.get(position).getFile().get(i).getSrc()).into(file); break; case "doc": file.setImageDrawable(ctx.getResources().getDrawable(R.drawable.doc)); break; case "audio": file.setImageDrawable(ctx.getResources().getDrawable(R.drawable.audio)); break; case "video": file.setImageDrawable(ctx.getResources().getDrawable(R.drawable.video)); break; } linearLayoutFileContent.addView(file); } } linearLayoutTextContent.addView(messageContent); //Π”ΠΎΠ±Π°Π²Π»Π΅Π½ΠΈΠ΅ Π·Π°ΠΏΠΎΠ»Π½Π΅Π½Π½ΠΎΠ³ΠΎ ΠΊΠΎΠ½Ρ‚Π΅ΠΉΠ½Π΅Ρ€Π° Π² Π΄ΠΈΠ°Π»ΠΎΠ³ if(messages.get(position).getFile() != null && !(messages.get(position).getContent().equals("null"))) { // Π‘ΠΎΠΎΠ±Ρ‰Π΅Π½ΠΈΠ΅ ΠΈ Π²Π»ΠΎΠΆΠ΅Π½ΠΈΠ΅ mLinerLayout.addView(linearLayoutTextContent); mLinerLayout.addView(linearLayoutFileContent); holder.cv.addView(mLinerLayout); } else if(messages.get(position).getFile() != null && messages.get(position).getContent().equals("null")){ // Π’Π»ΠΎΠΆΠ΅Π½ΠΈΠ΅ mLinerLayout.addView(linearLayoutFileContent); holder.cv.addView(mLinerLayout); } else if(messages.get(position).getFile() == null && !(messages.get(position).getContent().equals("null"))) { // Π‘ΠΎΠΎΠ±Ρ‰Π΅Π½ΠΈΠ΅ mLinerLayout.addView(linearLayoutTextContent); holder.cv.addView(mLinerLayout); } } @Override public int getItemCount() { return messages.size(); } public static class MSGViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{ CardView cv; public MSGViewHolder(View itemView) { super(itemView); cv = (CardView) itemView.findViewById(R.id.msg_card); } @Override public void onClick(View view) { } } 
  • Perhaps this is due to the fact that in a static MSGViewHolder I only have CardView and the Adapter only remembers it? and the rest of the elements he creates in a new state and do not remember them? - Heaven

1 answer 1

You are using the adapter incorrectly. All element creation should occur in onCreateViewHolder() . onBindViewHolder() receives a link to the structure created during onCreateViewHolder() (i.e., all the views have already been created) and exclusively controls their visibility, appearance, and displayed data.